<!-- 出库记录 -->
<template>
  <div class="app-container takeRecord">
    <div class="page-container takeRecordPage" 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: 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="chemicalCategory">
            <el-select v-model="queryParams.chemicalCategory"  placeholder="请选择类别" style="width: 120px">
              <el-option
                v-for="dict in categoryOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="status">
            <el-select v-model="queryParams.status"
                       placeholder="请选择出库方式"
                       style="width: 144px">
              <el-option
                v-for="dict in optionList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="cabinetId">
            <el-select
              style="width:145px;"
              @change="selectChange"
              v-model="queryParams.cabinetId"
              filterable
              remote
              placeholder="请搜索化学品柜"
              :remote-method="searchSelectList">
              <el-option
                v-for="item in searchOptions"
                :key="item.cabinetId"
                :label="item.cabinetName"
                :value="item.cabinetId">
              </el-option>
            </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: 324px"
            />
          </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;margin-right:0;">
            <export-component :exportConfig="exportConfig" v-hasPermiRouter="['chemical:stockout: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="150" 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="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="safeUser" width="150" show-overflow-tooltip/>
          <el-table-column label="存储位置" prop="position" width="240" show-overflow-tooltip/>
          <el-table-column label="规格" prop="specNum" width="100" 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="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">
            <template slot-scope="scope" >
              <el-popover trigger="hover" placement="top" v-if=" scope.row.status==3">
                <p>{{ scope.row. disuseReason}}</p>
                <div slot="reference">{{ scope.row.statusStr }}</div>
              </el-popover>
              <span v-if=" scope.row.status!=3">{{ scope.row.statusStr }}</span>
            </template>
          </el-table-column>
          <el-table-column label="出库时间" prop="outTime" width="150" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.outTime,"{y}-{m}-{d} {h}:{i}") }}</span>
            </template>
          </el-table-column>
          <el-table-column label="归属人" prop="belongName" width="100" show-overflow-tooltip/>
          <el-table-column label="操作人" prop="outUserName" width="100" show-overflow-tooltip/>
          <el-table-column label="双人认证" prop="content" width="180" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.outOneUserName}} {{scope.row.outTwoUserName}}</span>
            </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>
    <!--<add-page :propsData="propsData" v-if="pageType === 2"></add-page>-->
  </div>
</template>
<script>
  import exportComponent from "@/components/exportComponent/exportComponent.vue";
  import { getDeptDropList,systemBuildingGetTreeList,} from "@/api/commonality/permission";
  import { chemicalCabinetSelect, chemicalStockListOut } from '@/api/chemicalManage'
  import { getDicts } from '@/api/commonality/noPermission'
  export default {
    name: 'index',
    components: {
      exportComponent
    },
    data () {
      return {
        //导出配置
        exportConfig:{
          api:'chemical/stock/exportOut',             //导出接口地址
          ids:'',                                       //勾选导出,勾选的IDS
          fileName:'出库记录',                         //导出文件的命名
        },
        tableButtonType:this.hasPermiDom(['demo:demo:detail','demo:demo:edit','demo:demo:del',]),
        //页面状态
        pageType:1,
        //页面遮罩
        loading:false,
        //学院
        deptOptions:[],
        //楼栋楼层
        buildFloorOptions:[],
        //搜索化学品柜
        searchOptions:[],
        categoryOptions:[],
        //下拉列表数据
        optionList:[{value:2,label:'空瓶出库'},{value:3,label:'废弃出库'},{value:4,label:'整瓶领用'}],
        //查询条件
        queryParams:{
          page:1,
          pageSize:20,
          // flg :'ck',
          deptId :null,
          buildId :null,
          buildIdAndfloorId :[],
          searchValue:'',
          cabinetId:'',
          status:'',
        },
        //时间数据
        dateRange:[],
        //列表数据
        dataList:[{},{}],
        //数据数量
        total:10,
        // 选中数组
        selectedNum:0,
        ids:[],
        multiple:true,
        //组件传参
        propsData:{},
      }
    },
    created () {

    },
    mounted () {
      this.getDeptDropList();
      this.systemBuildingGetTreeList();
      this.getDicts();
      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.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //重置按钮
      resetQuery(){
        this.$set(this,'dateRange',[])
        this.$set(this,'queryParams',{
          page:1,
          pageSize:20,
          flg :'ck',
          deptId :null,
          buildId :null,
          floorId :null,
          buildIdAndfloorId :[],
          searchValue:'',
          cabinetId:'',
          status:'',
        });
        this.$refs['table-box'].clearSelection();
        this.$set(this.exportConfig,'ids','');
        this.getList();
      },
      //获取化学品类别
      getDicts(){
        getDicts('chemicals_category').then(response => {
          this.$set(this,'categoryOptions',response.data);
        });
      },
      //获取数据列表
      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 = "";
        }
        chemicalStockListOut(obj).then(response => {
          this.$set(this,'loading',false);
          this.$set(this,'dataList',response.data.records);
          this.$set(this,'total',response.data.total);
        });
      },
      //查询学院列表
      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
      },
      //搜索化学品柜
      searchSelectList(query) {
        if (query !== '' && query.length>1) {
          this.loading = true;
          let obj = {
            userName : query,
            userType: 2,
            pageSize: -1,
          };
          chemicalCabinetSelect({searchValue:query}).then(response => {
            this.searchOptions = response.data;
            this.loading = false;
          });
        } else {
          this.searchOptions = [];
        }
      },
      //化学品柜选中
      //人员选中
      selectChange(data){
        let self = this;
        // for(let i=0;i<self.optionsUser.length;i++){
        //   if(data == self.optionsUser[i].userId){
        //     this.form.stuNo = self.optionsUser[i].account;
        //     this.form.deptId = self.optionsUser[i].deptId;
        //     this.form.deptName = self.optionsUser[i].deptName;
        //     this.form.joinUserId = self.optionsUser[i].userId;
        //     this.form.joinUserName = self.optionsUser[i].userName;
        //     this.form.subjectId = '';
        //     this.laboratorySubRelInfoList();
        //   }
        // }
      },
      /*===记录勾选数据===
        需要再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">
  .takeRecord{
    .takeRecordPage{

    }
  }
</style>
