<template>
  <el-dialog
    v-dialogDrag
    title="货位调整"
    :visible.sync="open"
    width="1200px"
    append-to-body
  >
    <!-- 列表 -->
    <el-table v-loading="loading" :data="currentRows" border>
      <el-table-column
        label="物料批次"
        align="center"
        prop="materialLots"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料编码"
        align="center"
        prop="materialNo"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column label="数量" align="center" prop="inQty" />
      <el-table-column label="物料来源" align="center" prop="sourceType" />
      <el-table-column
        label="原仓库"
        align="center"
        prop="erpStockName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="原区域"
        align="center"
        prop="wmsStoreAreaName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="原货位"
        align="center"
        prop="storeName"
        width="120"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="调整后仓库"
        align="center"
        prop="erpStockIdNew"
        width="160"
      >
        <template slot-scope="scope">
          <StockSelect
            :ref="`stock-${scope.$index}`"
            v-model="scope.row.erpStockIdNew"
            filterable
            @change="($event) => { stockChange($event, scope.$index) }"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        label="调整后区域"
        align="center"
        prop="wmsStoreAreaIdNew"
        width="120"
      >
        <template slot-scope="scope">
          <AreaSelect
            :ref="`area-${scope.$index}`"
            v-model="scope.row.wmsStoreAreaIdNew"
            filterable
            @change="
              ($event) => {
                areaChange($event, scope.$index);
              }
            "
          />
        </template>
      </el-table-column>
      <el-table-column
        label="调整后货位"
        align="center"
        prop="storeIdNew"
        width="120"
      >
        <template slot-scope="scope">
          <LocationSelect
            :ref="`location-${scope.$index}`"
            v-model="scope.row.storeIdNew"
            filterable
          />
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="creator" />
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleAdjustLocation"> 完成</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { LocationAdjustment } from "@/api/wms/inLibrary/locationAdjustment";
import StockSelect from "../../incoming/wareHousing/components/StockSelect.vue";
import AreaSelect from "../../incoming/wareHousing/components/AreaSelect.vue";
import LocationSelect from "../../incoming/wareHousing/components/LocationSelect.vue";

export default {
  name: "LocationAdjustment",
  components: {
    StockSelect,
    AreaSelect,
    LocationSelect,
  },
  props: {
    currentRows: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // WMS库内业务-货位调整列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialLots: null,
        materialNo: null,
        materialName: null,
        inQty: null,
        erpStockId: null,
        storeId: null,
        erpStockIdNew: null,
        storeIdNew: null,
        remark: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialLots: [
          { required: true, message: "物料批次不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    open: {
      handler(val, oldVal) {
        if (val) {
          this.$nextTick(() => {
            for (let x = 0; x < this.currentRows.length; x++) {
              const { erpStockId } = this.currentRows[x];
              this.currentRows[x].erpStockIdNew = erpStockId;
              this.stockChange({ id: erpStockId }, x);
            }
          });
        }
      },
    },
  },
  methods: {
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        materialLots: undefined,
        materialNo: undefined,
        materialName: undefined,
        inQty: undefined,
        erpStockId: undefined,
        storeId: undefined,
        erpStockIdNew: undefined,
        storeIdNew: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    stockChange(item, index) {
      if (item) {
        const { id } = item;
        this.$refs[`area-${index}`].options = [];
        this.$refs[`area-${index}`].fetchAreaList(id);
      }
    },
    areaChange(item, index) {
      if (item) {
        const { id } = item;
        this.$refs[`location-${index}`].options = [];
        this.$refs[`location-${index}`].fetchLocationList(id);
      }
    },
    // 调整货位
    handleAdjustLocation() {
      console.log("this.currentRows", this.currentRows);
      const isEmpty = this.currentRows.every(
        (item) =>
          item.erpStockIdNew && item.wmsStoreAreaIdNew && item.storeIdNew
      );
      if (!isEmpty) {
        this.$modal.msgWarning("仓库/区域/货位不能为空");
        return;
      }
      LocationAdjustment(this.currentRows).then((res) => {
        this.$modal.msgSuccess("调整货位成功");
        this.open = false;
        this.$emit("complete");
      });
    },
  },
};
</script>
