<template>
  <div style="margin: 10px">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px">
      <el-form-item label="培训部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" style="width: 220px;" :options="deptOptions" :show-count="true" placeholder="请选择培训部门" />
      </el-form-item>
      <el-form-item label="培训名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入培训名称"
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="发布时间" prop="releaseTime">
        <el-date-picker
          v-model="releaseTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateFormat">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="search"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
        >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 培训统计列表 -->
    <el-table v-loading="loading" :data="trainCourseStatData">
      <el-table-column label="序号" width='100' align="center" prop="courseNum"/>
      <el-table-column label="培训部门" align="center" prop="deptName" width="120"/>
      <el-table-column label="课程标题" align="center" prop="courseName"/>
      <el-table-column label="培训计划" align="center" prop="planName"/>
      <el-table-column label="应参加人数" align="center" prop="maybeTestNum" width="100"/>
      <el-table-column label="实际完成人数" align="center" prop="finishNum" width="100"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="{ row: { courseId, courseName }}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="trainCourseStatDetail(courseId,courseName)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 列表分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getTrainStatList"
    />

    <!-- 详情 -->
    <el-dialog :title="'培训详情：'+courseName" :visible.sync="trainStatDetailOpen" append-to-body :close-on-click-modal="false">

      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        :loading="exportLoading"
        @click="handleExport"
        style="position: relative;left: 16px;top: -17px;"
      >导出</el-button>

      <el-table v-loading="loading" :data="trainStatDetailData">
        <el-table-column label="序号" width='100' align="center" prop="detailNum"/>
        <el-table-column label="培训人员" align="center" prop="nickName"/>
        <el-table-column label="所属部门" align="center" prop="deptName"/>
        <el-table-column label="培训时长" align="center" prop="finishDuration"/>
        <el-table-column label="是否完成" align="center" prop="isComplete"/>
      </el-table>

      <pagination
        v-show="totalDetail > 0"
        :total="totalDetail"
        :page.sync="queryDetailParams.pageNum"
        :limit.sync="queryDetailParams.pageSize"
        @pagination="getTrainStatDetail"
      />
    </el-dialog>

  </div>
</template>

<script>
import {getTrainCourseStatData,getTrainCourseStatDetailData,trainCourseStatDetailExport} from "@/api/train/trainCourseStat";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
export default {
  name: "trainCourseStat",
  components: {
    Treeselect
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName:"",
        releaseBeginTime: "",
        releaseEndTime: ""
      },
      releaseTime: "",
      trainCourseStatData: [],
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      totalDetail: 0,
      queryDetailParams:{
        pageNum: 1,
        pageSize: 10,
        courseId: ""
      },
      courseName: "",
      trainStatDetailOpen: false,
      trainStatDetailData: [],
      exportLoading: false,
      // 部门树选项
      deptOptions: undefined,
    }
  },
  created() {
    this.getTrainStatList();
    this.getTreeselect();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        console.log('部门树返回值')
        this.deptOptions = response.data;
      });
    },
    dateFormat(picker){
      this.queryParams.releaseBeginTime = picker[0];
      this.queryParams.releaseEndTime = picker[1];
    },
    search(){
      this.getTrainStatList();
    },
    resetClick(){
      this.releaseTime = "";
      this.queryParams.releaseBeginTime = null;
      this.queryParams.releaseEndTime = null;
      this.queryParams={
        pageNum: 1,
        pageSize: 10,
        courseName:"",
        releaseBeginTime: "",
        releaseEndTime: ""
      };
      this.getTrainStatList();
    },
    getTrainStatList(){
      getTrainCourseStatData(this.queryParams).then(res =>{
        this.trainCourseStatData = res.rows.map((item, index) => {
            return {
                courseNum:
                index +
                1 +
                (this.queryParams.pageNum - 1) * this.queryParams.pageSize,
                ...item,
            };
        });
        this.total = res.total;
      })
    },
    trainCourseStatDetail(courseId,courseName){
      this.queryDetailParams.courseId = courseId;
      this.courseName = courseName;
      this.queryDetailParams.pageNum = 1;
      this.queryDetailParams.pageSize = 10;
      this.getTrainStatDetail();
      this.trainStatDetailOpen = true;
    },
    getTrainStatDetail(){
      getTrainCourseStatDetailData(this.queryDetailParams).then(res =>{
          this.trainStatDetailData = res.rows.map((item, index) => {
          return {
                detailNum:
                index +
                1 +
                (this.queryDetailParams.pageNum - 1) * this.queryDetailParams.pageSize,
                ...item,
            };
          });
        this.totalDetail = res.total;
      })
    },
    handleExport(){
      this.$confirm('是否确认导出所有培训详细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
         return trainCourseStatDetailExport({"courseId":this.queryDetailParams.courseId});
        }).then(response => {
            this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>

</style>
