<template>
  <el-dialog
    :visible.sync="visible"
    title="仓库"
    @before-close="cancel"
    width="50%"
    :close-on-click-modal="false"
  >
    <el-form :model="formData" label-width="100px">
      <el-row>
        <el-col :span="9">
          <el-form-item label="选择仓库">
            <el-select filterable v-model="warehouseId">
              <el-option
                v-for="(item, index) in warehouseList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6">
          <el-button type="primary" @click="addTableData">添加</el-button>
        </el-col>
        <el-col :span="6">
          <el-form-item label="总包装数量">
            <span class="red_color">
              {{ this.packingQuantity }}
            </span></el-form-item
          >
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="tableDate" style="width: 100%">
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column label="仓库名称" prop="warehouseName"></el-table-column>
      <el-table-column label="包装数量" prop="packingQuantity" width="300">
        <template slot-scope="{ row, $index }">
          <el-input-number
            v-model="row.packingQuantity"
            :min="1"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column width="120" label="操作" fixed="right">
        <template slot-scope="{ row, $index }">
          <el-button type="text" @click="delItem($index)" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <template slot="footer">
      <el-button @click="handleSelect" type="primary">确认</el-button>
      <el-button @click="cancel">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import warehouseDefinition from '@/api/warehouseManagement/warehouseDefinition';
  export default {
    data() {
      return {
        packingQuantity: 1,
        tableDate: [],
        visible: false,
        warehouseList: [], //仓库
        formData: {},
        warehouseId: '',
        currentIndex: 0
      };
    },
    methods: {
      numberChange() {},
      cancel() {
        this.visible = false;
      },
      handleSelect() {
        if (this.tableDate.length == 0) {
          return this.$message.warning('请添加仓库！');
        }
        let total = this.tableDate.reduce(
          (total, currentObj) =>
            Number(total) + Number(currentObj.packingQuantity),
          0
        );
        if (total == this.packingQuantity) {
          this.$emit('selection', this.tableDate, this.currentIndex);
          this.visible = false;
        } else {
          this.$message.warning('总包装数量与仓库包装数量不一致！');
        }
      },
      addTableData() {
        if (this.tableDate.length < this.packingQuantity) {
          let list = this.tableDate.filter(
            (item) => item.warehouseId == this.warehouseId
          );
          if (list.length > 0) {
            return this.$message.warning('该仓库已存在！');
          }
          this.tableDate.push({
            packingQuantity: this.packingQuantity,
            warehouseName: this.warehouseList.filter(
              (item) => item.id == this.warehouseId
            )[0].name,
            warehouseId: this.warehouseId
          });
        } else {
          this.$message.warning('仓库数量已满！');
        }
      },
      delItem(index) {
        if (index == 0 && this.tableDate.length == 1) {
          return this.$message.warning('请至少保存一条数据！');
        }
        this.tableDate.splice(index, 1);
      },
      async getWarehouseList() {
        const res = await warehouseDefinition.list({});
        console.log(res);
        this.warehouseList = res.map((item) => {
          return { ...item, name: item.factoryName + '-' + item.name };
        });
      },
      open(packingQuantity, idx, warehouseId, warehouseName) {
        console.log('仓库id', warehouseId, warehouseName);

        this.warehouseId = '';
        this.tableDate = [];
        this.currentIndex = idx;
        this.packingQuantity = packingQuantity;
        this.visible = true;
        this.getWarehouseList();
        if (warehouseId && warehouseName) {
          this.warehouseId = warehouseId;
          this.tableDate.push({
            packingQuantity: packingQuantity,
            warehouseName: warehouseName,
            warehouseId: warehouseId
          });
        }
      }
    }
  };
</script>

<style lang="scss" scoped>
  .red_color {
    font-size: 20px;
    font-weight: bold;
    color: red;
  }
</style>
