<template>
  <div class="course-analysis-wrap">
    <div class="box-list">
      <div v-for="(item , index) in cardData" :key="index" class="box">
        <el-card class="box-card" v-bind:class="{ 'box-last': index == 2 }">
          <!-- <div class="box-center"> -->
          <p class="value">{{ item.value }}</p>
          <p class="title">{{ item.title }}</p>
          <!-- </div> -->
        </el-card>
      </div>
    </div>
    <div class="spacing"></div>
    <!-- <div class="chart-title">考试列表</div> -->
    <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 | 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="checkName(scope.row)" type="text" size="small">查看名单</el-button>
        </template>
      </el-table-column>
      <div slot="empty">
        <div class="table-empty">
          <img src="../../assets/image/no-content1.png" />
          <p>没有相关数据，请重新选择查询范围</p>
        </div>
      </div>
    </el-table>
    <div class="pagination">
      <el-pagination
        class="pagination"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNo"
        :page-sizes="[20, 50 ,100]"
        :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
    }
  },
  data() {
    return {
      cardData: [],
      tableData: [],
      totalRows: 0,
      projectId: "",
      pageNo: 1,
      pageSize: 20,
      second: 0
    };
  },
  created() {
    vm = this;
    this.projectId = vm.getUrlSearch(window.location.href, "id");
  },
  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 = [];
    });
  },
  methods: {
    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[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 ? 0 : 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);
        }
      });
      // let reqList = req;
      // reqList.pageNo = vm.pageNo;
      // reqList.pageSize = vm.pageSize;
      // vm.reportGET("report/portalProjectCourse/getCourseList", reqList).then(res => {
      //   closeLoading(vm);
      //   if (res.code == "000000") {
      //     vm.tableData = res.data.list;
      //   }
      // });
    },
    checkName(data) {
      console.log("dialogData", this.dialogData);
      let checkAll = operationData.hasAll(vm.formInline.organization);
      let routerData = {};
      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 ? 0 : 1;
      routerData.timeFlag = vm.formInline.timeFlag;
      routerData.hospitalIdList = vm.formInline.organization;

      // 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.search();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      vm.pageNo = val;
      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;
  }
}
</style>