<template>
  <div class="app-container" id="secondStore">
    <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
          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="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="deviceList" @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="typeName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="modelName" :show-overflow-tooltip="true" />
      <el-table-column label="进场数量" align="center" prop="jcNum" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!-- <span class="clickText" @click="openRecords">{{scope.row.jcNum}}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="退场数量" align="center" prop="tcNUm" :show-overflow-tooltip="true" />
      <el-table-column label="场内库存量" align="center" prop="kcNum" :show-overflow-tooltip="true" />
      <el-table-column label="已出库数量" align="center" prop="ckNum" :show-overflow-tooltip="true" />
      <el-table-column label="在库数量" align="center" prop="zkNum" :show-overflow-tooltip="true" />
      <el-table-column label="计量单位" align="center" prop="nuitName" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleOut(scope.row)"
          >出库</el-button>
          <el-button
            size="mini"
            type="warning"
            @click="handleIn(scope.row)"
          >退库</el-button>
        </template>
      </el-table-column>
    </el-table>

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

      <!-- 出库弹窗 -->
    <el-dialog :title="title" :visible.sync="openOut" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="类型名称:" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称" disabled/>
        </el-form-item>
        <el-form-item label="规格型号" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入规格型号" disabled/>
        </el-form-item>
        <el-form-item label="在库数量" prop="zkNum">
          <el-input v-model="form.zkNum" placeholder="请输入在库数量" disabled/>
        </el-form-item>

        <el-form-item label="出库数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入出库数量" />
        </el-form-item>
        <el-form-item label="班组名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="领用人员" prop="leaseMan">
          <el-input v-model="form.leaseMan" placeholder="请输入领用人员" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm(1)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 进库弹窗 -->
    <el-dialog :title="title" :visible.sync="openIn" width="600px" append-to-body>
      <el-form ref="nform" :model="nform" :rules="nrules" label-width="120px">
        <el-form-item label="类型名称:" prop="typeName">
          <el-input v-model="nform.typeName" placeholder="请输入类型名称" disabled/>
        </el-form-item>
        <el-form-item label="规格型号" prop="modelName">
          <el-input v-model="nform.modelName" placeholder="请输入规格型号" disabled/>
        </el-form-item>

        <el-form-item label="退库数量" prop="num">
          <el-input v-model="nform.num" placeholder="请输入退库数量"/>
        </el-form-item>
        <el-form-item label="班组名称" prop="teamName">
          <el-input v-model="nform.teamName" placeholder="请输入班组名称" />
        </el-form-item>
        <el-form-item label="退库人员" prop="leaseMan">
          <el-input v-model="nform.leaseMan" placeholder="请输入退库人员" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="nform.phone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm(2)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <!-- 记录弹窗 -->
     <el-dialog :title="title" :visible.sync="openIn" width="600px" append-to-body>
      

    </el-dialog>

  </div>
</template>

<script>
import { secondaryWarehouseList,operate,getRecords  } from "@/api/claimAndRefund/secondStore";
import { getUnitData } 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: "SecondStore",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openOut: false,
      openIn: false,
      // 日期范围
      dateRange: [],
      // 单位数据
      unitList: [],
      //搜索下拉数据
      typeList: [],
      equipmentTypeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
       
        unitId: undefined,
        modelId: undefined,
        typeId: undefined
      },
      // 表单参数 出库
      form: {},
      // 表单校验
      rules: {
        num: [
          { required: true, message: "出库数量不能为空", trigger: "blur" }
        ],
        teamName: [
          { required: true, message: "班组名称不能为空", trigger: "blur" }
        ]
      },
      // 表单参数 退库
      nform:{},
      nrules: {
        num: [
          { required: true, message: "退库数量不能为空", trigger: "blur" }
        ],
        teamName: [
          { required: true, message: "班组名称不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getTypeList();
    this.getUnitList();
    this.equipmentType();
    
    this.getList();
  },
  methods: {
    //获取单位
    getUnitList(){
      getUnitData().then(response => {
        this.unitList =  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.length>0){
            item.children.forEach((item2,index2)=>{
              if(item2.children.length>0){
                item2.children.forEach(item3=>{
                  if(item3.children.length>0){
                    item3.children.forEach(item4=>{ 
                      item4.machineTypeName = item3.typeName
                      item4.specificationType = item4.typeName
                      // item4.purchasePrice = 1
                      // item4.purchaseNum = 1
                      this.$set(item4, 'purchasePrice', 0);
                      this.$set(item4, 'purchaseNum', 1);
                    })
                  }
                })
              }
            })
          }
        })
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      secondaryWarehouseList(this.queryParams).then(response => {
          this.deviceList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.openOut = false;
      this.openIn = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
       
      };
      this.resetForm("form");
      this.nform = {
     
      };
      this.resetForm("nform");
    },
    /** 搜索按钮操作 */
    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
    },
    /** 提交按钮 */
    submitForm: function(type) {
      if(type==1){
        this.$refs["form"].validate(valid => {
          if (valid) {
            let param = {}
            param = this.form
            param.type=1
            operate(param).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openOut = false;
              this.getList();
            });
          }
        });
      }
      if(type==2){
        this.$refs["nform"].validate(valid => {
          if (valid) {
            let param = {}
            param = this.nform
            param.type=2
            operate(param).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openIn = false;
              this.getList();
            });
          }
        });
      }
    },
    /** 按钮操作 */
    handleOut(row) {//1
      this.reset();
      this.form = row;
      console.log(this.form)
      this.openOut = true;
      this.title = "出库";
    },
    handleIn(row) {//2
      this.reset();
      this.nform = row;
      this.openIn = true;
      this.title = "退库";
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep.el-table .fixed-width .el-button--mini {
  width: 60px !important;
  margin-bottom: 10px;
}
.clickText{
  color: #02A7F0;
  cursor: pointer;
}
</style>