<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="8">
          <el-form-item label="选择仓库">
            <el-select
              filterable
              v-model="warehouseId"
              @change="changeWarehouse"
            >
              <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="4" style="margin-top: 3px">
          <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-col :span="6">
          <el-form-item label="计量数量">
            <span class="red_color">
              {{ this.measureQuantity }}
            </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">
        <template slot-scope="{ row, $index }">
          <el-input-number
            v-model="row.packingQuantity"
            :min="0"
            size="mini"
            disabled
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="计量数量" prop="measureQuantity">
        <template slot-scope="{ row, $index }">
          <el-input-number
            v-model="row.measureQuantity"
            :min="0"
            size="mini"
            disabled
          ></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: 0,
        tableDate: [],
        visible: false,
        warehouseList: [], //仓库
        formData: {},
        warehouseId: '',
        currentIndex: 0,
        measureQuantity: 0
      };
    },
    methods: {
      changeWarehouse() {
        this.tableDate = [];
      },
      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) {
          console.log(this.tableDate, 'kkkkkkkkkkkkkkkkkkkkkkkkkkkkk');
          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({
          measureQuantity: this.measureQuantity,
          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(categoryType) {
        let params = {
          inventoryType: categoryType
        };

        if (this.$store.state.user.info.factoryId) {
          params['factoryId'] = this.$store.state.user.info.factoryId;
        }
        const res = await warehouseDefinition.list(params);
        this.warehouseList = res.map((item) => {
          return { ...item, name: item.name };
        });
      },
      async getBatchWarehouseList(batch) {
        let params = {};

        if (this.$store.state.user.info.factoryId) {
          params['factoryId'] = this.$store.state.user.info.factoryId;
        }
        if (batch?.length == 1) {
          params['inventoryType'] = batch[0];
        }

        const res = await warehouseDefinition.list(params);
        // if (!batch.length) {
        //   this.warehouseList = res.map((item) => {
        //     return { ...item, name: item.name };
        //   });
        //   return;
        // }

        // let filter = res.filter((item) => batch.includes(item.inventoryType));
        this.warehouseList = res.map((item) => {
          return { ...item, name: item.name };
        });
      },
      open({
        packingQuantity,
        measureQuantity,
        idx,
        warehouseId,
        warehouseName,
        categoryType,
        batch
      } = obj) {
        console.log(
          '仓库id',
          packingQuantity,
          measureQuantity,
          warehouseId,
          warehouseName
        );

        this.warehouseId = '';
        this.tableDate = [];
        this.currentIndex = idx;
        this.packingQuantity = packingQuantity;
        this.measureQuantity = measureQuantity;
        this.visible = true;

        console.log(batch);

        if (!batch) {
          this.getWarehouseList(categoryType);
        } else {
          this.getBatchWarehouseList(batch);
        }

        // this.getWarehouseList(categoryType);
        if (warehouseId && warehouseName) {
          this.warehouseId = warehouseId;
          this.tableDate.push({
            packingQuantity: packingQuantity,
            measureQuantity: measureQuantity,
            warehouseName: warehouseName,
            warehouseId: warehouseId
          });
        }
      }
    }
  };
</script>

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