<!-- 待入库 -->
<template>
  <div class="app-container savePending">
    <div class="page-container savePendingPage" 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: 120px">
              <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: 140px"
              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: 110px">
              <el-option
                v-for="dict in levelList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="chemicalCategory">
            <el-select v-model="queryParams.chemicalCategory" clearable placeholder="请选择类别" style="width: 110px">
              <el-option
                v-for="dict in optionListA"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="state">
            <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 label="" prop="searchValue">
            <el-input
              maxLength="30"
              v-model="queryParams.searchValue"
              placeholder="化学品名/CAS/编码/实验室/订单号..."
              style="width: 240px"
            />
          </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;width: 60px"
             v-hasPermiRouter="['stock:wait:add']"
             @click="tableButton(1)"
          >添加</p>
          <p class="page-submit-common-style-button"
             style="float: right;width: 60px;margin-right: 10px"
             v-hasPermiRouter="['stock:wait:add']"
             @click="tableButton(5)"
          >同步</p>
          <el-form-item style="float: right;">
            <export-component :exportConfig="exportConfig" v-hasPermiRouter="['stock:wait:export']"></export-component>
          </el-form-item>
          <el-form-item style="float: right;">
            <import-component :importConfig="importConfig" v-hasPermiRouter="['stock:wait:import']"></import-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="150" fixed show-overflow-tooltip/>
          <el-table-column label="编码" prop="chemicalNum" width="120" show-overflow-tooltip/>
          <el-table-column label="CAS" prop="casNum" width="120" 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="normsNum" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.normsNum}}{{scope.row.normsUnit}}/{{scope.row.applyUnit}}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="applyNum" width="120" show-overflow-tooltip/>
          <el-table-column label="过期时间" prop="expireTime" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-date-picker
                @change="expireTimeChange(scope.row)"
                :clearable="false"
                class="table-date-picker"
                value-format="yyyy-MM-dd"
                v-model="scope.row.expireTime"
                type="date"
                placeholder="无">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="归属人" prop="topicGroup" width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.topicGroup">{{scope.row.topicGroupName}}</span>
              <span v-if="!scope.row.topicGroup">{{scope.row.applyUserName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="学院" prop="deptName" width="150" show-overflow-tooltip/>
          <el-table-column label="楼栋楼层" prop="buildName" width="180" 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="230" show-overflow-tooltip/>
          <el-table-column label="实验室负责人" prop="adminName" width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.adminName}}-{{scope.row.adminPhone}}</span>
            </template>
          </el-table-column>
          <el-table-column label="安全员" prop="oneSafeName" width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.oneSafeName">{{scope.row.oneSafeName}}-{{scope.row.oneSafePhone}}</span>
              <span v-if="scope.row.twoSafeName">{{scope.row.twoSafeName}}-{{scope.row.twoSafePhone}}</span>
            </template>
          </el-table-column>
          <el-table-column label="入库期限" prop="createTime" width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.stockPeriod,"{y}-{m}-{d} {h}:{i}") }}</span ><span v-if="scope.row.isOverdue">逾期</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" 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"
                   v-hasPermiRouter="['stock:wait:detail']"
                   @click="tableButton(2,scope.row)"
                >详情</p>
                <p class="table-button-p"
                   v-if="scope.row.source==2 || scope.row.source==3 || scope.row.source==4"
                   v-hasPermiRouter="['stock:wait:edit']"
                   @click="tableButton(3,scope.row)"
                >编辑</p>
                <p class="table-button-p"
                   v-if="scope.row.source==2 || scope.row.source==3"
                   v-hasPermiRouter="['stock:wait:del']"
                   @click="tableButton(4,scope.row)"
                >删除</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>
    <info-page :propsData="propsData" v-if="pageType === 2"></info-page>
    <add-page :propsData="propsData" v-if="pageType === 3"></add-page>
  </div>
</template>
<script>
  import addPage from "./addPage.vue";
  import infoPage from "./infoPage.vue";
  import importComponent from "@/components/importComponent/importComponent.vue";
  import exportComponent from "@/components/exportComponent/exportComponent.vue";
  import { getDeptDropList,systemBuildingGetTreeList,} from "@/api/commonality/permission";
  import { getDicts } from '@/api/commonality/noPermission'
  import {
    chemicalApplyDetail,
    chemicalStockWaitDelete, chemicalStockWaitDetail,
    chemicalStockWaitList,
    chemicalStockWaitUpdateByExpire, chemicalStockWaitUpdateStockWait
  } from '@/api/chemicalManage'
  export default {
    name: 'index',
    components: {
      importComponent,
      exportComponent,
      addPage,
      infoPage,
    },
    data () {
      return {
        //导出配置
        exportConfig:{
          api:'/chemical/stockWait/export',             //导出接口地址
          ids:'',                                       //勾选导出,勾选的IDS
          fileName:'待入库导出',                         //导出文件的命名
        },
        importConfig:{
          upLoadApi:'/chemical/stockWait/importData',                //上传接口地址
          downloadApi:'/chemical/stockWait/importTemplate',          //下载模板接口地址
          loseApi:'/chemical/stockWait/importErrorData',             //失败报表接口地址
          fileName:'待入库导入模板',                                   //下载模板命名
        },
        tableButtonType:this.hasPermiDom(['stock:wait:detail','stock:wait:edit','stock:wait:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //学院
        deptOptions:[],
        //楼栋楼层
        buildFloorOptions:[],
        //下拉列表数据
        levelList:[{value:1,label:'管控'},{value:2,label:'非管控'}],
        optionListA:[],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          deptId:'',
          buildId :null,
          floorId:null,
          chemicalLevel:'',
          searchValue:"",
          buildIdAndfloorId :[],
        },
        //时间数据
        dateRange:[],
        //列表数据
        dataList:[],
        //数据数量
        total:10,
        // 选中数组
        selectedNum:0,
        ids:[],
        multiple:true,
        //组件传参
        propsData:{},
      }
    },
    created () {

    },
    mounted () {
      this.getDeptDropList();
      this.getDicts();
      this.systemBuildingGetTreeList();
      this.getList();
    },
    methods: {
      //获取化学品类别
      getDicts(){
        getDicts('chemicals_category').then(response => {
          this.$set(this,'optionListA',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:'',
          buildId :null,
          floorId:null,
          chemicalLevel:'',
          searchValue:"",
          buildIdAndfloorId :[],
        });
        this.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //获取数据列表
      getList(){
        this.$set(this,'loading',true);
        let obj = JSON.parse(JSON.stringify(this.queryParams))
        if(this.dateRange[0]){
          obj.startTime = this.dateRange[0]+'T00:00:00'
          obj.endTime = this.dateRange[1]+'T23:59:59'
        }else{
          obj.startTime = "";
          obj.endTime = "";
        }
        chemicalStockWaitList(obj).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,'propsData',{});
          this.$set(this,'pageType',3);

        }else if(type == 2){
          //详情
          chemicalStockWaitDetail({waitId:row.waitId}).then(response => {
            this.$set(this,'propsData',response.data);
            this.$set(this,'pageType',2);
          });
        }else if(type == 3){
          //编辑
          chemicalStockWaitDetail({waitId:row.waitId}).then(response => {
            this.$set(this,'propsData',response.data);
            this.$set(this,'pageType',3);
          });
        }else if(type == 4){
          //删除
          this.$confirm('是否确认删除?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
          }).then(() => {
            chemicalStockWaitDelete({waitId:row.waitId}).then(response => {
              self.msgSuccess(response.message)
              self.getList();
            });
          }).catch(() => {});
        }else if(type == 5){
          //同步
          chemicalStockWaitUpdateStockWait().then(response => {
            self.msgSuccess(response.message)
          });
        }else if(type == 'out'){
          //返回并刷新
          this.$set(this,'pageType',1);
          this.getList();
        }
      },
      //过期时间
      expireTimeChange(row){
        chemicalStockWaitUpdateByExpire({waitId:row.waitId,expireTime:row.expireTime}).then(response => {
          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.waitId
      },
      //多选框选中数据
      handleSelectionChange(selection) {
        this.selectedNum = selection.length;
        this.ids = selection.map(item => item.waitId)
        this.exportConfig.ids=this.ids;
        this.multiple = !selection.length

      },
    },
  }
</script>
<style scoped lang="scss">
  .savePending{
    .savePendingPage{
      .table-date-picker{
        width:176px;
        height:26px;
        ::v-deep input{
          height:26px!important;
          padding:0 45px;
        }
        ::v-deep .el-input__icon{
          line-height:26px;
        }
      }
    }
  }
</style>
