<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="培训类型" prop="trainType">
        <el-select v-model="queryParams.trainType" placeholder="请选择培训类型" clearable>
          <el-option label="类型一" value="1" />
          <el-option label="类型二" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="veriStat">
        <el-select v-model="queryParams.veriStat" placeholder="请选择审核状态" clearable>
          <el-option label="待审核" value="0" />
          <el-option label="已审核" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="培训机构" prop="trainOrgName">
        <el-input v-model="queryParams.trainOrgName" placeholder="请输入培训机构" clearable @keyup.enter="handleQuery" />
      </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:ttsatrain:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['ttsa:ttsatrain:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['ttsa:ttsatrain:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ttsa:ttsatrain:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="ttsa-ttsatrain-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="trainOrgName" />
      <el-table-column label="培训类型" prop="trainType">
        <template #default="scope">
          <dict-tag :options="trainTypeOptions" :value="scope.row.trainType" />
        </template>
      </el-table-column>
      <el-table-column label="培训开始时间" prop="trainStartTime" width="180" />
      <el-table-column label="培训完成时间" prop="trainCompTime" width="180" />
      <el-table-column label="培训地址" prop="trainAddr" />
      <el-table-column label="培训人数" prop="trainPersTot" />
      <el-table-column label="审核状态" prop="veriStat">
        <template #default="scope">
          <dict-tag :options="veriStatOptions" :value="scope.row.veriStat" />
        </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:ttsatrain:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['ttsa:ttsatrain: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="citNum">
          <el-input v-model="form.citNum" placeholder="请输入城市编号" />
        </el-form-item>
        <el-form-item label="所属组织" prop="orgGuid">
          <el-input v-model="form.orgGuid" placeholder="请输入所属组织" />
        </el-form-item>
        <el-form-item label="培训机构" prop="trainOrgName">
          <el-input v-model="form.trainOrgName" placeholder="请输入培训机构" />
        </el-form-item>
        <el-form-item label="培训类型" prop="trainType">
          <el-select v-model="form.trainType" placeholder="请选择培训类型">
            <el-option label="类型一" value="1" />
            <el-option label="类型二" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="培训开始时间" prop="trainStartTime">
          <el-date-picker v-model="form.trainStartTime" type="datetime" placeholder="选择培训开始时间" clearable />
        </el-form-item>
        <el-form-item label="培训完成时间" prop="trainCompTime">
          <el-date-picker v-model="form.trainCompTime" type="datetime" placeholder="选择培训完成时间" clearable />
        </el-form-item>
        <el-form-item label="培训教师" prop="trainTeat">
          <el-input v-model="form.trainTeat" placeholder="请输入培训教师" />
        </el-form-item>
        <el-form-item label="培训地址" prop="trainAddr">
          <el-input v-model="form.trainAddr" placeholder="请输入培训地址" />
        </el-form-item>
        <el-form-item label="培训机构GUID" prop="trainOrgGuid">
          <el-input v-model="form.trainOrgGuid" placeholder="请输入培训机构GUID" />
        </el-form-item>
        <el-form-item label="培训人数" prop="trainPersTot">
          <el-input v-model="form.trainPersTot" placeholder="请输入培训人数" />
        </el-form-item>
        <el-form-item label="培训课时" prop="trainHour">
          <el-input v-model="form.trainHour" placeholder="请输入培训课时" />
        </el-form-item>
        <el-form-item label="审核状态" prop="veriStat">
          <el-select v-model="form.veriStat" placeholder="请选择审核状态">
            <el-option label="待审核" value="0" />
            <el-option label="已审核" value="1" />
          </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="TtsaTrain">
import { listBisTtsaTrain, getBisTtsaTrain, addBisTtsaTrain, updateBisTtsaTrain, delBisTtsaTrain } from "@/api/slaj/ttsa";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();

const trainTypeOptions = ref([{ label: "类型一", value: "1" }, { label: "类型二", value: "2" }]);
const veriStatOptions = ref([{ label: "待审核", value: "0" }, { label: "已审核", value: "1" }]);

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({ trainOrgName: { visible: true }, trainType: { visible: true }, trainStartTime: { visible: true }, trainCompTime: { visible: true }, trainAddr: { visible: true }, trainPersTot: { visible: true }, veriStat: { visible: true } });

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, trainType: null, veriStat: null, trainOrgName: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

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

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

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

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

function reset() {
  form.value = { guid: null, citNum: null, orgGuid: null, trainOrgName: null, trainType: null, trainStartTime: null, trainCompTime: null, trainTeat: null, trainAddr: null, trainOrgGuid: null, trainPersTot: null, trainHour: null, veriStat: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
