<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="计划名称" prop="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入计划名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="审批状态" prop="apprStat">
        <el-select v-model="queryParams.apprStat" placeholder="请选择审批状态" clearable>
          <el-option label="待审批" value="待审批" />
          <el-option label="已审批" value="已审批" />
          <el-option label="已驳回" value="已驳回" />
        </el-select>
      </el-form-item>
      <el-form-item label="上报状态" prop="repStat">
        <el-select v-model="queryParams.repStat" placeholder="请选择上报状态" clearable>
          <el-option label="待上报" value="待上报" />
          <el-option label="已上报" 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:ttsaexamplan:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ttsa:ttsaexamplan:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ttsa:ttsaexamplan:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ttsa:ttsaexamplan:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="ttsa-ttsaexamplan-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="planName" />
      <el-table-column label="成立时间" prop="estaTime" width="180" />
      <el-table-column label="审批状态" prop="apprStat">
        <template #default="scope">
          <dict-tag :options="appr_stat_options" :value="scope.row.apprStat" />
        </template>
      </el-table-column>
      <el-table-column label="上报状态" prop="repStat">
        <template #default="scope">
          <dict-tag :options="rep_stat_options" :value="scope.row.repStat" />
        </template>
      </el-table-column>
      <el-table-column label="申请时间" prop="applTime" width="180" />
      <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:ttsaexamplan:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ttsa:ttsaexamplan: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="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称" />
        </el-form-item>
        <el-form-item label="成立组织" prop="estaOrgGuid">
          <el-input v-model="form.estaOrgGuid" placeholder="请输入成立组织" />
        </el-form-item>
        <el-form-item label="成立时间" prop="estaTime">
          <el-date-picker v-model="form.estaTime" type="datetime" placeholder="选择成立时间" clearable />
        </el-form-item>
        <el-form-item label="计划内容" prop="planCont">
          <el-input v-model="form.planCont" type="textarea" placeholder="请输入计划内容" />
        </el-form-item>
        <el-form-item label="审批状态" prop="apprStat">
          <el-select v-model="form.apprStat" placeholder="请选择审批状态">
            <el-option label="待审批" value="待审批" />
            <el-option label="已审批" value="已审批" />
            <el-option label="已驳回" value="已驳回" />
          </el-select>
        </el-form-item>
        <el-form-item label="审批组织" prop="apprOrgGuid">
          <el-input v-model="form.apprOrgGuid" placeholder="请输入审批组织" />
        </el-form-item>
        <el-form-item label="审批意见" prop="apprOpin">
          <el-input v-model="form.apprOpin" type="textarea" placeholder="请输入审批意见" />
        </el-form-item>
        <el-form-item label="上报状态" prop="repStat">
          <el-select v-model="form.repStat" placeholder="请选择上报状态">
            <el-option label="待上报" value="待上报" />
            <el-option label="已上报" 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="TtsaExamPlan">
import { listBisTtsaExamPlan, getBisTtsaExamPlan, addBisTtsaExamPlan, updateBisTtsaExamPlan, delBisTtsaExamPlan } from "@/api/slaj/ttsa";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();

const appr_stat_options = ref([
  { label: "待审批", value: "待审批" },
  { label: "已审批", value: "已审批" },
  { label: "已驳回", value: "已驳回" }
]);
const rep_stat_options = ref([
  { label: "待上报", value: "待上报" },
  { label: "已上报", value: "已上报" }
]);

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({ planName: { visible: true }, estaTime: { visible: true }, apprStat: { visible: true }, repStat: { visible: true }, applTime: { visible: true } });

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, planName: null, apprStat: null, repStat: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

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

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

function handleExport() {
  proxy.download('/ttsa/ttsaexamplan/export', { ...queryParams.value }, `考核计划_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { guid: null, planName: null, estaOrgGuid: null, estaTime: null, planCont: null, apprStat: null, apprOrgGuid: null, apprOpin: null, repStat: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
