<template>
  <div class="examination-analysis">
    <div v-for="(item , index) in cardData" :key="index" class="box">
      <el-card class="box-card">
        <div slot="header" class="title">
          <span class="weight">{{ item.title }}</span>
        </div>
        <div class="box-content">
          <span class="weight">{{ item.num }}</span>
          {{ item.unit }}
        </div>
      </el-card>
      <div v-if="index+1 !== cardData.length" class="line"></div>
    </div>
    <div class="table">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="examName" label="考试名" min-width="150" align="center"></el-table-column>
        <el-table-column prop="examUserCount" label="参与考试人数" min-width="100" align="center"></el-table-column>
        <el-table-column prop="userCountForPassExam" label="通过考试人数" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="showTd[0]" prop="userCountList[0]" :label="labelTd[0]" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="showTd[1]" prop="userCountList[1]" :label="labelTd[1]" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="showTd[2]" prop="userCountList[2]" :label="labelTd[2]" min-width="80" align="center"></el-table-column>
        <el-table-column v-if="showTd[3]" prop="userCountList[3]" :label="labelTd[3]" min-width="80" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" align="center" min-width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="goPage(scope.row)">查看名单</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>
    <!-- 分页 -->
    <div class="pagination">
      <el-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 { log } from 'util';
import * as operationData from "../../utils-o/operation";
import { openLoading, closeLoading } from "../../utils-o/utils";
import * as commonUtil from "../../utils-o/utils";
let vm
export default {
  props: {
    formInline: {
      type: Object
    },
    activeName: {
      type: String
    },
    dialogData: {
      type: Object
    },
    organizationList: {
      type: Array
    }
  },
  data() {
    return {
      projectId: '',
      tableData: [],
      cardData: [],
      totalRows: 0, //数据总数
      pageNo: 1, //当前是第几页
      pageSize: 20, //页面总数
      labelTd: [],
      showTd: [false,false,false,false],
    };
  },
  created() {
    vm = this;
    this.projectId = vm.getUrlSearch(window.location.href, "id");
  },
   // 挂载到Dom完成时
  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.projectExamCount;
      vm.cardData = [
        {
          title: "项目所有考试数",
          num: data.projectExamCount,
          unit: "门考试"
        },
        {
          title: "项目所有考题数",
          num: data.projectExamQuestionCount,
          unit: "道题"
        }
      ];
      // vm.cardData[0].num = data.projectExamCount;
      // vm.cardData[1].num = data.projectExamQuestionCount;
    },
    // 搜索列表
    search() {
      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,
        ids: operationData.getIds(vm.formInline,vm.organizationList,checkAll),
        type: operationData.getSearchType(vm.formInline,checkAll),
        originalFlag: vm.formInline.checked == false ? 0 : 1
      };
      console.log("4 req", req);
      openLoading(vm);
      vm.reportGET("report/portalProjectExam/getTotal", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          if(res.data.projectExamCount == 0 ) {
            vm.$message("没有相关数据，请重新选择查询范围");
          }
          vm.setCardData(res.data);
          vm.labelTd = res.data.gradeNameList;
        } else {
          vm.$message(res.message);
        }
      });
      let reqList = req;
      reqList.pageNo = vm.pageNo;
      reqList.pageSize = vm.pageSize;
      vm.reportGET("report/portalProjectExam/getExamList", req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          vm.tableData = res.data.list;
          if(res.data.list.length == 0) {
            return;
          }
          if(res.data.list[0].userCountList == null || res.data.list[0].userCountList.length == 0) {
            vm.showTd = [false,false,false,false];
          } else if (res.data.list[0].userCountList.length == 1){
            vm.showTd = [true,false,false,false];
          } else if (res.data.list[0].userCountList.length == 2){
            vm.showTd = [true,true,false,false];
          } else if (res.data.list[0].userCountList.length == 3){
            vm.showTd = [true,true,true,false];
          } else if (res.data.list[0].userCountList.length == 4){
            vm.showTd = [true,true,true,true];
          }
        }
      });

      //
    },
    // 跳转查看名单页面
    goPage(data) {
      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 = 2;
      routerData.examId = data.examId;
      routerData.ids = operationData.getIds(vm.formInline,vm.organizationList,checkAll);
      routerData.type = operationData.getSearchType(vm.formInline,checkAll),
      routerData.originalFlag = vm.formInline.checked == false ? 0 : 1
     
      // this.$router.push({ path: '/name-list-old', query: routerData});
      let routeData = this.$router.resolve({ path: '/name-list-old', 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' rel='stylesheet/scss'>
.examination-analysis {
  background: #fff;
  overflow: hidden;
  padding: 10px;
  .weight {
    font-weight: bold;
  }
  .el-card {
    border-width: 0px;
  }
  .el-card.is-always-shadow {
    box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.1);
  }
  .line {
    background: #e4e4e4;
    height: 80px;
    width: 1px;
    float: left;
    margin-top: 20px;
  }
  .box {
  }
  .table {
    margin-top: 30px;
  }
  .box-card {
    width: 48%;
    margin-right: 1%;
    float: left;
    .el-card__header {
      border-bottom-width: 0;
    }
    .title {
      text-align: center;
    }
    .box-content {
      text-align: center;
      height: 40px;
      font-size: 15px;
    }
  }
}
</style>
