<template>
  <div>
    <MOSTY.PageTree :pageTitle="'单位培训档案列表'" :searchType="'01'" @getDeptId="getdeptId">
      <template #serach>
        <el-divider />
        <MOSTY.Search :searchArr="searchArr" @submit="onSearch" />
        <el-divider />
        <section class="button">
          <el-button type="primary" @click="export2Excel" v-loading="loadingShow">
            <el-icon>
              <Download />
            </el-icon>
            <span>导出表格</span>
          </el-button>
        </section>
      </template>
      <template #table>
        <section class="eltab9">
          <MOSTY.Table :table-columns="tableColumn" :tableData="tableData" :tableloading="tableloading"
            :isSelect="false" style="width: 100%;">
            <template #otherType="{ row }">
              <sapn v-if="!row.otherType">无</sapn>
              <dict-tag :options="GZ_TRAINOT" :value="row.otherType" :tag="false" />
            </template>
            <template #type="{ row }">
              <sapn>培训</sapn>
            </template>
            <template #examinationType="{ row }">
              <sapn v-if="!row.examinationType">无</sapn>
              <dict-tag :options="D_TB_PXKHFS" :value="row.examinationType" :tag="false" />
            </template>
            <template #actions="{ row }">
              <span class="operesee" @click="handleDetail(row)" v-if="row.pzlx != '0'"><el-icon>
                  <View />
                </el-icon>详情</span>
            </template>
          </MOSTY.Table>
        </section>
        <div class="fenye">
          <MOSTY.Pages :pageConfiger="pageConfiger" @changeSize="handleSizeChange" @changeNo="handleCurrentChange">
          </MOSTY.Pages>
        </div>
      </template>
    </MOSTY.PageTree>


    <!-- 详情 -->
    <div v-if="addDiaShowxq" class="dialog-box">
      <pxssxqdia :formData="formObj" :type="types" @changeAddVi="addDiaShowxq = false" from="dwpxda">
      </pxssxqdia>
    </div>

  </div>
</template>

<script setup>
// 引入
import { reactive, onMounted, watch, ref, getCurrentInstance } from "vue";
import { exportXlsx } from "@/excel/Export2Excel";
import * as MOSTY from "@/components/MyComponents/index";
import { getApi, deleteApi, putApi } from "@/api/tobAcco_api.js";
import { filterYear } from "@/utils/auth.js";
import { filterqx } from "@/utils/auth.js";
import pxssxqdia from "./pxssxqdia.vue";
import * as XLSX from "xlsx";
const { proxy } = getCurrentInstance();
// 数据
const { D_TB_ZSLB, GZ_TRAINOT, D_TB_PXKHFS, D_BZ_PXDX } = proxy.$dict(
  "D_TB_ZSLB",
  "GZ_TRAINOT",
  "D_TB_PXKHFS",
  "D_BZ_PXDX"
);
const searchArr = reactive([
  {
    showType: "select",
    prop: "year",
    width: '250px',
    defaultVal: new Date().getFullYear().toString(),
    options: filterYear(7),
  }
]);
const tableColumn = ref([
  {
    title: "培训项目",
    prop: "planName",
    sortable: true
  },
  {
    title: "培训类型",
    prop: "otherType",
    sortable: true,
    slot: true
  },
  {
    title: "培训方式",
    prop: "type",
    sortable: true,
    slot: true
  },
  {
    title: "培训人数",
    prop: "implementationTraineeNumber",
    sortable: true
  },
  {
    title: "培训课时(h)",
    prop: "realClassHour",
    sortable: true
  },
  {
    title: "培训内容",
    prop: "realContent",
    sortable: true
  },
  {
    title: "培训实施时间",
    prop: "planImplementationDate",
    sortable: true
  },
  {
    title: "考核方式",
    prop: "examinationType",
    sortable: true,
    slot: true
  },
  {
    title: "责任部门",
    prop: "planChargeDeptName",
    sortable: true
  },
  {
    title: "操作",
    prop: "actions",
    slot: true
  }
])
const pageConfiger = reactive({
  pageSize: 20,
  pageCurrent: 1,
  total: 0
})
const tableloading = ref(false);
const tableData = ref([]);
const loadingShow = ref(false);
// 分页
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20,
  year: new Date().getFullYear()
});
const formObj = ref({});
const addDiaShowxq = ref(false);
const orgLevel = ref(JSON.parse(window.localStorage.getItem("deptId")).orgLevel.substring(0, 1))
const types = ref("");
const params = ref({
  deptname: "",
  deptcode: "",
  parentid: ""
});
const total = ref(0);
const addDiaShow = ref(false);

function onSearch(obj) {
  listQuery.value.year = obj.year
  pageConfiger.pageCurrent = 1;
  listQuery.value.pageCurrent = 1;
  getTableData();
}
//车辆培训==计划外
const handleDetail = (row) => {
  types.value = row.type;
  var isTrue = false;
  //计划外没有三级培训
  getApi({ id: row.planHostsDeptId }, '/mosty-base/deptFeign/getOrgById').then(res => {
    formObj.value.orgLevel = res.orgLevel
    // 是否是安全部门
    if (filterqx('isaqbm') == '1') {
      if (res.orgLevel.substring(0, 1) == '1') {
        if (orgLevel.value == '1') {
          isTrue = true
        }
      } else if (res.orgLevel.substring(0, 1) == '2') {
        //当前登录部门orgLevel为1、2能看
        if (orgLevel.value == '1' || orgLevel.value == '2') {
          isTrue = true
        }
      } else if (res.orgLevel.substring(0, 1) == '3') {
        if (orgLevel.value == '1' || orgLevel.value == '2' || orgLevel.value == '3') {
          isTrue = true
        }
      }
    } else {
      isTrue = false
    }

    if (isTrue && row.otherType == "09") {
      formObj.value.sjtype = 'yj'
      formObj.value.title = '三级教育培训详情'
    } else {
      if (row.ej && row.otherType == "09") {
        //二级未实施
        formObj.value.sjtype = 'yj'
        formObj.value.title = '三级教育培训详情（一级培训）'
      } else if ((row.sj && row.otherType == "09") || row.oldejid) {
        //二级已实施，三级未实施
        formObj.value.sjtype = 'ej'
        formObj.value.title = '三级教育培训详情（二级培训）'
      } else if (row.oldsjid) {
        //三级已实施
        formObj.value.sjtype = 'sj'
        formObj.value.title = '三级教育培训详情（三级培训）'
      } else {
        formObj.value.sjtype = 'jc'
        formObj.value.title = '培训详情'
      }
    }
    formObj.value.id = row.id;
    formObj.value.state = row.state;
    formObj.value.oldejid = row.oldejid;
    formObj.value.type = row.type;
    addDiaShowxq.value = true
  })
};
//获取部门数据
const getdeptId = (val) => {
  listQuery.value.planHostsDeptId = val.id;
  pageConfiger.pageCurrent = 1;
  listQuery.value.pageCurrent = 1;
  getTableData();
};
onMounted(() => {

});

// 获取表格数据
const getTableData = () => {
  tableloading.value=true
  getApi(listQuery.value, "/mosty-jcgl/training/other/getDeptTrain").then(
    (res) => {
      tableData.value = res.records;
      tableData.value.forEach((item) => {
        if (item.planImplementationDate) {
          item.planImplementationDate = item.planImplementationDate.substring(
            0,
            10
          );
        }
        if (item.planTraineeType) {
          item.planTraineeType = item.planTraineeType.split(",");
        }
      });
      pageConfiger.total = res.total;
    }
  ).finally(() => {
    tableloading.value = false
  });
};

//页码发生变化
const handleCurrentChange = (val) => {
  pageConfiger.pageCurrent = val;
  listQuery.value.pageCurrent = val;
  getTableData();
};
//查询条数发生变化
const handleSizeChange = (val) => {
  pageConfiger.pageSize = val;
  listQuery.value.pageSize = val;
  getTableData();
};

//导出表格
const export2Excel = () => {
  loadingShow.value = true
  let title = {
    planName: "培训项目",
    otherType: "培训类型",
    types: "培训方式",
    planTraineeType: "培训对象",
    implementationTraineeNumber: "培训人数",
    realClassHour: "培训课时(h)",
    realContent: "培训内容",
    planImplementationDate: "计划举办时间",
    examinationType: "考核方式",
    planChargeDeptName: "责任部门",
  };
  const data = {
    pageSize: 1000,
    pageCurrent: 1,
    year: listQuery.value.year,
    planHostsDeptId: listQuery.value.planHostsDeptId
  }
  getApi(data, "/mosty-jcgl/training/other/getDeptTrain").then((res) => {
    if (res) {
      let data = res.records;
      if (data) {
        for (let i = 0; i < data.length; i++) {
          data[i].otherType = data[i].otherType ? proxy.$getDictVal(data[i].otherType, GZ_TRAINOT) : '无';
          data[i].examinationType = data[i].examinationType ? proxy.$getDictVal(data[i].examinationType, D_TB_PXKHFS) : '无';
          data[i].planTraineeType = data[i].planTraineeType ? proxy.$getDictVal(data[i].planTraineeType, D_BZ_PXDX) : '无';
          data[i].types = data[i].type == '1' ? "计划内" : '计划外';
        }
        let tablename = `${listQuery.value.year}年度培训情况统计.xlsx`;
        exportXlsx(title, data, `${listQuery.value.year}年度培训情况统计`, tablename);
      }
    }
  }).finally(() => {
    loadingShow.value = false
  })
};
</script>

<style lang="scss" scoped>
.button {
  display: flex;
  align-items: center;
  justify-content: end;
  margin-bottom: 12px;
}


</style>
