<template>
  <div class="app-container rectificationReport">
    <div class="rectificationReport-page" v-if="pageType == 1">
      <div class="title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm" :inline="true" label-width="80px">
          <!--与我相关-->
          <div class="form-button-max-big-box-me" style="margin-left:10px;">
            <div class="form-button-big-box-me">
              <div :class="queryParams.myRelated==1?'checkDiv-me':''" @click="topRightClickType">
                <p class="text-p-me">与我有关{{correlationNum}}</p>
                <p class="el-icon-check icon-p-me" v-if="queryParams.myRelated==1"></p>
              </div>
            </div>
          </div>
          <el-form-item label="关键字" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="计划任务/实验室/房间号"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="学院" prop="deptId" label-width="50px">
            <el-select v-model="queryParams.deptId" clearable placeholder="学院" style="width: 150px">
              <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="dateRange" style="margin-left:10px;" 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:checkRectify:export']">
            <el-col :span="1.5">
              <p class="inquire-button-one"
                 style="width:100px;"
                 @click="allDownload"
              >批量下载</p>
            </el-col>
          </el-form-item>
        </el-form>
      </div>
      <div class="content-box">
        <el-table  border :data="tableList" ref="multipleTable" @selection-change="handleSelectionChange"  :row-key="getRowKeys">
          <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
          <el-table-column label="序号" align="center"  type="index" width="50" />
          <el-table-column label="计划任务" align="center" prop="title" show-overflow-tooltip/>
          <el-table-column label="学院" align="center" prop="collegeName" show-overflow-tooltip width="160"/>
          <el-table-column label="实验室" align="center" prop="subjectName" show-overflow-tooltip width="160"/>
          <el-table-column label="房间号" align="center" prop="roomNumber" show-overflow-tooltip width="80"/>
          <el-table-column label="楼栋" align="center" prop="buildName" show-overflow-tooltip width="100"/>
          <el-table-column label="隐患数" align="center" prop="hazardCount" show-overflow-tooltip width="80"/>
          <el-table-column label="计划周期" align="center" prop="cycleTime" show-overflow-tooltip width="200"/>
          <el-table-column label="检查者" align="center" prop="checkUser" show-overflow-tooltip width="80"/>
          <el-table-column label="检查时间" align="center" prop="checkTime" show-overflow-tooltip width="120"/>
          <el-table-column label="操作" align="center" prop="deptName" width="200">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p" @click="goPage(2,scope.row)">检查详情</p>
                <p class="table-button-p" @click="lookDocumentButton(1,scope.row)">查看</p>
                <p class="table-button-p" @click="downloadButton(scope.row)">下载</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </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>
    </div>
    <listPage v-if="pageType == 2" :propsListPageData="propsListPageData"></listPage>
    <lookDocumentDataDialog v-if="lookDocumentType" :propsLookDocumentData="propsLookDocumentData"></lookDocumentDataDialog>
  </div>
</template>

<script>
  import { listDepartments } from "@/api/system/dept";
  import { checkRectifyList,genReport,genReportZip } from '@/api/safetyCheck/index'
  import listPage from './listPage.vue'
  import lookDocumentDataDialog from '@/components/lookDocumentDialog/lookDocumentDataDialog.vue'
  export default {
    name: 'index',
    components: {
      listPage,
      lookDocumentDataDialog
    },
    data(){
      return{
        //子组件数据
        lookDocumentType:false,
        propsLookDocumentData:{},
        propsListPageData:{},
        pageType:1,
        deptSelectList:[],
        queryParams:{
          pageNum:1,
          pageSize:20,
          deptId:'',
          searchValue:'',
          myRelated:1,
        },
        dateRange:[],
        tableList:[],
        total:0,
        selectedNum:0,
        ids:[],
        correlationNum:'',
      }
    },
    created(){

    },
    mounted(){
      this.listDepartments();
      this.getList();
    },
    methods:{
      //获取相关数量
      getCorrelationNum(){
        checkRectifyList({
          pageNum:1,
          pageSize:20,
          myRelated:1,
          deptId:this.queryParams.deptId,
          searchValue:this.queryParams.searchValue,
          beginTime:this.dateRange[0]?this.dateRange[0]:'',
          endTime:this.dateRange[1]?this.dateRange[1]:'',
        }).then(response => {
          this.$set(this,'correlationNum',response.data.total>999?' 999+':(response.data.total<1?'':' '+response.data.total));
        });
      },
      //与我相关按钮
      topRightClickType(){
        this.$set(this.queryParams,'myRelated',this.queryParams.myRelated==1?0:1);
        this.handleQuery();
      },
      // 批量下载
      allDownload(){
        let self = this;
        if(!this.ids[0]){
          this.msgError('请先勾选数据')
          return
        }
        this.$confirm('是否批量下载?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          let text = self.ids+'';
          self.download('/zd-security/checkRectify/genReport/zip/'+text, {}, '批量报告下载.zip')
        }).then(() => {
        }).catch(() => {});
      },
      // 单个下载按钮
      downloadButton(row){
        this.download('/zd-security/checkRectify/genReport/'+row.id, {}, row.title+'(整改报告).docx')
      },
      //开关详情页面
      goPage(type,data){
        if(this.pageType != type){
          if(type == 2){
            this.$set(this.propsListPageData,'id',data.id);
          }
          this.$set(this,'pageType',type);
        }
      },
      //查看单个文档
      lookDocumentButton(type,row){
        if(type==1){
          this.$set(this,'propsLookDocumentData',{
            title:"整改报告",
            type:1,
            id:row.id,
            name:row.title
          });
          this.$set(this,'lookDocumentType',true);
        }else{
          this.$set(this,'lookDocumentType',false);
        }
      },
      //获取数据列表
      getList(){
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.beginTime = this.dateRange[0]
        }else{
          obj.beginTime = ""
        }
        if(this.dateRange[1]){
          obj.endTime = this.dateRange[1]
        }else{
          obj.endTime = ""
        }
        let self = this;
        setTimeout(function(){
          self.getCorrelationNum();
        },500);
        checkRectifyList(obj).then(response => {
          this.total = response.data.total;
          this.tableList = response.data.records;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.$set(this.queryParams,'pageNum',1);
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$set(this,'dateRange',[]);
        this.$set(this,'queryParams',{
          pageNum:1,
          pageSize:20,
          deptId:'',
          searchValue:'',
          myRelated:1,
        });
        this.handleQuery();
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.id
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.selectedNum = selection.length;
        this.ids = selection.map(item => item.id)
      },
      //获取学院列表
      listDepartments(){
        listDepartments().then(response => {
          this.deptSelectList = response.data;
        });
      },
    },
  }
</script>

<style scoped lang="scss">
  .rectificationReport{
    flex: 1;
    display: flex !important;
    flex-direction: column;
    overflow: hidden;
    .rectificationReport-page{
      flex: 1;
      display: flex !important;
      flex-direction: column;
      overflow: hidden;
      .title-box{
        padding-top:20px;
        .form-button-max-big-box{
          display: inline-block;
          .form-button-big-box{
            display: flex;
            div{
              position: relative;
              height:40px;
              width:100px;
              line-height: 40px;
              text-align: center;
              color:#0045AF;
              font-size:14px;
              border:1px solid #0045AF;
              border-radius:4px;
              margin-left:10px;
              font-weight:500;
              cursor: pointer;
            }
            .checkDiv{
              color:#fff;
              background-color:#0045AF;
              border:1px solid #0045AF;
            }
          }
        }
        .form-button-max-big-box-me{
          display: inline-block;
          .form-button-big-box-me{
            display: flex;
            div{
              position: relative;
              height:40px;
              width:130px;
              line-height: 40px;
              text-align: center;
              color:#999;
              font-size:14px;
              border:1px solid #999;
              border-radius:4px;
              margin-left:10px;
              font-weight:500;
              cursor: pointer;
              .icon-p-me{
                width:15px;
                height:15px;
                line-height:15px;
                text-align: center;
                position: absolute;
                right:0;
                bottom:0;
                color:#fff;
                background: #0183fa;
                border-top-left-radius:4px;
              }
            }
            .checkDiv-me{
              color:#0183FA!important;
              border:1px solid #0183FA!important;
            }
          }
        }
      }
      .content-box{
        flex: 1;
        display: flex;
        flex-direction: column;
        padding:0 20px 20px;
        overflow: hidden;
      }
    }
  }
</style>
