<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="证书编号" prop="certCode">
        <el-input v-model="queryParams.certCode" placeholder="请输入证书编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="证书类型" prop="ttsaCertType">
        <el-select v-model="queryParams.ttsaCertType" placeholder="请选择证书类型" clearable>
          <el-option v-for="dict in ttsa_cert_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="证书状态" prop="certStat">
        <el-select v-model="queryParams.certStat" placeholder="请选择证书状态" clearable>
          <el-option v-for="dict in ttsa_cert_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['ttsa:ttsacert:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ttsa:ttsacert:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ttsa:ttsacert:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ttsa:ttsacert:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="ttsa-ttsacert-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="证书编号" prop="certCode" />
      <el-table-column label="证书类型" prop="ttsaCertType" width="120">
        <template #default="scope">
          <dict-tag :options="ttsa_cert_type" :value="scope.row.ttsaCertType" />
        </template>
      </el-table-column>
      <el-table-column label="发证时间" prop="issueTime" width="180" />
      <el-table-column label="证书到期时间" prop="certExpiTime" width="180" />
      <el-table-column label="颁发单位" prop="grantInst" />
      <el-table-column label="证书状态" prop="certStat" width="120">
        <template #default="scope">
          <dict-tag :options="ttsa_cert_stat" :value="scope.row.certStat" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ttsa:ttsacert:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ttsa:ttsacert:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="父级GUID" prop="pGuid">
          <el-input v-model="form.pGuid" placeholder="请输入父级GUID" />
        </el-form-item>
        <el-form-item label="阵列GUID" prop="arrayGuid">
          <el-input v-model="form.arrayGuid" placeholder="请输入阵列GUID" />
        </el-form-item>
        <el-form-item label="证书编号" prop="certCode">
          <el-input v-model="form.certCode" placeholder="请输入证书编号" />
        </el-form-item>
        <el-form-item label="人员三类GUID" prop="persTtsGuid">
          <el-input v-model="form.persTtsGuid" placeholder="请输入人员三类GUID" />
        </el-form-item>
        <el-form-item label="三类GUID" prop="ttsaGuid">
          <el-input v-model="form.ttsaGuid" placeholder="请输入三类GUID" />
        </el-form-item>
        <el-form-item label="证书类型" prop="ttsaCertType">
          <el-select v-model="form.ttsaCertType" placeholder="请选择证书类型">
            <el-option v-for="dict in ttsa_cert_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="发证时间" prop="issueTime">
          <el-date-picker v-model="form.issueTime" type="datetime" placeholder="选择发证时间" clearable />
        </el-form-item>
        <el-form-item label="发证组织GUID" prop="issueOrgGuid">
          <el-input v-model="form.issueOrgGuid" placeholder="请输入发证组织GUID" />
        </el-form-item>
        <el-form-item label="证书到期时间" prop="certExpiTime">
          <el-date-picker v-model="form.certExpiTime" type="datetime" placeholder="选择证书到期时间" clearable />
        </el-form-item>
        <el-form-item label="颁发单位" prop="grantInst">
          <el-input v-model="form.grantInst" placeholder="请输入颁发单位" />
        </el-form-item>
        <el-form-item label="证书状态" prop="certStat">
          <el-select v-model="form.certStat" placeholder="请选择证书状态">
            <el-option v-for="dict in ttsa_cert_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TtsaCert">
import { listBisTtsaCert, getBisTtsaCert, addBisTtsaCert, updateBisTtsaCert, delBisTtsaCert } from "@/api/slaj/ttsa";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const { ttsa_cert_type, ttsa_cert_stat } = proxy.useDict("ttsa_cert_type", "ttsa_cert_stat");

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const columns = ref({ certCode: { visible: true }, ttsaCertType: { visible: true }, issueTime: { visible: true }, certExpiTime: { visible: true }, grantInst: { visible: true }, certStat: { visible: true } });

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, certCode: null, ttsaCertType: null, certStat: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

function getList() {
  loading.value = true;
  listBisTtsaCert(queryParams.value).then(r => {
    dataList.value = r.rows;
    total.value = r.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(s) {
  ids.value = s.map(i => i.guid);
  single.value = s.length !== 1;
  multiple.value = !s.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加三类人员证书";
}

function handleUpdate(row) {
  reset();
  getBisTtsaCert(row.guid || ids.value[0]).then(r => {
    form.value = r.data;
    open.value = true;
    title.value = "修改三类人员证书";
  });
}

function submitForm() {
  proxy.$refs["formRef"].validate(v => {
    if (v) {
      if (form.value.guid != null) {
        updateBisTtsaCert(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBisTtsaCert(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  proxy.$modal.confirm('确认删除？').then(() => delBisTtsaCert(row.guid || ids.value.join(","))).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  });
}

function handleExport() {
  proxy.download('/ttsa/ttsacert/export', { ...queryParams.value }, `三类人员证书_${new Date().getTime()}.xlsx`);
}

function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.value = { guid: null, pGuid: null, arrayGuid: null, certCode: null, persTtsGuid: null, ttsaGuid: null, ttsaCertType: null, issueTime: null, issueOrgGuid: null, certExpiTime: null, grantInst: null, certStat: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
