<template>
  <section class="AppLicense">
    <ai-list>
      <ai-title slot="title" title="证书管理" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="dialogForm={},dialog=true">添加</el-button>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索证书名称" v-model="search.condition" clearable
            @clear="page.current = 1,search.condition = '', getTableData()"
                      v-throttle="() => {page.current = 1, getTableData()}"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict"
                  @selection-change="v=>ids=v.filter(e=>e.id).map(e=>e.id)">
          <el-table-column slot="options" align="center" label="操作" fixed="right" width="160px">
            <el-row type="flex" justify="center" align="middle" slot-scope="{row}">
              <el-button type="text" @click="handleDownload(row.id)">下载</el-button>
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </el-row>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
    <!--添加证书-->
    <ai-dialog :title="dialogTitle" :visible.sync="dialog" width="600px"
               @onConfirm="saveLicense">
      <el-form ref="licenseForm" :model="dialogForm" :rules="rules" size="small"
               label-width="120px">
        <el-form-item required label="证书名称" prop="name">
          <el-input v-model.trim="dialogForm.name" placeholder="请输入证书名称" clearable
                    :maxLength="100"/>
        </el-form-item>
        <el-form-item label="主板序列号" prop="mainBoard">
          <el-input v-model.trim="dialogForm.mainBoard" placeholder="请输入主板序列号" clearable
                    :maxLength="50"/>
        </el-form-item>
        <el-form-item required label="CPU" prop="cpu">
          <el-input v-model.trim="dialogForm.cpu" placeholder="请输入CPU信息" clearable
                    :maxLength="50"/>
        </el-form-item>
        <el-form-item required label="MAC地址" prop="mac">
          <el-input v-model.trim="dialogForm.mac" placeholder="请输入MAC地址" clearable
                    :maxLength="100"/>
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress">
          <el-input v-model.trim="dialogForm.ipAddress" placeholder="请输入IP地址" clearable
                    :maxLength="100"/>
        </el-form-item>
        <el-form-item label="过期日期" prop="expireDate">
          <el-input v-model.trim="dialogForm.expireDate" placeholder="请输入过期日期" clearable
                    :maxLength="20"/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

<script>

export default {
  name: "AppLicense",
  label: "证书管理",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    isEdit() {
      return !!this.dialogForm.id
    },
    dialogTitle() {
      return this.isEdit ? '修改证书' : '添加证书'
    },
    colConfigs() {
      return [
        {label: "证书名称", prop: "name"},
        {label: "CPU", prop: "cpu", align: 'center'},
        {label: "MAC地址", prop: "mac"},
        {label: "主板序列号", prop: "mainBoard"},
        {label: "IP地址", prop: "ipAddress"},
        {label: "过期日期", prop: "expireDate"},
        {label: "创建时间", prop: "createTime"},
        {slot: "options"}
      ]
    },
    rules() {
      return {
        name: [{required: true, message: "请输入证书名称"}],
        cpu: [{required: true, message: "请输入CPU信息"}],
        mac: [{required: true, message: "请输入MAC地址"}],
        mainBoard: [{required: true, message: "请输入主板序列号"}],
        ipAddress: [{required: true, message: "请输入IP地址"}],
        expireDate: [{required: true, message: "请选择过期日期"}]
      }
    }
  },
  data() {
    return {
      page: {current: 1, size: 10, total: 0},
      dialog: false,
      dialogForm: {},
      tableData: [],
      search: {condition: ""},
      ids: []
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/sysLicense/page", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    // 新增/修改
    saveLicense() {
      this.$refs.licenseForm.validate(v => {
        if (v) {
          this.instance.post("/sysLicense/save", this.dialogForm).then(res => {
            if (res?.code == 0) {
              this.dialog = false;
              this.$message.success("保存成功")
              this.getTableData();
            } else {
              this.$message.error(res?.msg)
            }
          })
        }
      })
    },
    handleDelete(ids) {
      this.$confirm("是否要删除该证书信息?").then(() => {
        this.instance.post("/sysLicense/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.getTableData();
            this.$message.success("删除成功!");
          }
        })
      }).catch(() => 0)
    },
    handleDownload(id) {
      this.instance.post("/sysLicense/create?id="+id, null, {responseType: 'blob'}).then(res => {
        const link = document.createElement('a')
        console.log(res.type)
        let blob = new Blob([res], {type: res.type})
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        link.setAttribute('download', 'license.lic')
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)

      }).catch(() => {
        this.$message.error("证书生成失败!");
      })
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.AppLicense {
  height: 100%;

}
</style>
