<template>
  <div>
    <el-dialog
      v-if="type === 104"
      :model-value="type === 104"
      :title="title"
      :before-close="fnClose"
    >
      <el-space wrap>
        <el-button type="primary" @click="fnAdd">新增</el-button>
        <el-button type="danger" @click="fnDelete"> 删除 </el-button>
        <el-button type="primary" v-print="'#printContent'">打印</el-button>
      </el-space>
      <div id="printContent">
        <div style="text-align: center">
          <el-text size="large">{{ year }}年度安全培训教育管理台账</el-text>
        </div>
        <div>
          <el-text size="small">单位名称:{{ CORP_NAME }}</el-text>
        </div>
        <layout-table
          :data="data.dataList"
          :show-pagination="false"
          highlight-current-row
          @row-click="fnCurrentChange"
          @row-dblclick="fnAddOrUpdate"
        >
          <el-table-column label="序号" width="45">
            <template v-slot="{ $index }">
              {{ serialNumber({ currentPage: 1, pageSize: 99999 }, $index) }}
            </template>
          </el-table-column>
          <el-table-column prop="TRAINING_DATE" label="培训时间" />
          <el-table-column prop="TRAINING_OBJECT" label="培训对象" />
          <el-table-column prop="TRAINING_TYPE" label="培训类型" />
          <el-table-column
            show-overflow-tooltip
            prop="TRAINING_CONTENT"
            label="培训内容"
          />
          <el-table-column prop="PERSON_NUMBER" label="参加人数" />
          <el-table-column prop="CLASS_HOURS" label="培训学时" />
          <el-table-column
            show-overflow-tooltip
            prop="TRAINING_PLACE"
            label="培训地点"
          />
          <el-table-column prop="TRAINING_TEACHER" label="培训教师" />
          <el-table-column prop="ASSESSMENT_METHOD" label="考核方式" />
          <el-table-column
            show-overflow-tooltip
            prop="ASSESSMENT"
            label="汇总考核情况"
          />
        </layout-table>
        <el-row>
          <el-col :span="6">填表人:</el-col>
          <el-col :span="6"></el-col>
          <el-col :span="6"></el-col>
          <el-col :span="6">填表日期:</el-col>
        </el-row>
      </div>
      <template #footer>
        <el-button @click="fnClose">关闭</el-button>
      </template>
    </el-dialog>
    <edumanager-form
      v-if="data.formVisible"
      v-model:visible="data.formVisible"
      :ARCHIVESEDUMANAGER_ID="data.current.ARCHIVESEDUMANAGER_ID"
      :type="data.current.ARCHIVESEDUMANAGER_ID ? 'edit' : 'add'"
      :year="year"
      @get-data="fnGetList"
    />
  </div>
</template>

<script setup>
import {
  deleteArchivesEdumanager,
  getArchivesEdumanagerList,
} from "@/request/archives.js";
import { reactive } from "vue";
import { useVModels } from "@vueuse/core";
import { useUserStore } from "@/pinia/user.js";
import LayoutTable from "@/components/table/index.vue";
import { serialNumber } from "@/assets/js/utils.js";
import EdumanagerForm from "./edumanager_form.vue";
import { ElMessageBox } from "element-plus";

const userStore = useUserStore();
const CORP_NAME = userStore.getUserInfo.CORP_NAME;

const props = defineProps({
  title: {
    type: String,
    required: false,
    default: "",
  },
  year: {
    type: String,
    required: false,
    default: "",
  },
  type: {
    type: Number,
    required: true,
  },
  studyTaskId: {
    type: String,
    required: false,
    default: "",
  },
});
const { title, year, type } = useVModels(props);

const data = reactive({
  dataList: [],
  current: undefined,
  formVisible: false,
});

const emits = defineEmits(["update:visible"]);

const fnClose = () => {
  emits("update:current", undefined);
};

const fnCurrentChange = (row) => {
  data.current = row;
};

const fnAdd = () => {
  data.dataList.push({});
};

const fnDelete = async () => {
  if (data.current) {
    if (data.current.ARCHIVESEDUMANAGER_ID) {
      await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
      await deleteArchivesEdumanager({
        ARCHIVESEDUMANAGER_ID: data.current.ARCHIVESEDUMANAGER_ID,
      });
    }
    data.current = undefined;
    await fnGetList();
  }
};

const fnGetList = async () => {
  const respData = await getArchivesEdumanagerList({ YEAR: year.value });
  if (respData && respData.varList) {
    data.dataList = respData.varList;
  }
};
await fnGetList();

const fnAddOrUpdate = (row) => {
  data.current = row;
  if (row) {
    data.formVisible = true;
  }
};
</script>

<style scoped lang="scss"></style>
