<!-- 库存明细 -->
<template>
  <div class="inventoryList">
    <div class="page-container inventoryListPage" v-if="pageType === 1">
      <div class="title-max-button-box">
        <div class="check-box">
          <p>库存明细</p>
          <p></p>
        </div>
        <div @click="titleClick">
          <p>汇总统计</p>
          <p></p>
        </div>
      </div>
      <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: 140px">
              <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
              style="width: 120px"
              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="chemicalLevel">
            <el-select v-model="queryParams.chemicalLevel"
                       placeholder="请选择级别" style="width: 140px">
              <el-option
                v-for="item in levelOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="chemicalCategory">
            <el-select v-model="queryParams.chemicalCategory"
                       placeholder="请选择类别" style="width: 140px">
              <el-option
                v-for="item in categoryOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="useStatus">
            <el-select v-model="queryParams.useStatus"
                       placeholder="请选择状态" style="width: 140px">
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="化学品名/CAS/编码/实验室/房间号"
              style="width: 340px"
            />
          </el-form-item>
          <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
          <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
          <el-form-item style="float: right;">
            <export-component :exportConfig="exportConfig" v-hasPermiRouter="['chemical:stock:export']"></export-component>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-content-box">
        <el-table class="table-box" v-loading="loading" border :data="dataList" ref="table-box"
                  @selection-change="handleSelectionChange" :row-key="getRowKeys">
          <el-table-column type="selection" width="50" align="center" fixed="" :reserve-selection="true"/>
          <el-table-column label="名称" prop="chemicalName" width="200" fixed="" show-overflow-tooltip/>
          <el-table-column label="编码" prop="tagCode" width="180" show-overflow-tooltip/>
          <el-table-column label="类别" prop="chemicalCategoryName" width="120" show-overflow-tooltip/>
          <el-table-column label="级别" prop="chemicalLevelName" width="120" show-overflow-tooltip/>
          <el-table-column label="学院" prop="deptName" width="120" show-overflow-tooltip/>
          <el-table-column label="楼栋楼层" prop="content" width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.buildName}}-{{scope.row.floorName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="实验室" prop="content" width="230" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.subName}}({{scope.row.subRoom?scope.row.subRoom:''}})</span>
            </template>
          </el-table-column>
          <el-table-column label="实验室负责人" prop="adminUser" width="170" show-overflow-tooltip/>
          <el-table-column label="安全员" prop="safeUser" width="150" show-overflow-tooltip/>
          <el-table-column label="存储位置" prop="position" width="250" show-overflow-tooltip/>
          <el-table-column label="规格" prop="specNum" width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.specNum}}{{scope.row.specUnit}}/{{scope.row.packUnit}}</span>
            </template>
          </el-table-column>
          <el-table-column label="归属人" prop="belongName" width="150" show-overflow-tooltip/>
          <el-table-column label="库存余量" prop="surplus" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.surplus}}{{scope.row.specUnit}}</span>
            </template>
          </el-table-column>

          <el-table-column label="状态" prop="statusStr" width="100" show-overflow-tooltip/>
          <el-table-column label="操作" width="100" 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(2,scope.row)"
                   v-hasPermiRouter="['chemical:stock:detail']"
                >详情</p>
                <p class="table-button-null"></p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="selected-num-box" v-show="total>0">
          <p class="selected-num-p">
            <i class="el-icon-warning"></i>
            已选择 {{selectedNum}} 项
          </p>
          <pagination :page-sizes="[20, 30, 40, 50]"
                      :total="total"
                      :page.sync="queryParams.page"
                      :limit.sync="queryParams.pageSize"
                      @pagination="getList"
          />
        </div>
      </div>
    </div>
    <infoPage v-if="pageType == 2" :propsData="propsData"></infoPage>
    <!--<add-page :propsData="propsData" v-if="pageType === 2"></add-page>-->
  </div>
</template>
<script>
  //import { getDicts } from "@/api/commonality/noPermission";
  //import { systemUserSelect } from "@/api/commonality/permission";
  //import { getInfo } from "@/api/basicsModules/index";
  //import addPage from "./addPage.vue";
  import infoPage from "./infoPage.vue"
  import exportComponent from "@/components/exportComponent/exportComponent.vue";
  import { getDeptDropList,systemBuildingGetTreeList,} from "@/api/commonality/permission";
  import {
    chemicalCabinetList,
    chemicalStockStockDetails,
    chemicalStockStockDetailsList
  } from '@/api/chemicalManage'
  import { getDicts } from '@/api/commonality/noPermission'
  export default {
    name: 'inventoryList',
    components: {
      exportComponent,
      infoPage,
    },
    data () {
      return {
        titleType:1,
        //导出配置
        exportConfig:{
          api:'chemical/stock/stockDetailsListExport',             //导出接口地址
          ids:'',                                       //勾选导出,勾选的IDS
          fileName:'库存明细',                         //导出文件的命名
        },
        tableButtonType:this.hasPermiDom(['chemical:stock:detail']),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //状态
        statusOptions:[
          {label:'未使用',value:'0'},
          {label:'使用中',value:'1'},
          {label:'超时未归还',value:'2'},
        ],
        //类别
        categoryOptions:[],
        //级别
        levelOptions:[
          {label:'管控',value:'1'},
          {label:'非管控',value:'2'},
        ],
        //学院
        deptOptions:[],
        //楼栋楼层
        buildFloorOptions:[],
        //下拉列表数据
        optionList:[{value:true,label:'启用'},{value:false,label:'停用'}],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          deptId :null,
          floorId :null,
          buildId :null,
          chemicalLevel :null,
          chemicalCategory :null,
          useStatus :null,
          searchValue :null,
          buildIdAndfloorId :[],
        },
        //列表数据
        dataList:[],
        //数据数量
        total:10,
        // 选中数组
        selectedNum:0,
        ids:[],
        multiple:true,
        //组件传参
        propsData:{},
      }
    },
    created () {

    },
    mounted () {
      this.getDeptDropList();
      this.getDicts();
      this.systemBuildingGetTreeList();
      this.getList();
    },
    methods: {
      titleClick(){
        this.$parent.titleTypeButton(2);
      },
      //获取化学品类别
      getDicts(){
        getDicts('chemicals_category').then(response => {
          this.$set(this,'categoryOptions',response.data);
        });
      },
      //查询按钮
      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.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          deptId :null,
          floorId :null,
          buildId :null,
          chemicalLevel :null,
          chemicalCategory :null,
          useStatus :null,
          searchValue :null,
          buildIdAndfloorId :[],
        });
        this.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        chemicalStockStockDetailsList(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){
        if(type == 2){
          //详情
          chemicalStockStockDetails({id:row.id}).then(response => {
            this.$set(this,'propsData',JSON.parse(JSON.stringify(response.data)));
            this.$set(this,'pageType',2);
          });
        }else if(type == 'out'){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //查询学院列表
      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
      },
      /*===记录勾选数据===
        需要再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)
        this.exportConfig.ids = this.ids
        this.multiple = !selection.length
      },
    },
  }
</script>
<style scoped lang="scss">
  .inventoryList{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    .inventoryListPage{
      flex:1;
      display: flex;
      flex-direction: column;
      overflow: hidden;
      .title-max-button-box{
        height:80px;
        display: flex;
        border-bottom:1px solid #dedede;
        div{
          cursor: pointer;
          margin-left:30px;
          p:nth-child(1){
            width:80px;
            line-height: 52px;
            text-align: center;
            font-size:16px;
            margin-top:12px;
          }
          p:nth-child(2){
            width:60px;
            height:4px;
            border-radius:2px;
            margin-left:10px;
          }
        }
        .check-box{
          p:nth-child(1){
            color:#0045AF;
          }
          p:nth-child(2){
            background-color:#0045AF;
          }
        }
      }
      .page-form-title-box{
        border-bottom:none;
      }
      .page-content-box{
        padding-top:0;
      }
    }
  }
</style>
