<template>
  <div class="layout-no-col">
    <el-main class="main">
      <h2 class="page-title">更新产品许可证</h2>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <h4>产品信息</h4>
        </div>
        <div class="content" v-loading="dataLoading">
          <el-row>
            <el-col :span="12" :xs="24"><span class="txt-label">产品代码：</span> <span class="txt-value" v-text="productData.appCode"></span></el-col>
            <el-col :span="12" :xs="24"><span class="txt-label">产品名称：</span> <span class="txt-value" v-text="productData.appName"></span></el-col>
            <el-col :span="12" :xs="24"><span class="txt-label">产品版本：</span> <span class="txt-value" v-text="productData.appVersion"></span></el-col>
            <el-col :span="12" :xs="24"><span class="txt-label">有效期至：</span> <span class="txt-value" v-text="productData.endDate"></span></el-col>
          </el-row>
        </div>
      </el-card>

      <el-card class="box-card">
        <div slot="header" class="clearfix" style="position:relative;">
          <h4>许可证信息</h4>
          <span style="position:absolute;right:-7px;top:-7px;">

          </span>
        </div>
        <div class="content" v-loading="dataLoading">
          <el-form :model="licenseForm" ref="licenseForm" label-width="140px">
            <el-form-item label="许可证请求码：">
              <el-input type="textarea" v-model="productData.requestCode" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item prop="license" label="许可证码：" :verify="['NotNull']">
              <el-input type="textarea" v-model="licenseForm.license" rows="8"></el-input>
            </el-form-item>
            <el-form-item prop="password" label="管理员密码：" :verify="['NotNull']">
              <el-input type="password" ref="password" v-model="licenseForm.password"></el-input>
            </el-form-item>
            <el-form-item label-width="0" style="text-align:center;">
              <el-button type="primary" :loading="submitLoading" style="width:150px" @click="saveLicense">保存许可证</el-button> <el-button :loading="exportLoading" @click="exportLicense">导出许可证码</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <div style="text-align:center;">
        <el-button @click="attentionDialogVisible = true">注意事项</el-button>
        <!-- <el-popover placement="top-start" width="140" trigger="hover">
          <el-button style="margin-left:10px" slot="reference">联系泽元销售</el-button>
          <div>欢迎拨打电话：<span class='txt-phone-number'>400-697-9700</span></div>
        </el-popover> -->
      </div>
    </el-main>
    <el-dialog
      title="注意事项"
      :visible.sync="attentionDialogVisible"
      width="500px">
      <ol class="attention-list">
        <li>在更换新的许可证前，请提前备份旧的许可证，以备新的许可证码出现问题时及时恢复。</li>
      </ol>
      <span slot="footer" class="dialog-footer">
        <el-button @click="attentionDialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import util from '@src/common/util.js'
export default {
  data () {
    return {
      productData: {
        appCode: '',
        appName: '',
        appVersion: '',
        endDate: '',
        requestCode: ''
      },
      licenseForm: {
        license: '',
        password: ''
      },
      attentionDialogVisible: false,
      dataLoading: true,
      submitLoading: false,
      exportLoading: false
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 页面初始化，从后端加载数据
    async init () {
      let res = await axios.get('/ui/license')
      this.dataLoading = false
      if (res.data.status === 1) {
        this.productData = res.data.data
      } else {
        util.showResponseMessage(res.data)
      }
    },
    // 更新服务端的license
    async saveLicense() {
      await util.validateForm(this.$refs.licenseForm)
      this.submitLoading = true
      let res = await axios.put('/ui/license', this.licenseForm)
      this.submitLoading = false
      if (res.data.status === 1) {
        window.location.href = 'index.html'
      } else {
        util.showResponseMessage(res.data)
      }
    },
    // 导出license
    async exportLicense() {
      if (this.licenseForm.password.trim() === '') {
        util.showError('请填写管理员密码')
        this.$refs.password.focus()
        return
      }
      // 有可能注意事项Dialog是打开状态
      this.attentionDialogVisible = false
      this.exportLoading = true
      // 导出前检查管理员密码，如果正确则返回文件名
      let res = await axios.post('/ui/license/check_export', {
        password: this.licenseForm.password
      })
      let resData = res.data
      this.exportLoading = false
      if (resData.status === 1 && typeof resData.fileName !== 'undefined' && resData.fileName !== '') {
        window.open(`./ui/license/download?fileName=${resData.fileName}`)
      } else {
        util.showResponseMessage(resData)
      }
    }
  }

}
</script>
<style scoped>
  .main {
    max-width: 1000px;
    padding: 10px;
    margin: 0 auto;
  }
  .txt-label {
    color: #99a9bf;
  }
  .txt-phone-number {
    display: block;font-size: 18px;color: #67C23A;font-weight: bold;
  }
  .attention-list li{
    margin-bottom: 10px;
    list-style: none
  }
</style>
