<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="课程名称" prop="eutrCourName">
        <el-input v-model="queryParams.eutrCourName" placeholder="请输入课程名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="课程类型" prop="courType">
        <el-select v-model="queryParams.courType" placeholder="请选择课程类型" clearable style="width: 200px">
          <el-option v-for="dict in eutr_cour_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核结果" prop="veriResu">
        <el-select v-model="queryParams.veriResu" placeholder="请选择审核结果" clearable style="width: 200px">
          <el-option v-for="dict in eutr_cour_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="['eutr:objcour:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eutr:objcour:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eutr:objcour:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eutr:objcour:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eutr-cour-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="课程名称" align="center" prop="eutrCourName" v-if="columns.eutrCourName.visible" :show-overflow-tooltip="true" />
      <el-table-column label="培训内容" align="center" prop="trainCont" v-if="columns.trainCont.visible" :show-overflow-tooltip="true" />
      <el-table-column label="课时" align="center" prop="eutrCourHour" v-if="columns.eutrCourHour.visible" />
      <el-table-column label="课程类型" align="center" prop="courType" v-if="columns.courType.visible">
        <template #default="scope">
          <dict-tag :options="eutr_cour_type" :value="scope.row.courType" />
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="courApplPers" v-if="columns.courApplPers.visible" />
      <el-table-column label="提交时间" align="center" prop="subTime" v-if="columns.subTime.visible" width="180" />
      <el-table-column label="审核结果" align="center" prop="veriResu" v-if="columns.veriResu.visible">
        <template #default="scope">
          <dict-tag :options="eutr_cour_stat" :value="scope.row.veriResu" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['eutr:objcour:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['eutr:objcour: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="100px">
        <el-form-item label="课程名称" prop="eutrCourName">
          <el-input v-model="form.eutrCourName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="课程编码" prop="eutrCourCode">
          <el-input v-model="form.eutrCourCode" placeholder="请输入课程编码" />
        </el-form-item>
        <el-form-item label="课程类别" prop="cateGuid">
          <el-input v-model="form.cateGuid" placeholder="请输入课程类别guid" />
        </el-form-item>
        <el-form-item label="培训内容" prop="trainCont">
          <el-input v-model="form.trainCont" type="textarea" placeholder="请输入培训内容" />
        </el-form-item>
        <el-form-item label="课时" prop="eutrCourHour">
          <el-input v-model="form.eutrCourHour" placeholder="请输入课时" />
        </el-form-item>
        <el-form-item label="课程类型" prop="courType">
          <el-select v-model="form.courType" placeholder="请选择">
            <el-option v-for="dict in eutr_cour_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请人" prop="courApplPers">
          <el-input v-model="form.courApplPers" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="提交时间" prop="subTime">
          <el-date-picker v-model="form.subTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择日期" />
        </el-form-item>
        <el-form-item label="提交机构" prop="subOrgGuid">
          <el-input v-model="form.subOrgGuid" placeholder="请输入机构guid" />
        </el-form-item>
        <el-form-item label="审核机构" prop="veriOrgGuid">
          <el-input v-model="form.veriOrgGuid" placeholder="请输入审核机构guid" />
        </el-form-item>
        <el-form-item label="审核结果" prop="veriResu">
          <el-select v-model="form.veriResu" placeholder="请选择">
            <el-option v-for="dict in eutr_cour_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="EutrCour">
import { listObjCour, getObjCour, addObjCour, updateObjCour, delObjCour } from "@/api/slaj/eutr";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const { eutr_cour_type, eutr_cour_stat } = proxy.useDict("eutr_cour_type", "eutr_cour_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 dateRange = ref([]);

const columns = ref({
  eutrCourName: { visible: true },
  trainCont: { visible: true },
  eutrCourHour: { visible: true },
  courType: { visible: true },
  courApplPers: { visible: true },
  subTime: { visible: true },
  veriResu: { visible: true }
});

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, eutrCourName: null, courType: null, veriResu: null },
  rules: {
    eutrCourName: [{ required: true, message: "课程名称不能为空", trigger: "blur" }]
  }
});

const { form, queryParams, rules } = toRefs(data);

function getList() {
  loading.value = true;
  listObjCour(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

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

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加培训课程";
}

function handleUpdate(row) {
  reset();
  const guid = row.guid || ids.value[0];
  getObjCour(guid).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改培训课程";
  });
}

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

function handleDelete(row) {
  const guids = row.guid || ids.value.join(",");
  proxy.$modal.confirm('是否确认删除所选数据？').then(function() {
    return delObjCour(guids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  });
}

function handleExport() {
  proxy.download('/eutr/objcour/export', { ...queryParams.value }, `培训课程_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = {
    guid: null, eutrCourName: null, eutrCourCode: null, cateGuid: null, trainCont: null, eutrCourHour: null,
    courType: null, courApplPers: null, subTime: null, subOrgGuid: null, veriOrgGuid: null, veriResu: null, note: null
  };
  proxy.resetForm("formRef");
}

getList();
</script>
