<!-- 化学品柜管理 -->
<template>
  <div class="app-container chemicalsCabinetManage">
    <div class="page-container chemicalsCabinetManagePage" v-if="pageType === 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" class="form-box" ref="queryForm"
                 :inline="true" style="width:100%;">
          <el-form-item label="" prop="deptId">
            <el-select v-model="queryParams.deptId" placeholder="请选择学院" style="width: 200px">
              <el-option
                v-for="item in deptOptions"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="floorId">
            <el-cascader
              placeholder="请选择楼栋楼层"
              :props="{value: 'id', label: 'name',children:'buildFloorVoList',checkStrictly: true}"
              v-model="queryParams.buildIdAndfloorId"
              :options="buildFloorOptions">
            </el-cascader>
          </el-form-item>
          <el-form-item label="" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="化学品柜名/实验室/姓名/联系方式"
              clearable
              style="width: 260px"
            />
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <p class="page-submit-common-style-button"
             style="float: right;"
             @click="tableButton(2)"
             v-hasPermiRouter="['cabinet:info:add']"
          >新增</p>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList">
          <el-table-column label="名称" prop="cabinetName" width="250" fixed show-overflow-tooltip/>
          <el-table-column label="学院" prop="deptName" width="200" show-overflow-tooltip/>
          <el-table-column label="楼栋楼层" prop="buildName" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.buildName}}-{{scope.row.floorName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="实验室" prop="subName" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.subName}}-({{scope.row.roomNum}})</span>
            </template>
          </el-table-column>
          <el-table-column label="安全员" prop="safeInfo" width="200" show-overflow-tooltip/>
          <el-table-column label="柜门数量" prop="doorNum" width="100" show-overflow-tooltip/>
          <el-table-column label="柜锁数量" prop="lockNum" width="100" show-overflow-tooltip/>
          <el-table-column label="化学品数量" prop="chemicalNum" width="120" show-overflow-tooltip/>
          <el-table-column label="操作" width="230" show-overflow-tooltip v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="tableButton(3,scope.row)"
                   v-hasPermiRouter="['door:stock:list']"
                >化学品</p>
                <p class="table-button-p"
                   @click="tableButton(4,scope.row)"
                   v-hasPermiRouter="['chemical:lockLog:list']"
                >开门记录</p>
                <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)"
                             v-hasPermiRouter="['cabinet:info:edit','cabinet:info:del']">
                  <p class="table-button-p">更多>></p>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="1">二维码</el-dropdown-item>
                    <el-dropdown-item command="2" v-hasPermiRouter="['cabinet:info:edit']">编辑</el-dropdown-item>
                    <el-dropdown-item command="3" v-hasPermiRouter="['cabinet:info:del']">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <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.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </div>
    <qr-code-dialog ref="qrCodeDialog" :qrCodeDialogData="qrCodeDialogData"></qr-code-dialog>
    <add-page :propsData="propsData" v-if="pageType === 2"></add-page>
    <list-page :propsData="propsData" v-if="pageType === 3"></list-page>
    <info-page :propsData="propsData" v-if="pageType === 4"></info-page>
  </div>
</template>
<script>
  import addPage from "./addPage.vue";
  import listPage from "./listPage.vue";
  import infoPage from "./infoPage.vue";
  import qrCodeDialog from "@/components/qrCodeDialog/index.vue"
  import { getDeptDropList,systemBuildingGetTreeList,systemUserSelect} from "@/api/commonality/permission";
  import {
    chemicalCabinetDelete,
    chemicalCabinetDetail,
    chemicalCabinetGetDoorList,
    chemicalCabinetList
  } from '@/api/chemicalManage'
  export default {
    name: 'index',
    components: {
      addPage,
      listPage,
      infoPage,
      qrCodeDialog,
    },
    data () {
      return {
        tableButtonType:this.hasPermiDom(['cabinet:info:edit','chemical:lockLog:list','cabinet:info:del','door:stock:list']),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //学院
        deptOptions:[],
        //楼栋楼层
        buildFloorOptions:[],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          searchValue:"",
          deptId :null,
          buildId :null,
          buildIdAndfloorId :[],
        },
        //列表数据
        dataList:[],
        //数据数量
        total:0,
        //组件传参
        propsData:{},
        //二维码相关
        qrCodeDialogData:{},
      }
    },
    created () {

    },
    mounted () {
      this.getDeptDropList();
      this.systemBuildingGetTreeList();
      this.getList();
    },
    methods: {
      //查询按钮
      handleQuery(){
        if (this.queryParams.buildIdAndfloorId[0]){
          if (this.queryParams.buildIdAndfloorId.length ==1){
            this.$set(this.queryParams,'buildId',this.queryParams.buildIdAndfloorId[0]);
          }else if (this.queryParams.buildIdAndfloorId.length ==2){
            this.$set(this.queryParams,'buildId',this.queryParams.buildIdAndfloorId[0]);
            this.$set(this.queryParams,'floorId',this.queryParams.buildIdAndfloorId[1]);
          }
        }

        this.$set(this.queryParams,'page',1);
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          searchValue:"",
          deptId :null,
          buildId :null,
          floorId :null,
          buildIdAndfloorId :[],
        });
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        chemicalCabinetList(this.queryParams).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //操作按钮
      tableButton(type,row){
        let self = this;
        if(type == 1){
          //返回
          this.$set(this,'pageType',type);
        }else if(type == 2){
          //新增
          this.$set(this,'pageType',type);
          this.$set(this,'propsData','');
        }else if(type == 3){
          //化学品
          //化学品柜-化学品-柜门列表
          chemicalCabinetGetDoorList({cabinetId:row.cabinetId}).then(response => {
            if (response.data[0]){
              row.doorList=response.data;
              this.$set(this,'propsData',row);
              this.$set(this,'pageType',type);
            }else{
              self.msgError("暂无化学品柜!")
            }
          });

        }else if(type == 4){
          //开门记录
          this.$set(this,'propsData',row);
          this.$set(this,'pageType',type);
        }else if(type == 5){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //更多
      handleCommand(val,row){
        let self = this;
        if(val == '1'){
          //二维码
          this.qrCodeDialogButton(row);
        }else if(val == '2'){
          //编辑
          chemicalCabinetDetail({cabinetId:row.cabinetId}).then(response => {
            let userIds=[];
            response.data.cabinetDoorVoList.forEach(function(item) {
              item.cabinetAdminVoList.forEach(function(item2) {
                userIds.push(item2.userId)
              })
            })
            this.filtrationUser(response.data,userIds)

          })
        }else if(val == '3'){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            chemicalCabinetDelete({cabinetId:row.cabinetId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }
      },

      //过滤人员
      filtrationUser(row,userIds){
        systemUserSelect({userIds:userIds}).then(response => {
          if (response.data[0]){
            row.cabinetDoorVoList.forEach(function(item) {
              item.cabinetAdminVoList.forEach(function(item2) {
                response.data.forEach(function(item3) {
                  if (item2.userId==item3.userId){
                    item2.userName=item3.userName
                  }
                })
                userIds.push(item2.userId)
              })
            })
          }
          this.$set(this,'propsData',row);
          this.$set(this,'pageType',2);
        });
      },
      //二维码组件开关
      qrCodeDialogButton(row){
        this.$set(this,'qrCodeDialogData',{
          download:true,
          type:'8',                                                     //二维码类型 用于区分二维码功能类型
          parameter:row.cabinetId,                                             //二维码参数
          name:row.cabinetName,                                                //二维码名称用于下载时命名
          codeName:row.cabinetName,                                            //二维码图片下名称展示(非必传)
        });
        this.$refs['qrCodeDialog'].initialize();
      },
      //查询学院列表
      getDeptDropList(){
        getDeptDropList({deptName:"",level:2,deptType:1}).then(response => {
          this.$set(this, 'deptOptions', response.data)
        });
      },
      //查询楼栋楼层
      systemBuildingGetTreeList(){
        systemBuildingGetTreeList({}).then(response => {
          if (response.data[0]) {
            let list = this.forBuildFloor(response.data);
            let newList = [];
            for(let i=0;i<list.length;i++){
              if(list[i].buildFloorVoList){
                for(let o=0;o<list[i].buildFloorVoList.length;o++){
                  if(list[i].buildFloorVoList[o].buildFloorVoList){
                    newList.push(list[i].buildFloorVoList[o])
                  }
                }
              }
            }
            this.$set(this, 'buildFloorOptions', newList)
          }
        })
      },
      //处理楼栋楼层数据
      forBuildFloor(list){
        let self = this;
        for(let i=0;i<list.length;i++){
          if(list[i].buildFloorVoList){
            if(list[i].buildFloorVoList[0]){
              list[i].buildFloorVoList = self.forBuildFloor(list[i].buildFloorVoList);
            }else{
              delete list[i].buildFloorVoList;
            }
          }else{
            delete list[i].buildFloorVoList;
          }
        }
        return list
      },
    },
  }
</script>
<style scoped lang="scss">
  .chemicalsCabinetManage{
    .chemicalsCabinetManagePage{

    }
  }
</style>
