<template>
  <div class="course-analysis-wrap">
    <div class="box-list">
      <div v-for="(item , index) in cardData" :key="index" class="box-card" v-bind:class="{ 'box-last': index == 2 }">
        <!-- <el-card class="box-card" v-bind:class="{ 'box-last': index == 2 }"> -->
          <p class="value">{{ item.value }}</p>
          <p class="title">{{ item.title }}</p>
        <!-- </el-card> -->
      </div>
    </div>
    <div class="spacing"></div>
    <!-- <div class="chart-title">考试列表</div> -->
    <div v-if="downloadId == null">
      <el-table :data="tableData" class="course-table">
        <el-table-column prop="courseName" label="课程名" align="center"></el-table-column>
        <el-table-column prop="courseTime" label="课程时长" align="center">
          <template slot-scope="scope">{{ scope.row.courseTime | getAvgTime }}</template>
        </el-table-column>
        <el-table-column prop="averageTime" label="人均学习时长" align="center">
          <template slot-scope="scope">{{ scope.row.averageTime | getAvgTime }}</template>
        </el-table-column>
        <el-table-column prop="joinCount" label="参与培训人数" align="center"></el-table-column>
        <el-table-column prop="finishCount" label="完成培训人数" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" min-width="50" align="center">
          <template slot-scope="scope">
            <el-button class="button-jump" @click="goPage(scope.row)" type="text" size="small">查看名单</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <div class="table-empty">
            <img src="../../assets/image/noData.png" />
          </div>
        </div>
      </el-table>
    </div>
    <div v-if="downloadId != null">
      <el-table :data="tableData.slice((pageNo-1)*pageSize,pageNo*pageSize)" class="course-table">
        <el-table-column prop="courseName" label="课程名" align="center"></el-table-column>
        <el-table-column prop="courseTime" label="课程时长" align="center">
          <template slot-scope="scope">{{ scope.row.courseTime | getTotalTime }}</template>
        </el-table-column>
        <el-table-column prop="averageTime" label="人均学习时长" align="center">
          <template slot-scope="scope">{{ scope.row.averageTime | getAvgTime }}</template>
        </el-table-column>
        <el-table-column prop="joinCount" label="参与培训人数" align="center"></el-table-column>
        <el-table-column prop="finishCount" label="完成培训人数" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" min-width="50" align="center">
          <template slot-scope="scope">
            <el-button class="button-jump" @click="goPage(scope.row)" type="text" size="small">查看名单</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <div class="table-empty">
            <img src="../../assets/image/noData.png" />
          </div>
        </div>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        class="pagination"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="[20, 50 ,100, 200]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalRows"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { create } from "domain";
import * as operationData from "../../utils/operation";
import { openLoading, closeLoading } from "../../utils/utils";
let vm = null;
export default {
  props: {
    formInline: {
      type: Object
    },
    activeName: {
      type: String
    },
    dialogData: {
      type: Object
    },
    organizationList: {
      type: Array
    },
    timeInfoData: {
      type: Object
    },
  },
  data() {
    return {
      cardData: [],
      tableData: [],
      totalRows: 0,
      projectId: "",
      pageNo: 1,
      pageSize: 20,
      timeInfoChild: {},
      downloadId: null,
    };
  },
   watch: {
    timeInfoData: {
      deep: true,
      handler(nv, ov) {
        vm.timeInfoChild = nv;
      }
    }
  },
  created() {
    vm = this;
    vm.projectId = vm.getUrlSearch(window.location.href, "id");
    vm.downloadId = vm.getUrlSearch(window.location.href, "downloadId");
  },
  mounted: function() {
    this.$on("search", () => {
      // console.log(this.formInline);
      this.pageNo = 1;
      this.search();
    });
    this.$on("init", () => {
      this.$nextTick(function() {
        if (vm.formInline.region.length != 0) {
          this.search();
        }
      });
    });
    this.$on("reset", () => {
      this.pageNo = 1;
      this.pageSize = 20;
      this.tableData = [];
      this.cardData = [];
    });
    this.$on("static", () => {
      vm.getStatic();
    });
  },
  methods: {
    getStatic() {
      // console.log(vm.timeInfoChild);
      let courseAnalysisModel = vm.timeInfoChild.courseAnalysisModel;
      vm.setCardData(courseAnalysisModel);
      vm.tableData = courseAnalysisModel.pCourseList;
      vm.totalRows = courseAnalysisModel.totalSize;
    },
    setCardData(data) {
      vm.totalRows = data.projectCourseCount;
      let cardData = [
        {
          title: "项目所有课程数",
          value: "0门"
        },
        {
          title: "项目所有课程总时长",
          value: "0分钟"
        },
        {
          title: "项目人均学习时长",
          value: "0分钟"
        }
      ];
      cardData[0].value = data.totalSize+'门';
      // cardData[1].value = data.pCourseTotalTime+"分钟";
      cardData[1].value = operationData.getAvgTime(data.pCourseTotalTime);
      cardData[2].value = operationData.getAvgTime(data.pCourseAvg);
      vm.cardData = cardData;
    },
    search(type) {
      // if (vm.formInline.region.length == 0) {
      //   const h = this.$createElement;
      //   vm.$message({
      //     message: h(
      //       "p",
      //       { style: "color: #FF3399" },
      //       "请先选择地区后再进行查询 "
      //     )
      //   });
      //   return;
      // }
      let checkAll = operationData.hasAll(vm.formInline.organization);
      let req = {
        projectId: vm.projectId,
        beginDate: operationData.setDuringTime(vm.formInline.during, "begin"),
        endDate: operationData.setDuringTime(vm.formInline.during, "end"),
        hospitalIdList: vm.formInline.organization,
        originalFlag: vm.formInline.checked == false ? 2 : 1,
        regionId: vm.formInline.region[vm.formInline.region.length - 1],
        timeFlag: vm.formInline.timeFlag,
        pageNo: vm.pageNo,
        pageSize: vm.pageSize,
      };
      // console.log("3 req", req);
      openLoading(vm);
      vm.POST("stats/report/course", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          if (type !== 2) {
            vm.setCardData(res.data);
          }
          vm.tableData = res.data.pCourseList;
          vm.totalRows = res.data.totalSize;
        } else {
          // vm.$message(res.message);
        }
      });
    },
    goPage(data) {
      let routerData = {};
      if(vm.downloadId == null) {
        // console.log("dialogData", this.dialogData);
        routerData = this.dialogData;
        routerData.projectId = vm.projectId;
        routerData.projectName = vm.getUrlSearch(
          window.location.href,
          "projectName"
        );
        routerData.tableType = 1;
        routerData.courseId = data.courseId;
        routerData.beginDate = operationData.setDuringTime(vm.formInline.during, "begin"),
        routerData.endDate = operationData.setDuringTime(vm.formInline.during, "end"),
        routerData.regionId = vm.formInline.region[vm.formInline.region.length - 1];
        routerData.originalFlag = vm.formInline.checked == false ? 2 : 1;
        routerData.timeFlag = vm.formInline.timeFlag;
        routerData.hospitalIdList = operationData.setHospitalIdList(vm.formInline.organization);
      } else {
        let reportParamModel = vm.timeInfoChild.reportParamModel;
        routerData.base = 'static';
        routerData.region = reportParamModel.regionNames;
        routerData.organization = reportParamModel.hospitalNames;
        routerData.projectId = reportParamModel.projectId,
        routerData.projectName = reportParamModel.projectName,
        routerData.tableType = 1;
        routerData.courseId = data.courseId;
        routerData.beginDate = reportParamModel.beginDate;
        routerData.endDate = reportParamModel.endDate;
        routerData.regionId = reportParamModel.regionId;
        routerData.originalFlag = reportParamModel.originalFlag;
        routerData.timeFlag = reportParamModel.timeFlag;
        routerData.hospitalIdList = operationData.setHospitalIdList(reportParamModel.hospitalIdList);
      }

      // this.$router.push({ path: '/name-list', query: routerData});
      let routeData = this.$router.resolve({
        path: "/name-list",
        query: routerData
      });
      window.open(routeData.href, "_blank");
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      vm.pageSize = val;
      vm.pageNo = 1;
      if(vm.downloadId == null) {
        vm.search();
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      vm.pageNo = val;
      if(vm.downloadId == null) {
        vm.search();
      }
    }
  }
};
</script>
<style lang="scss">
.course-analysis-wrap {
  background: #fff;
  overflow: hidden;
  .spacing {
    position: relative;
    width: 100%;
    height: 20px;
    background: #f0f2f5;
  }
  .chart-title {
    position: relative;
    margin-top: 17px;
    margin-left: 30px;
    width: 100%;
    height: 60px;
    font-size: 18px;
    font-weight: 600;
    line-height: 60px;
    color: #000000;
  }
  .box-list {
    position: relative;
    height: 120px;
    background: #f0f2f5;
    .box-card {
      position: relative;
      background-color: #ffffff;
      width: 32%;
      margin-right: 2%;
      height: 120px;
      float: left;
      .el-card__header {
        border-bottom-width: 0;
      }
      .el-card__body {
        padding: 0px;
      }
      .value {
        color: #373839;
        text-align: center;
        margin-top: 24px;
        margin-bottom: 8px;
        height: 45px;
        line-height: 45px;
        font-size: 32px;
        font-weight: 500;
      }
      .title {
        color: #666666;
        text-align: center;
        font-size: 14px;
        height: 20px;
        line-height: 20px;
      }
    }
    .box-last {
      margin-right: 0%;
    }
  }
  .course-table {
    top: 20px;
    padding: 0;
    width: 95%;
    margin:0 auto 20px auto;
    .button-jump {
      color: #449284;
    }
  }
  .pagination {
    margin-top: 30px;
    margin-right: 13px;
  }
  .table-empty {
    img {
      height: 24px;
      width: 106px;
    }
  }
}
</style>