<template>
  <div class="menu">
    <div class="nav-left">
      <img class="logo" src="@/assets/images/logo-header.png" alt="logo" />
      <div class="text-title" @click="setStatus">{{ $t('components.learnSysLayout.navigation.title') }}</div>
    </div>
    <div class="nav-right">
      <div class="nav-a">
        <a :href="documentUrl" target="_blank">查看学员手册</a>
        <a href="https://library.ciis.edu" target="_blank">电子图书馆</a>
      </div>
      <div class="notify" @click="goNotify()">
        {{ $t('components.learnSysLayout.navigation.tip') }}
        <div class="num" v-if="this.$store.getters.myMsg != 0">{{ this.$store.getters.myMsg }}</div>
      </div>
      <language-switch />
    </div>
    <el-dialog
      title="学员须知"
      width="460px"
      :visible.sync="dialogVisible"
      :center="true"
      :append-to-body="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="handleClose"
    >
      <div class="sign">
        <p>
          使用学习平台前，请认真阅读<a
            href="https://zws-imgs-pub.oss-cn-beijing.aliyuncs.com/pc/ciis/CIIS%E5%AD%A6%E7%94%9F%E6%89%8B%E5%86%8C.pdf"
            target="_blank"
            >《紫荆教育-CIIS应用心理学硕士项目学员手册》</a
          >内容，并同意遵守其中的各项要求。
        </p>
        <el-checkbox v-model="checked">我已阅读并同意</el-checkbox>
      </div>
      <template #footer>
        <el-button type="primary" :disabled="!checked" @click="handlePrimary">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import LanguageSwitch from '@/components/languageSwitch/index.vue'
import cAction from '../../action'

export default {
  components: { LanguageSwitch },
  data() {
    return {
      num: this.$store.getters.myMsg,
      dialogVisible: false,
      checked: false,
      sign: {},
      documentUrl: ''
    }
  },
  mounted() {
    cAction.Other.getNavMsg()
      .then(data => {
        this.$store.commit('myMsg', data.num)
      })
      .catch(e => {
        this.$message.error(e.message)
      })
      .finally(() => {})
  },
  methods: {
    goNotify() {
      this.$router.push({ path: '/app/other/message' })
    },
    setStatus() {
      if (this.status) {
        this.status = false
      } else {
        this.status = true
      }
      this.$root.$emit('updateStatus', this.status)
    },
    createSign() {
      cAction.Other.createSign().then(response => {
        const { code, data } = response
        if (code === 1) {
          this.sign = data
          this.getSignDocument(data.flowid).then(() => {
            if (!this.documentUrl) {
              this.dialogVisible = true
            }
          })
        } else {
          this.$message({ type: 'error', message: '处理签名失败' })
        }
      })
    },
    getSignDocument(flowid) {
      return cAction.Other.getSignDocument({ flowid }).then(response => {
        const { code, data } = response
        if (code === 1) {
          this.documentUrl = data.url
        }
        return response
      })
    },
    handlePrimary() {
      this.getSignDocument(this.sign.flowid).then(response => {
        if (this.documentUrl) {
          this.dialogVisible = false
        } else {
          this.newWindowPreview(this.sign.shortUrl)
        }
      })
    },
    handleClose() {
      this.getSignDocument(this.sign.flowid).then(response => {
        if (this.documentUrl) {
          this.dialogVisible = false
        } else {
          window.location.href = webConf.others.loginUrl
        }
      })
    },
    // 新窗口预览
    newWindowPreview(url) {
      const a = document.createElement('a')
      a.href = url
      a.target = '_blank'
      document.body.appendChild(a)
      a.click()
      a.remove()
    }
  },
  beforeMount() {
    this.createSign()
  }
}
</script>

<style scoped lang="scss">
.menu {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #b80037;
  .nav-left {
    float: left;
    position: relative;
    height: 100%;
    img.logo {
      position: absolute;
      left: 0;
      top: 50%;
      // height: 80%;
      width: 160px;
      transform: translateY(-50%);
    }
    .text-title {
      float: left;
      margin-left: 190px;
      line-height: 60px;
      font-size: 16px;
    }
  }
  .nav-right {
    display: flex;
    align-items: center;
  }
  .notify {
    position: relative;
    float: right;
    padding: 0 20px;
    line-height: 60px;
    font-size: 16px;
    cursor: pointer;
    .num {
      position: absolute;
      top: 9px;
      left: 50px;
      padding: 1px 5px 2px 4px;
      font-size: 12px;
      line-height: 14px;
      color: #ffffff;
      background: #fb2121;
      border-radius: 50%;
    }
    &:hover {
      color: #b49441;
    }
  }
}

@media (max-width: 767px) {
  .menu {
    .nav-left {
      img.logo {
        display: none;
      }
      .text-title {
        margin-left: 0;
      }
    }
  }
}
.sign {
  p {
    text-indent: 2em;
    line-height: 24px;
  }
}
.nav-a {
  a {
    margin: 0 10px;
  }
}
</style>
