<template>
  <div class="app-container snapshotManagement">
    <div class="snapshotManagement-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">
            <div class="form-button-big-box" style="margin-left:10px;">
              <div :class="queryParams.rectifyStatus===''?'checkDiv':''" @click="topLeftClickType('')">
                <p class="text-p">全部</p>
                <p class="el-icon-check icon-p" v-if="queryParams.rectifyStatus===''"></p>
              </div>
              <div :class="queryParams.rectifyStatus===0?'checkDiv':''" @click="topLeftClickType(0)">
                <p class="text-p">待整改</p>
                <p class="el-icon-check icon-p" v-if="queryParams.rectifyStatus===0"></p>
              </div>
              <div :class="queryParams.rectifyStatus===1?'checkDiv':''" @click="topLeftClickType(1)">
                <p class="text-p">已整改</p>
                <p class="el-icon-check icon-p" v-if="queryParams.rectifyStatus===1"></p>
              </div>
              <div :class="queryParams.rectifyStatus===2?'checkDiv':''" @click="topLeftClickType(2)">
                <p class="text-p">暂无法整改</p>
                <p class="el-icon-check icon-p" v-if="queryParams.rectifyStatus===2"></p>
              </div>
            </div>
          </div>
          <el-form-item label="关键字" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="实验室/房间号/上报人"
              clearable
              style="width: 180px"/>
          </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: 220px"
              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:rectifyClap:add']">
            <el-col :span="1.5">
              <p class="inquire-button-one"
                 style="width:100px;margin-right:0;"
                 @click="addButton"
              ><i class="el-icon-plus"></i> 随手拍</p>
            </el-col>
          </el-form-item>
          <el-form-item style="float: right;">
            <el-col :span="1.5">
              <p class="add-button-one-90"
                 style="width:100px;"
                 @click="goPage(2)"
              >上报记录</p>
            </el-col>
          </el-form-item>
        </el-form>
      </div>
      <div class="content-box">
        <el-table  border :data="tableList" ref="multipleTable" @sort-change="sortChange">
          <el-table-column label="序号" align="center"  type="index" width="60" />
          <el-table-column label="学院" align="center" prop="deptName" show-overflow-tooltip/>
          <el-table-column label="实验室" align="center" prop="subName" show-overflow-tooltip width="220">
            <template slot-scope="scope">{{scope.row.roomNum?scope.row.subName+'-'+scope.row.roomNum:scope.row.subName}}</template>
          </el-table-column>
          <el-table-column label="安全责任人" align="center" prop="safetyPeople" show-overflow-tooltip width="120"/>
          <el-table-column label="上报人" align="center" prop="createName" show-overflow-tooltip width="90"/>
          <el-table-column label="上报时间" sortable="custom" align="center" prop="createTime" show-overflow-tooltip width="157"/>
          <el-table-column label="隐患描述" align="center" prop="hazardDescribe" show-overflow-tooltip width="280"/>
          <el-table-column label="整改人" align="center" prop="rectifyPeople" show-overflow-tooltip width="100"/>
          <el-table-column label="整改时间" sortable="custom" align="center" prop="rectifyTime" show-overflow-tooltip width="157"/>
          <el-table-column label="整改状态" align="center" prop="rectifyStatus" show-overflow-tooltip width="120">
            <template slot-scope="scope">
              {{scope.row.rectifyStatus==0?'待整改':(scope.row.rectifyStatus==1?'已整改':(scope.row.rectifyStatus==2?'暂无法整改':''))}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="deptName" width="70">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p" @click="goPage(3,scope.row)" v-hasPermi="['safety:rectifyClap:query']">详情</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"></listPage>
    <infoPage v-if="pageType == 3" :infoPropsData="infoPropsData"></infoPage>
    <el-dialog class="safetyHazard-info-dialog-box" @close="outDialog"
               :close-on-click-modal="false"
               v-loading="loading"
               title="随手拍上报" :visible.sync="addDialogType" v-if="addDialogType"
               width="787px" append-to-body>
      <div>
        <el-form  ref="addDialogForm" :model="addDialogForm" :rules="rules" label-width="140px">
          <el-form-item label="学院：" prop="deptId">
            <el-select v-model="addDialogForm.deptId" @change="dialogDeptChange" placeholder="请选择学院" style="width:548px;">
              <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">
            <el-select
              style="width:548px;"
              v-model="addDialogForm.subId"
              filterable
              remote
              reserve-keyword
              placeholder="搜索选择实验室"
              @change="dialogSubChange"
              :remote-method="getSelectList"
              :loading="dialogLoading">
              <el-option
                v-for="item in dialogSubList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="隐患描述：" prop="hazardDescribe">
            <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 4}"
              placeholder="请输入隐患描述"
              v-model="addDialogForm.hazardDescribe"
              maxlength="100"
              resize="none"
              show-word-limit
              style="width:548px;">
            </el-input>
          </el-form-item>
          <el-form-item label="隐患照片:" prop="imgDtoList">
            <div class="snapshotManagement-for-img-box" v-for="(img,imgIndex) in addDialogForm.imgDtoList" :key="imgIndex">
              <img class="for-img" :src="img.fileUrl">
              <p class="for-del-button el-icon-circle-close" @click="delImg(imgIndex)"></p>
            </div>
            <el-upload
              v-if="addDialogForm.imgDtoList.length<5"
              style="display: inline-block;overflow: hidden"
              class="avatar-uploader"
              :action="uploadImgUrl"
              :show-file-list="false"
              :on-success="(res)=>handleAvatarSuccess(res)"
              :headers="headers"
              :before-upload="(res)=>beforeAvatarUpload(res)">
              <p class="el-icon-plus up-img-p" style="display: inline-block"></p>
            </el-upload>
            <p class="dialog-material-text">支持jpg/png/bmp/gif格式，最多上传5张</p>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer dialog-footer-box" style="display: flex">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="outDialog">取消</p>
        <p class="dialog-footer-button-primary" @click="upDialogButton">确定</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { securityCheckClapList,checkClapAdd } from '@/api/safetyCheck/index'
  import { getNoAdminSubjectListNopage } from "@/api/laboratory/subject";
  import { listDepartments } from "@/api/system/dept";
  import listPage from './listPage.vue'
  import infoPage from './infoPage.vue'
  import { getToken } from "@/utils/auth";
  export default {
    name: 'index',
    components: {
      listPage,
      infoPage
    },
    data(){
      return{
        loading:false,
        uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        pageType:1,
        deptSelectList:[],
        queryParams:{
          pageNum:1,
          pageSize:20,
          deptId:'',
          searchValue:'',
          rectifyStatus:'',
          upTimeOrder:"",
          zgTimeOrder:"",
        },
        dateRange:[],
        tableList:[{}],
        total:0,
        //新增窗口
        addDialogType:false,
        addDialogForm:{
          deptId:"",
          deptName:"",
          subId:"",
          subName:"",
          hazardDescribe:"",
          imgDtoList:[],
        },
        rules:{
          deptId: [
            { required: true, message: "请选择学院", trigger: "change" },
          ],
          subId: [
            { required: true, message: "请选择实验室", trigger: "change" },
          ],
          imgDtoList: [
            { required: true, message: "请上传隐患照片", trigger: "change" },
          ],
        },
        dialogDeptOptions:[],
        dialogLoading:false,
        dialogSubList:[],
        dialogSubListData:[],
        suffixName:"",
        //详情
        infoPropsData:{}
      }
    },
    created(){

    },
    mounted(){
      this.listDepartments();
      this.getList();
    },
    methods:{
      goPage(type,data){
        if(this.pageType != type){
          if (type==1){
            this.$set(this,'pageType',type);
          } else if(type==2){
            this.$set(this,'pageType',type);
          } else if(type==3){
            this.$set(this,'infoPropsData',data);
            this.$set(this,'pageType',type);
          } else if(type==4){
            this.$set(this,'pageType',1);
            this.getList();
          }
        }
      },
      //范围选择
      topLeftClickType(type){
        if(this.queryParams.rectifyStatus !== type){
          this.$set(this.queryParams,'rectifyStatus',type);
          this.handleQuery();
        }
      },
      //时间排序方法
      sortChange(val){
        if(val.prop == 'rectifyTime'){
          this.$set(this.queryParams,'zgTimeOrder',val.order=='ascending'?'1':(val.order=='descending'?'2':''));
          this.$set(this.queryParams,'upTimeOrder','');
          this.handleQuery();
        }else if(val.prop == 'createTime'){
          this.$set(this.queryParams,'upTimeOrder',val.order=='ascending'?'1':(val.order=='descending'?'2':''));
          this.$set(this.queryParams,'zgTimeOrder','');
          this.handleQuery();
        }
      },
      //获取数据列表
      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 = ""
        }
        securityCheckClapList(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:'',
          rectifyStatus:'',
          upTimeOrder:"",
          zgTimeOrder:"",
        });
        this.handleQuery();
      },
      /*==========上传相关==========*/
      handleAvatarSuccess(res) {
        this.$set(this,'loading',false);
        if(this.addDialogForm.imgDtoList.length>4){
          this.msgError('最多只可上传5张')
          return
        }
        let suffixName= this.upDataName.split('.')[this.upDataName.split('.').length - 2]
        //判断文件名中是否有逗号和分号
        if(suffixName.indexOf(',')==-1 && suffixName.indexOf(';')==-1){
        }else{
          this.msgError('文件名里包含逗号或分号，请修改后重新上传！')
          return
        }
        let obj ={
          fileName:this.upDataName,
          fileUrl:res.data.url,
        };
        this.addDialogForm.imgDtoList.push(obj);
        this.$forceUpdate()
      },
      beforeAvatarUpload(file) {
        if(this.addDialogForm.imgDtoList.length>4){
          this.msgError('最多只可上传5张')
          return false
        }
        let type = false;
        if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/gif' || file.type == 'image/bmp') {
          if(file.size> 2100000){
            this.msgError('上传图片大小不能超过2M')
            return false
          }
          this.$set(this,'loading',true);
          this.upDataName = file.name;
          type = true;
        }else{
          this.msgError('仅支持jpg/png/bmp/gif格式')
          type = false;
        }
        return type;
      },
      //删除照片
      delImg(imgIndex){
        this.addDialogForm.imgDtoList.splice(imgIndex,1);
      },
      /**************** 新增窗口 ******************/
      //提交
      upDialogButton(){
        this.$refs["addDialogForm"].validate(valid => {
          if (valid) {
            checkClapAdd(this.addDialogForm).then(response => {
              this.msgSuccess(response.msg);
              this.outDialog();
              this.getList();
            });
          }
        })
      },
      addButton(){
        this.$set(this,'addDialogForm',{
          deptId:"",
          deptName:"",
          subId:"",
          subName:"",
          hazardDescribe:"",
          imgDtoList:[],
        });
        this.$set(this,'addDialogType',true);
      },
      outDialog(){
        this.$set(this,'addDialogType',false);
      },
      //选中院系
      dialogDeptChange(val){
        let self = this;
        let obj = {
          deptId:val,
        }
        getNoAdminSubjectListNopage(obj).then(response => {
          this.$set(this,'dialogSubListData',response.data);
          this.$set(this,'dialogSubList',response.data);
          for(let i=0;i<self.deptSelectList.length;i++){
            if(val == self.deptSelectList[i].deptId){
              self.$set(self.addDialogForm,'deptName',self.deptSelectList[i].deptName);
            }
          }
          this.$set(this.addDialogForm,'subId','');
          this.$set(this.addDialogForm,'subName','');
        });
      },
      //实验室选中
      dialogSubChange(val){
        let self = this;
        for(let i=0;i<self.dialogSubList.length;i++){
          if(val == self.dialogSubList[i].id){
            self.$set(self.addDialogForm,'subName',self.dialogSubList[i].name);
          }
        }
      },
      /** 实验室-本地懒加载 */
      getSelectList(val) {
        let self = this;
        let list = [];
        for(let i=0;i<self.dialogSubListData.length;i++){
          if(self.dialogSubListData[i].name.indexOf(val) != -1){
            list.push(self.dialogSubListData[i]);
          }
        }
        this.dialogSubList = JSON.parse(JSON.stringify(list))
      },
      //获取学院列表
      listDepartments(){
        listDepartments().then(response => {
          this.deptSelectList = response.data;
        });
      },
    }
  }
</script>

<style scoped lang="scss">
  .snapshotManagement{
    flex: 1;
    display: flex !important;
    flex-direction: column;
    overflow: hidden;
    .snapshotManagement-page{
      flex: 1;
      display: flex !important;
      flex-direction: column;
      overflow: hidden;
      .title-box{
        padding-top:20px;
        border-bottom:1px solid #dedede;
        .form-button-max-big-box{
          display: inline-block;
          .form-button-big-box{
            display: flex;
            div{
              position: relative;
              height:40px;
              width:80px;
              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{
                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{
              color:#0183FA;
              border:1px solid #0183FA;
            }
          }
        }
      }
      .content-box{
        flex: 1;
        display: flex;
        flex-direction: column;
        padding:20px;
        overflow: hidden;
      }
    }
  }
  ::v-deep .snapshotManagement-for-img-box{
    width:80px;
    height:80px;
    border-radius:4px;
    display: inline-block;
    overflow: hidden;
    margin-right:20px;
    position: relative;
    .for-img{
      width:80px;
      height:80px;
      display: inline-block;
      overflow: hidden;
    }
    .for-del-button{
      background: rgba(0,0,0,0.7);
      width:20px;
      height:20px;
      line-height: 20px;
      text-align: center;
      color:#fff;
      border-bottom-left-radius:4px;
      cursor: pointer;
      position: absolute;
      top:0;
      right:0;
    }
  }
  ::v-deep .up-img-p{
    height:80px;
    width:80px;
    line-height:80px;
    text-align: center;
    font-size:16px;
    border-radius:4px;
    border:1px dashed #E0E0E0;
  }
</style>
