<!--指标统计列表-->
<template>
  <div class="checkIndexStatistics">
    <el-form :model="queryParams" class="form-box" ref="queryForm" :inline="true">
      <el-form-item label="" prop="deptId" label-width="50px" v-if="!userFlg">
        <el-select v-model="queryParams.deptId" clearable placeholder="全部">
          <el-option
            v-for="item in deptSelectList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="subId" label-width="50px" v-if="userFlg">
        <el-select placeholder="请输入关键词" filterable remote reserve-keyword
                   v-model="queryParams.subId" :remote-method="getSubList">
          <el-option
            v-for="item in subSelectList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="searchValue" label-width="80px">
        <el-input
          maxLength="30"
          v-model="queryParams.searchValue"
          placeholder="计划任务"
          clearable
          style="width: 200px"/>
      </el-form-item>
      <el-form-item label="" prop="dateRange" label-width="70px">
        <el-date-picker
          :clearable="false"
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery" style="margin-right:10px;">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
      <el-form-item style="float: right;" v-hasPermi="['safety:dataSub:export']">
        <el-dropdown @command="exportButton">
          <div class="form-dropdown-box">
            <img src="@/assets/ZDimages/personnelManagement/icon_jzgxx_sc.png">
            <p>导出</p>
            <img src="@/assets/ZDimages/personnelManagement/icon_jzggl_xljt.png">
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item style="border-bottom:1px solid #E0E0E0;margin:0 10px;color:#333;" :command="{command:1}">导出全部数据</el-dropdown-item>
            <el-dropdown-item style="margin:0 10px;color:#333;" :command="{command:2}">导出选中数据</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
    </el-form>
    <div class="num-data-box">
      共搜索到隐患总数 {{numData.sumTotal}}，一级指标隐患总数 {{numData.level1}}，占比 {{numData.percent1}}，二级指标隐患总数 {{numData.level2}}，占比 {{numData.percent2}}，三级指标隐患总数 {{numData.level3}}，占比 {{numData.percent3}}
    </div>
    <el-table  border :data="tableList" ref="multipleTable"
               :row-key="getRowKeys"
               @select="select" @select-all="selectAll">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="序号" align="center"  type="index" width="60" />
      <el-table-column label="计划任务" align="center" prop="title" show-overflow-tooltip/>
      <el-table-column v-if="!userFlg" label="学院" align="center" prop="collegeName" show-overflow-tooltip width="230"/>
      <el-table-column v-if="userFlg" label="实验室" align="center" prop="subjectName" show-overflow-tooltip width="230"/>
      <el-table-column label="隐患总数" align="center" prop="sumTotal" show-overflow-tooltip width="120"/>
      <el-table-column label="一级指标隐患数" align="center" prop="itemsTotal1" show-overflow-tooltip width="130"/>
      <el-table-column label="一级指标占比" align="center" prop="percent" show-overflow-tooltip width="130"/>
      <el-table-column label="二级指标隐患数" align="center" prop="itemsTotal2" show-overflow-tooltip width="130"/>
      <el-table-column label="二级指标占比" align="center" prop="percent2" show-overflow-tooltip width="130"/>
      <el-table-column label="三级指标隐患数" align="center" prop="itemsTotal3" show-overflow-tooltip width="130"/>
      <el-table-column label="三级指标占比" align="center" prop="percent3" show-overflow-tooltip width="130"/>
    </el-table>
    <pagination :page-sizes="[20, 30, 40, 50]"
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
  </div>
</template>

<script>
  import { listDepartments } from "@/api/system/dept";
  import { dataStatisticsItemsList,dataStatisticsItemsListTop,isSCollege,getloginUser,
           hiddenDataItemsSubList,hiddenDataItemsSubListTop,listSubAdmin } from "@/api/safetyCheck/index";
  export default {
    name: 'checkIndexStatistics',
    props:{
      propsData:{},
    },
    data(){
      return{
        deptSelectList:[],
        subSelectList:[],
        classTypeList:[],
        classifiedList:[],
        dateRange:[],
        queryParams:{},
        setChildren:"",
        numData:{},
        tableList:[{}],
        total:0,
        userFlg:false,
        deptId:"",
      }
    },
    created(){
    },
    mounted(){
      this.initializationInterface();
      this.getList();
    },
    methods:{
      initializationInterface(){
        this.$set(this,'queryParams',{
          pageNum:1,
          pageSize:20,
          searchValue:"",
        });
        //获取学院列表
        listDepartments().then(response => {
          this.deptSelectList = response.data;
        });
        getloginUser().then(response => {
          this.$set(this,'deptId',response.data.deptId)
          this.getSubList('');
        });
        isSCollege().then(response => {
          this.$set(this,'userFlg',response.data.userFlg);
          if(this.userFlg){
            this.$set(this.queryParams,'subId','')
          }else{
            this.$set(this.queryParams,'deptId','')
          }
          this.getList();
        })
      },
      //实验室搜索
      getSubList(val){
        console.log('val',val)
        listSubAdmin({deptId:this.deptId,name:val}).then(response => {
          this.subSelectList = response.data;
        });
      },
      //获取数据列表
      getList(){
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        let objTop = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.beginTime = this.dateRange[0];
          objTop.beginTime = this.dateRange[0];
        }else{
          obj.beginTime = "";
          objTop.beginTime = "";
        }
        if(this.dateRange[1]){
          obj.endTime = this.dateRange[1];
          objTop.endTime = this.dateRange[1];
        }else{
          obj.endTime = "";
          objTop.endTime = "";
        }
        delete objTop.pageNum
        delete objTop.pageSize
        if(this.userFlg){
          //院级
          hiddenDataItemsSubList(obj).then(response => {
            this.total = response.total;
            this.tableList = response.rows;
          });
          hiddenDataItemsSubListTop(objTop).then(response => {
            this.$set(this,'numData',response.data);
          });
        }else{
          //校级
          dataStatisticsItemsList(obj).then(response => {
            this.total = response.total;
            this.tableList = response.rows;
          });
          dataStatisticsItemsListTop(objTop).then(response => {
            this.$set(this,'numData',response.data);
          });
        }
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.$set(this.queryParams,'pageNum',1);
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$set(this,'dateRange',[]);
        this.$set(this,'queryParams',{
          pageNum:1,
          pageSize:20,
          searchValue:"",
        });
        if(this.userFlg){
          this.$set(this.queryParams,'subId','')
        }else{
          this.$set(this.queryParams,'deptId','')
        }
        this.handleQuery();
      },
      /** 导出按钮操作 */
      exportButton(item) {
        let self = this;
        if(item.command == 1){
          self.$confirm(`确认导出全部数据?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(async () => {

            if(self.userFlg){
              self.download('/zd-security/collegCheck/collegeItemsExport/', {}, '检查指标统计-.xlsx')
            }else{
              self.download('/zd-security/DataStatistics/itemsExport/', {}, '检查指标统计-.xlsx')
            }
          }).catch(() => {})
        }else if(item.command == 2){
          let list = self.$refs.multipleTable.selection;
          let ids = [];
          for(let i=0;i<list.length;i++){
            ids.push(list[i].id)
          }
          if(ids.length>0) {
            self.$confirm(`确认导出选中数据?`, "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(async () => {
              if(self.userFlg){
                self.download(`/zd-security/collegCheck/collegeItemsExport/?ids=`+ids,{}, '检查指标统计.xlsx')
                this.$refs.multipleTable.clearSelection();
              }else{
                self.download(`/zd-security/DataStatistics/itemsExport/?ids=`+ids,{}, '检查指标统计.xlsx')
                this.$refs.multipleTable.clearSelection();
              }
            }).catch(() => {})
          }else {
            this.msgError('请选择要导出的数据')
          }
        }
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.id
      },
      // 单选
      select (selection, row) {
        if (selection.some(el => { return row.id === el.id })) {
          if (row.children) {
            this.setChildren(row.children, true)
          }
        } else {
          if (row.children) {
            this.setChildren(row.children, false)
          }
        }
      },
      // 全选
      selectAll (selection) {
        const isSelect = selection.some(el => {
          const tableDataIds = this.tableData.map(j => j.id)
          return tableDataIds.includes(el.id)
        })
        const isCancel = !this.tableData.every(el => {
          const selectIds = selection.map(j => j.id)
          return selectIds.includes(el.id)
        })
        if (isSelect) {
          selection.map(el => {
            if (el.children) {
              this.setChildren(el.children, true)
            }
          })
        }
        if (isCancel) {
          this.tableData.map(el => {
            if (el.children) {
              this.setChildren(el.children, false)
            }
          })
        }
      },
    }
  }
</script>

<style scoped lang="scss">
  .checkIndexStatistics{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding:20px;
    .form-box{
      .form-dropdown-box{
        display: flex;
        margin:0;
        padding:0 10px;
        cursor: pointer;
        height:40px;
        img:nth-child(1){
          width:16px;
          height:16px;
          margin-top:12px;
        }
        p{
          width:47px;
          text-align: center;
          font-size:14px;
          margin:0;
          line-height:40px;
        }
        img:nth-child(3){
          width:10px;
          height:6px;
          margin-top:17px;
        }
      }
    }
    .num-data-box{
      background: rgba(1,131,250,0.2);
      color:#0183FA;
      font-size:14px;
      line-height:40px;
      padding:0 20px;
      border-radius:4px;
      margin-bottom:20px;
    }
  }
</style>
