<template>
  <div class="app-container" id="inStoreBook">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable maxlength="50" 
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择单位名称"  clearable>
          <el-option
            v-for="item in unitList" filterable 
            :key="item.id" :label="item.name" :value="item.id" 
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工程名称" prop="proId">
        <el-select v-model="queryParams.proId" placeholder="请选择工程名称"  clearable>
          <el-option
            v-for="item in proList" filterable 
            :key="item.id" :label="item.name" :value="item.id" 
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型名称" prop="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="请选择类型名称"
          clearable filterable
          style="width: 240px">
          <el-option
            v-for="typeItem in typeList"
            :key="typeItem.typeId"
            :label="typeItem.typeName"
            :value="typeItem.typeId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="规格型号" prop="modelId">
        <treeselect 
          v-model="queryParams.modelId" 
          default-expand-all :options="equipmentTypeList" 
          placeholder="请选择规格型号" 
          
          :disable-branch-nodes="true"
          style="width: 240px;"  
          noChildrenText="没有数据了"
          noOptionsText="没有数据"
          noResultsText="没有搜索结果"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:labelType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="单位名称" align="center" prop="unitName" :show-overflow-tooltip="true" />
      <el-table-column label="工程名称" align="center" prop="proName" :show-overflow-tooltip="true" />
      <el-table-column label="类型名称" align="center" prop="typeName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="modelName" :show-overflow-tooltip="true" />
      <el-table-column label="退库数量" align="center" prop="num" :show-overflow-tooltip="true" />
      <el-table-column label="退料人员" align="center" prop="leaseMan" :show-overflow-tooltip="true" />
      <el-table-column label="退库人员" align="center" prop="createBy" :show-overflow-tooltip="true" />
      <el-table-column label="退库时间" align="center" prop="createTime" :show-overflow-tooltip="true" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { getOperateList  } from "@/api/claimAndRefund/secondStore";
import { getUnitData,getProData } from "@/api/claimAndRefund/receive.js"
import { getTypeList } from "@/api/store/warehousing";
import { equipmentTypeTree } from "@/api/store/tools";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "InStoreBook",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openOut: false,
      openIn: false,
      // 日期范围
      dateRange: [],
        // 单位数据
        unitList: [],
        proList: [],
      //搜索下拉数据
      typeList: [],
      equipmentTypeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type:2,
        unitId: undefined,
        modelId: undefined,
        typeId: undefined
      }
    };
  },
  created() {
    this.getTypeList();
    this.getUnitList();
    this.getProList();
    this.equipmentType();
    this.getList();
  },
  methods: {
    //获取单位
    getUnitList(){
      getUnitData().then(response => {
        this.unitList =  response.data;
        })
    },
    //获取工程
    getProList(){
      getProData().then(response => {
        this.proList =  response.data;
        })
    },
    getTypeList() {
      getTypeList({level:'3'}).then(response => {
          this.typeList = response.data;
        }
      )
    },
    //规格型号
    equipmentType() {
      equipmentTypeTree().then(response => {
        this.equipmentTypeList = response.data;
        this.equipmentTypeList.forEach((item,index)=>{
          if(item.children&&item.children.length>0){
            item.children.forEach((item2,index2)=>{
              if(item2.children&&item2.children.length>0){
                item2.children.forEach(item3=>{
                  if(item3.children&&item3.children.length>0){
                    item3.children.forEach(item4=>{ 
                      item4.machineTypeName = item3.typeName
                      item4.specificationType = item4.typeName
                      this.$set(item4, 'purchasePrice', 0);
                      this.$set(item4, 'purchaseNum', 1);
                    })
                  }
                })
              }
            })
          }
        })
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getOperateList(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.downloadJson('material/secondaryWarehouse/exportIntOperateList',JSON.stringify(this.queryParams), `回库台账_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
