<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入物料"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位" prop="locationNo">
        <el-input
          v-model="queryParams.locationNo"
          placeholder="请输入库位编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdj"
          v-hasPermi="['ams:invLotLocId:adj']"
        >库存调整
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleMove"
          v-hasPermi="['ams:invLotLocId:move']"
        >库存移动
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ams:invLotLocId:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ams:invLotLocId:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ams:invLotLocId:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ams:invLotLocId:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="invLotLocIdList" @selection-change="handleSelectionChange">
      <!--<el-table-column label="lotnum" align="center" prop="lotnum" width="200"/>-->
      <el-table-column type="selection" />
      <el-table-column label="物料" align="center" prop="sku" width="200"/>
      <el-table-column label="物料名称" align="center" prop="skuName" width="200"/>
      <el-table-column label="库位" align="center" prop="locationNo" width="200"/>
      <el-table-column label="库存数量" align="center" prop="qty"/>
      <el-table-column label="已分配数量" align="center" prop="qtyallocated" width="200"/>
      <el-table-column
        v-for="(item, index) in tableOption"
        :key="index"
        :label="item.label"
        align="center" :prop="item.prop" width="200">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ams:invLotLocId:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ams:invLotLocId:remove']"
          >删除</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="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="跟踪号" prop="traceid">
          <el-input v-model="form.traceid" placeholder="请输入跟踪号" />
        </el-form-item>
        <el-form-item label="库存数量(件)" prop="qty">
          <el-input v-model="form.qty" placeholder="请输入库存数量(件)" />
        </el-form-item>
        <el-form-item label="库存数量" prop="qtyEach">
          <el-input v-model="form.qtyEach" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="分配数量" prop="qtyallocated">
          <el-input v-model="form.qtyallocated" placeholder="请输入分配数量" />
        </el-form-item>
        <el-form-item label="库存数量(件)" prop="qtyallocatedEach">
          <el-input v-model="form.qtyallocatedEach" placeholder="请输入库存数量(件)" />
        </el-form-item>
        <el-form-item label="上架数量" prop="qtypa">
          <el-input v-model="form.qtypa" placeholder="请输入上架数量" />
        </el-form-item>
        <el-form-item label="${comment}" prop="qtyrpin">
          <el-input v-model="form.qtyrpin" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="qtyrpout">
          <el-input v-model="form.qtyrpout" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="qtymvin">
          <el-input v-model="form.qtymvin" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="qtymvout">
          <el-input v-model="form.qtymvout" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="qtyonhold">
          <el-input v-model="form.qtyonhold" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 库存调整dialog -->
    <adj-dialog  ref="adjDialog" @getList="getList()"></adj-dialog>
  </div>
</template>

<script>
import { listInvLotLocId, getInvLotLocId, adjInvLotLocId, moveInvLotLocId, delInvLotLocId, addInvLotLocId, updateInvLotLocId } from "@/api/ams/invLotLocId";
import { lotattConfigList } from "@/api/ams/config";
import adjDialog from "@/views/ams/inv/invLotLocId/adjDialog";

export default {
  name: "InvLotLocId",
  components: { adjDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的数据
      tableSelection: [],
      // 是否勾选
      isSelect: false,
      // 非单个禁用
      single: false,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库位库存信息表格数据
      invLotLocIdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        locationNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tableOption: []
    };
  },
  created() {
    this.initLotatt();
    this.getList();
  },
  methods: {
    initLotatt() {
      lotattConfigList().then(response => {
        let data = response.data
        for(let i=0;i<data.length;i++) {
          let obj = new Object();
          obj.label = data[i].lotattName
          obj.prop = data[i].lotattId
          this.tableOption.push(obj)
        }
      })
    },
    /** 查询库位库存信息列表 */
    getList() {
      this.loading = true;
      listInvLotLocId(this.queryParams).then(response => {
        this.invLotLocIdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lotnum: null,
        locationId: null,
        traceid: null,
        customerId: null,
        sku: null,
        qty: null,
        qtyEach: null,
        qtyallocated: null,
        qtyallocatedEach: null,
        qtypa: null,
        qtyrpin: null,
        qtyrpout: null,
        qtymvin: null,
        qtymvout: null,
        qtyonhold: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lotnum)
      this.tableSelection = selection;
      this.isSelect =  selection.length > 0;
      this.single = selection.length == 1;
      this.multiple = selection.length > 1;

    },
    /** 库存调整操作 */
    handleAdj() {
      if (!this.isSelect) {
        this.$modal.alertWarning("请选择一条！");
        return;
      }
      if (!this.single) {
        this.$modal.alertWarning("库存调整只能选择单条！");
        return;
      }
      this.$nextTick(() => {
        this.$refs.adjDialog.show(this.tableSelection);
      });
    },
    /** 库存移动操作 */
    handleMove() {

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位库存信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lotnum = row.lotnum || this.ids
      getInvLotLocId(lotnum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位库存信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.lotnum != null) {
            updateInvLotLocId(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvLotLocId(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lotnums = row.lotnum || this.ids;
      this.$modal.confirm('是否确认删除库位库存信息编号为"' + lotnums + '"的数据项？').then(function() {
        return delInvLotLocId(lotnums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ams/invLotLocId/export', {
        ...this.queryParams
      }, `invLotLocId_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
