<!--分级管控/实验室列表-->
<template>
  <div class="checkList">
    <div class="checkList-page" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryFormTwo" :inline="true" v-show="showSearch" style="margin-top:20px;">
        <el-form-item label="发布状态" prop="isFabu" label-width="80px">
          <el-select v-model="queryParams.isFabu" placeholder="请选择发布状态" clearable size="small">
            <el-option label="未发布" value="0" />
            <el-option label="已发布" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="整改状态" prop="zgStatus" label-width="80px">
          <el-select v-model="queryParams.zgStatus" placeholder="请选择整改状态" clearable size="small">
            <el-option label="待整改" value="0" />
            <el-option label="整改待复核" value="1" />
            <el-option label="复核未通过" value="2" />
            <el-option label="整改中" value="3" />
            <el-option label="完成" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="实验室" prop="laboratoryName" label-width="60px">
          <el-input
            style="width:160px;"
            v-model="queryParams.laboratoryName"
            placeholder="请输入实验室名称"
            clearable
            size="small"
            maxLength="15"
          />
        </el-form-item>
        <el-form-item label="实验室负责人" prop="fzrName" label-width="110px">
          <el-input
            style="width:170px;"
            v-model="queryParams.fzrName"
            placeholder="请输入实验室负责人"
            clearable
            maxLength="10"
            size="small"
          />
        </el-form-item>
        <el-form-item label="整改类型" prop="zgType" label-width="80px">
          <el-select v-model="queryParams.zgType" placeholder="请选择整改类型" clearable size="small">
            <el-option label="一般整改" value="0" />
            <el-option label="重大整改" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="检查时间" label-width="80px">
          <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">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="checkRecordList" @selection-change="handleSelectionChange" ref="multipleTable" :row-key="getRowKeys">
        <el-table-column type="selection" width="55" :reserve-selection="true" align="center" />
        <el-table-column label="发布状态" align="left" prop="isFabu" width="100">
          <template slot-scope="scope">
            {{scope.row.isFabu == '0'?'未发布':(scope.row.isFabu == '1'?'已发布':'')}}
          </template>
        </el-table-column>
        <el-table-column label="整改状态" align="left" prop="zgStatus" width="100">
          <template slot-scope="scope">
            {{scope.row.zgStatus == '0'?'待整改':(scope.row.zgStatus == '1'?'整改待复核':(scope.row.zgStatus == '2'?'复核未通过':(scope.row.zgStatus == '3'?'整改中':(scope.row.zgStatus == '4'?'完成':''))))}}
          </template>
        </el-table-column>
        <el-table-column label="实验室名称" align="left" prop="laboratoryName"  :show-overflow-tooltip="true"/>
        <el-table-column label="实验楼" align="left" prop="buildingName" width="160"/>
        <el-table-column label="房间号" align="left" prop="fjNumber" width="100"/>
        <el-table-column label="实验室负责人" align="left" prop="fzrName" width="140"/>
        <el-table-column label="整改类型" align="left" prop="zgType" width="100">
          <template slot-scope="scope">{{scope.row.zgType==0?'一般整改':(scope.row.zgType==1?'重大整改':'')}}</template>
        </el-table-column>
        <el-table-column label="检查时间" align="left" prop="createTime" width="200"/>
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="120" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="button-box" style="display: flex;margin-left:20px;">
            <p class="table-min-button"
               @click="goPageInfo(2,scope.row)"
               v-hasPermi="['laboratory:checkRecord:query']"
            >查看</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div style="margin:0 auto;width:220px;">
        <el-col :span="1.5">
          <p class="reset-button-one"
             style="width:90px;margin-right:40px;"
             @click="outPage"
          >取消</p>
        </el-col>
        <el-col :span="1.5">
          <p class="add-button-one-90"
             @click="okButton"
          >确定</p>
        </el-col>
      </div>
    </div>
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检查名称" prop="releaseDescribe">
          <el-input v-model="form.releaseDescribe" maxLength="20" placeholder="请输入检查名称" style="width:330px;"/>
        </el-form-item>
        <el-form-item label="实验室" prop="list">
          <p style="line-height:40px;margin:0;" v-if="!form.list[0]">请重新选择检查记录</p>
          <div style="display: inline-block;background:#0E9EED;padding:0 10px;margin:0 10px 10px 0;border-radius:6px;" v-for="(item,index) in form.list" :key="index">
            <div style="display: flex;">
              <p style="margin:0;color:#fff;line-height:40px;">{{item.laboratoryName}}</p>
              <p style="margin:0;line-height:40px;color:#fff;margin-left:20px;cursor: pointer" class="el-icon-close" @click="delItem(index)"></p>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <check-record-info-page v-if="pageType == 2" :infoId="infoId"></check-record-info-page>
  </div>
</template>

<script>
  import { listJcjl,release,checkFabu } from "@/api/laboratory/checkRecord";
  import checkRecordInfoPage from "./checkRecordInfoPage.vue"
  export default {
    name: "subjectList",
    components: {
      checkRecordInfoPage
    },
    data() {
      return {
        tableButtonType:this.hasPermiDom(['laboratory:checkRecord:query']),
        pageType:1,
        infoId:"",
        dateRange:[],
        checkRecordList:[],
        loading:false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        queryParams:{
          pageNum:1,
          pageSize:20,
        },
        //已选中
        ids:[],
        idData:[],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        open:false,
        title:"",
        form:{
          releaseDescribe:"",
          list:[],
        },
        // 表单校验
        rules: {
          releaseDescribe: [
            { required: true, message: "请输入检查名称", trigger: "blur" },
            { required: true, message: "请输入检查名称", validator: this.spaceJudgment, trigger: "blur" }
          ],
          list: [
            { required: true, message: "请选择记录", trigger: "blur" }
          ]
        },
      }
    },
    created(){

    },
    mounted(){
      this.getList();
    },
    methods:{
      //页面状态切换
      goPageInfo(type,row){
        if(type == 2){
          this.pageType = 2;
          this.infoId = row.id;
        }else if(type == 1){
          this.pageType = 1;
          this.getList();
        }
      },
      outPage(){
        this.$parent.goPageInfo(1);
      },
      //确定按钮
      okButton(){
        if(!this.ids[0]){
          this.msgError("请勾选记录")
          return
        }
        let obj = {
          name:"",
          list:JSON.parse(JSON.stringify(this.idData)),
        };
        this.$set(this,'form',obj);
        this.open = true;
        this.title = "发布";
      },
      /** 提交按钮 */
      submitForm() {
        let self = this;
        this.$refs["form"].validate(valid => {
          if (valid) {
            let newList = [];
            for(let i=0;i<self.form.list.length;i++){
              newList.push(self.form.list[i].id)
            }
            let obj = {
              releaseDescribe:this.form.releaseDescribe,
              checkId:newList + '',
            };
            release(obj).then( response => {
              if(response.code == 200){
                this.msgSuccess("操作成功");
                this.$parent.goPageInfo(1);
              }
            });
          }
        });
      },
      cancel() {
        this.open = false;
      },
      //删除选中记录
      delItem(index){
        this.form.list.splice(index,1);
      },
      //获取数据列表
      getList(){
        this.loading = true;
        if(this.dateRange&&this.dateRange.length>0){
          this.queryParams.beginTime=this.dateRange[0]
          this.queryParams.endTime=this.dateRange[1]
        }else{
          this.queryParams.beginTime=null;
          this.queryParams.endTime=null
        }
        // listCheckRecord(this.queryParams).then( response => {
        listJcjl(this.queryParams).then( response => {
          this.checkRecordList =  response.rows;
          this.total =  response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.dateRange = [];
        this.queryParams.beginTime = "";
        this.queryParams.endTime = "";
        this.$refs.multipleTable.clearSelection()
        this.handleQuery();
      },
      /*===记录勾选数据===
        需要再el-table 添加  :row-key="getRowKeys"
        需要在selection 添加 :reserve-selection="true"
      */
      getRowKeys(row) {
        return row.id
      },
      //选择本页
      selectPage(){
        console.log(this.$refs.multipleTable)
        this.$refs.multipleTable.toggleAllSelection()
        // console.log(this.$refs.multipleTable.selection)
      },
      //清除选择
      clearSelection(){
        this.$refs.multipleTable.clearSelection()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.idData = selection.map(item => item)
        this.single = selection.length!==1
        this.multiple = !selection.length
        if(selection[0]){
          this.checkFabu();
        }
      },
      //检查是否已发布
      checkFabu(){
        let obj = {
          checkId:this.ids[this.ids.length-1]
        };
        checkFabu(obj).then( response => {
          if(response.data != 0){
            this.msgError("近期已发布过该记录")
            this.$refs.multipleTable.toggleRowSelection(this.idData[this.idData.length-1],false);
          }
        });
      }
    }
  }
</script>

<style lang="scss" scoped>
  .checkList{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden !important;
    padding:0 20px 20px;
    .checkList-page{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden !important;
    }
  }
</style>
