<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">{{ $t('components.learnSysLayout.navigation.handlbook') }}</a>
        <!-- <a href="https://library.ciis.edu" target="_blank">{{ $t('components.learnSysLayout.navigation.library') }}</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="$t('components.learnSysLayout.navigation.dialogTitle')"
      width="460px"
      :visible.sync="dialogVisible"
      :center="true"
      :append-to-body="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @closed="handleClose"
    >
      <div class="sign">
        <p>
          {{ $t('components.learnSysLayout.navigation.dialogMessage1')
          }}<a :href="$t('components.learnSysLayout.navigation.dialogMessageHref')" target="_blank">{{
            $t('components.learnSysLayout.navigation.dialogMessage2')
          }}</a
          >{{ $t('components.learnSysLayout.navigation.dialogMessage3') }}
        </p>
        <el-checkbox v-model="checked">{{ $t('components.learnSysLayout.navigation.dialogCheckbox') }}</el-checkbox>
      </div>
      <template #footer>
        <el-button type="primary" :disabled="!checked" @click="handlePrimary">{{
          $t('components.learnSysLayout.navigation.dialogButton')
        }}</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: '',
      timer: null
    }
  },
  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.error('处理签名失败')
        }
      })
    },
    getSignDocument(flowid) {
      return cAction.Other.getSignDocument({ flowid }).then(response => {
        const { code, data } = response
        if (code === 1) {
          this.documentUrl = data.url
        }
        return response
      })
    },
    setTimer() {
      this.timer = setInterval(() => {
        this.getSignDocument(this.sign.flowid).then(response => {
          if (response.code === 1) {
            this.clearTimer()
            this.dialogVisible = false
          } else {
            this.dialogVisible = true
          }
        })
      }, 5000)
    },
    clearTimer() {
      this.timer && clearInterval(this.timer)
    },
    handlePrimary() {
      this.setTimer()
      this.newWindowPreview(this.sign.shortUrl)
    },
    handleClose() {
      if (!this.documentUrl) {
        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()
  },
  destroyed() {
    this.clearTimer()
  }
}
</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;
    color: #303133;
    text-decoration: none;
  }
}
</style>
