<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="courGuid">
        <el-input v-model="queryParams.courGuid" placeholder="请输入课程guid" clearable style="width: 200px" @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="['eutr:courrec:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['eutr:courrec:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['eutr:courrec:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['eutr:courrec:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="eutr-courrec-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" align="center" prop="courGuid" v-if="columns.courGuid.visible" :show-overflow-tooltip="true" />
      <el-table-column label="学习开始时间" align="center" prop="studyStartTime" v-if="columns.studyStartTime.visible" width="180" />
      <el-table-column label="学习完成时间" align="center" prop="studyCompTime" v-if="columns.studyCompTime.visible" width="180" />
      <el-table-column label="累计时间" align="center" prop="eutrTime" v-if="columns.eutrTime.visible" />
      <el-table-column label="学时" align="center" prop="eutrHuor" v-if="columns.eutrHuor.visible" />
      <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:courrec:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['eutr:courrec: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="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="课程guid" prop="courGuid">
          <el-input v-model="form.courGuid" placeholder="请输入课程guid" />
        </el-form-item>
        <el-form-item label="人员guid" prop="persGuid">
          <el-input v-model="form.persGuid" placeholder="请输入人员guid" />
        </el-form-item>
        <el-form-item label="学习开始时间" prop="studyStartTime">
          <el-date-picker v-model="form.studyStartTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="学习完成时间" prop="studyCompTime">
          <el-date-picker v-model="form.studyCompTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="累计时间" prop="eutrTime">
          <el-input v-model="form.eutrTime" placeholder="请输入累计时间" />
        </el-form-item>
        <el-form-item label="学时" prop="eutrHuor">
          <el-input v-model="form.eutrHuor" 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="EutrCourRec">
import { listCourRec, getCourRec, addCourRec, updateCourRec, delCourRec } from "@/api/slaj/eutr";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
// No dicts needed for this entity

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({
  courGuid: { visible: true },
  studyStartTime: { visible: true },
  studyCompTime: { visible: true },
  eutrTime: { visible: true },
  eutrHuor: { visible: true }
});

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, courGuid: null },
  rules: {}
});

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

function getList() {
  loading.value = true;
  listCourRec(queryParams.value).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleQuery() { queryParams.value.pageNum = 1; getList(); }
function resetQuery() { 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();
  getCourRec(row.guid || ids.value[0]).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) {
        updateCourRec(form.value).then(() => { proxy.$modal.msgSuccess("修改成功"); open.value = false; getList(); });
      } else {
        addCourRec(form.value).then(() => { proxy.$modal.msgSuccess("新增成功"); open.value = false; getList(); });
      }
    }
  });
}

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

function handleExport() { proxy.download('/eutr/courrec/export', { ...queryParams.value }, `参与记录_${new Date().getTime()}.xlsx`); }
function cancel() { open.value = false; reset(); }
function reset() {
  form.value = { guid: null, courGuid: null, persGuid: null, studyStartTime: null, studyCompTime: null, eutrTime: null, eutrHuor: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
