<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="证书GUID" prop="stanCertGuid">
        <el-input v-model="queryParams.stanCertGuid" placeholder="请输入证书GUID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="是否同意" prop="ifAgree">
        <el-select v-model="queryParams.ifAgree" placeholder="请选择是否同意" clearable>
          <el-option v-for="dict in eutr_appl_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="变更类型" prop="chanType">
        <el-select v-model="queryParams.chanType" placeholder="请选择变更类型" clearable>
          <el-option v-for="dict in eutr_cha_type" :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="['stan:stancertchaappl:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['stan:stancertchaappl:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['stan:stancertchaappl:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['stan:stancertchaappl:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="stan-stancertchaappl-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="证书GUID" prop="stanCertGuid" />
      <el-table-column label="变更类型" prop="chanType" width="120">
        <template #default="scope">
          <dict-tag :options="eutr_cha_type" :value="scope.row.chanType" />
        </template>
      </el-table-column>
      <el-table-column label="申请时间" prop="apllTime" width="180" />
      <el-table-column label="是否同意" prop="ifAgree" width="120">
        <template #default="scope">
          <dict-tag :options="eutr_appl_stat" :value="scope.row.ifAgree" />
        </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="['stan:stancertchaappl:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['stan:stancertchaappl: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="stanCertGuid">
          <el-input v-model="form.stanCertGuid" placeholder="请输入证书GUID" />
        </el-form-item>
        <el-form-item label="变更名称" prop="chanName">
          <el-input v-model="form.chanName" placeholder="请输入变更名称" />
        </el-form-item>
        <el-form-item label="变更类型" prop="chanType">
          <el-select v-model="form.chanType" placeholder="请选择变更类型">
            <el-option v-for="dict in eutr_cha_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请人" prop="applPers">
          <el-input v-model="form.applPers" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="风险分析" prop="riskAna">
          <el-input v-model="form.riskAna" type="textarea" placeholder="请输入风险分析" />
        </el-form-item>
        <el-form-item label="控制措施" prop="contMeas">
          <el-input v-model="form.contMeas" type="textarea" placeholder="请输入控制措施" />
        </el-form-item>
        <el-form-item label="申请时间" prop="apllTime">
          <el-date-picker v-model="form.apllTime" type="datetime" placeholder="选择申请时间" clearable />
        </el-form-item>
        <el-form-item label="是否同意" prop="ifAgree">
          <el-select v-model="form.ifAgree" placeholder="请选择是否同意">
            <el-option v-for="dict in eutr_appl_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="验证时间" prop="veriTime">
          <el-date-picker v-model="form.veriTime" type="datetime" placeholder="选择验证时间" clearable />
        </el-form-item>
        <el-form-item label="验证人" prop="veriPers">
          <el-input v-model="form.veriPers" placeholder="请输入验证人" />
        </el-form-item>
        <el-form-item label="单位意见" prop="wiunOpin">
          <el-input v-model="form.wiunOpin" type="textarea" placeholder="请输入单位意见" />
        </el-form-item>
        <el-form-item label="批准时间" prop="apprTime">
          <el-date-picker v-model="form.apprTime" type="datetime" placeholder="选择批准时间" clearable />
        </el-form-item>
        <el-form-item label="批准人" prop="apprPers">
          <el-input v-model="form.apprPers" placeholder="请输入批准人" />
        </el-form-item>
        <el-form-item label="批准是否同意" prop="apprIfAgree">
          <el-input v-model="form.apprIfAgree" placeholder="请输入批准是否同意" />
        </el-form-item>
        <el-form-item label="批准单位意见" prop="apprWiunOpin">
          <el-input v-model="form.apprWiunOpin" type="textarea" placeholder="请输入批准单位意见" />
        </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="StanCertChaAppl">
import { listBisStanCertChaAppl, getBisStanCertChaAppl, addBisStanCertChaAppl, updateBisStanCertChaAppl, delBisStanCertChaAppl } from "@/api/slaj/stan";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const { eutr_appl_stat, eutr_cha_type } = proxy.useDict("eutr_appl_stat", "eutr_cha_type");

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({ stanCertGuid: { visible: true }, chanType: { visible: true }, apllTime: { visible: true }, ifAgree: { visible: true } });

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, stanCertGuid: null, ifAgree: null, chanType: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

function getList() {
  loading.value = true;
  listBisStanCertChaAppl(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();
  getBisStanCertChaAppl(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) {
        updateBisStanCertChaAppl(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBisStanCertChaAppl(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

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

function handleExport() {
  proxy.download('/stan/stancertchaappl/export', { ...queryParams.value }, `证书变更申请_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { guid: null, stanCertGuid: null, chanName: null, chanType: null, applPers: null, riskAna: null, contMeas: null, apllTime: null, ifAgree: null, veriTime: null, veriPers: null, wiunOpin: null, apprTime: null, apprPers: null, apprIfAgree: null, apprWiunOpin: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
