<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="申请属性" prop="applProp">
        <el-select v-model="queryParams.applProp" placeholder="请选择申请属性" clearable>
          <el-option v-for="dict in eutr_revi_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请等级" prop="applGrade">
        <el-input v-model="queryParams.applGrade" placeholder="请输入申请等级" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="验证结论" prop="veriConc">
        <el-select v-model="queryParams.veriConc" placeholder="请选择验证结论" clearable>
          <el-option v-for="dict in eutr_revi_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="['stan:objstanrevi:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['stan:objstanrevi:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['stan:objstanrevi:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['stan:objstanrevi:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="stan-objstanrevi-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="applProp" width="120">
        <template #default="scope">
          <dict-tag :options="eutr_revi_type" :value="scope.row.applProp" />
        </template>
      </el-table-column>
      <el-table-column label="申请等级" prop="applGrade" />
      <el-table-column label="申请时间" prop="applTime" width="180" />
      <el-table-column label="验证时间" prop="veriTime" width="180" />
      <el-table-column label="验证结论" prop="veriConc" width="120">
        <template #default="scope">
          <dict-tag :options="eutr_revi_stat" :value="scope.row.veriConc" />
        </template>
      </el-table-column>
      <el-table-column label="验证意见" prop="veriOpin" />
      <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:objstanrevi:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['stan:objstanrevi: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="申请属性" prop="applProp">
          <el-select v-model="form.applProp" placeholder="请选择申请属性">
            <el-option v-for="dict in eutr_revi_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请等级" prop="applGrade">
          <el-input v-model="form.applGrade" placeholder="请输入申请等级" />
        </el-form-item>
        <el-form-item label="申请组织GUID" prop="applOrgGuid">
          <el-input v-model="form.applOrgGuid" placeholder="请输入申请组织GUID" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applTime">
          <el-date-picker v-model="form.applTime" type="datetime" placeholder="选择申请时间" clearable />
        </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="veriConc">
          <el-select v-model="form.veriConc" placeholder="请选择验证结论">
            <el-option v-for="dict in eutr_revi_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="验证意见" prop="veriOpin">
          <el-input v-model="form.veriOpin" type="textarea" placeholder="请输入验证意见" />
        </el-form-item>
        <el-form-item label="标准化前状况" prop="befoStanSitu">
          <el-input v-model="form.befoStanSitu" type="textarea" placeholder="请输入标准化前状况" />
        </el-form-item>
        <el-form-item label="自评结论" prop="selfEvaConc">
          <el-input v-model="form.selfEvaConc" type="textarea" placeholder="请输入自评结论" />
        </el-form-item>
        <el-form-item label="验证单位编码" prop="veriWiunCode">
          <el-input v-model="form.veriWiunCode" 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="ObjStanRevi">
import { listObjStanRevi, getObjStanRevi, addObjStanRevi, updateObjStanRevi, delObjStanRevi } from "@/api/slaj/stan";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const { eutr_revi_type, eutr_revi_stat } = proxy.useDict("eutr_revi_type", "eutr_revi_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({ applProp: { visible: true }, applGrade: { visible: true }, applTime: { visible: true }, veriTime: { visible: true }, veriConc: { visible: true }, veriOpin: { visible: true } });

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, applProp: null, applGrade: null, veriConc: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

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

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

function handleExport() {
  proxy.download('/stan/objstanrevi/export', { ...queryParams.value }, `标准化评审对象_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { guid: null, applProp: null, applGrade: null, applOrgGuid: null, applTime: null, veriTime: null, veriConc: null, veriOpin: null, befoStanSitu: null, selfEvaConc: null, veriWiunCode: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
