<template>
  <el-dialog
    v-dialogDrag
    title="货位调整"
    :visible.sync="open"
    width="1200px"
    append-to-body
  >
    <el-form
      ref="ruleForm"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      class="form-container"
    >
      <el-row :gutter="20">
        <el-col :span="12"
          ><el-form-item label="调拨类型" prop="transferTypeCode">
            <el-select
              v-model="formData.transferType"
              placeholder="请选择调拨类型"
              @change="selectDict"
            >
              <el-option
                v-for="dict in getDictDatas('erp_move_out_type')"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value + '-' + dict.label"
              />
            </el-select> </el-form-item
        ></el-col>

        <el-col :span="12">
          <el-form-item label="调入仓库" prop="stockIdNew">
            <StockSelect
              :ref="`stock`"
              v-model="formData.stockIdNew"
              filterable
              @change="
                ($event) => {
                  stockChange($event);
                }
              "
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调入区域" prop="areaIdNew">
            <AreaSelect
              :ref="`area`"
              v-model="formData.areaIdNew"
              filterable
              @change="
                ($event) => {
                  areaChange($event);
                }
              "
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="调入货位" prop="locationIdNew">
            <LocationSelect
              :ref="`location`"
              v-model="formData.locationIdNew"
              filterable
              @change="
                ($event) => {
                  locationChange($event);
                }
              "
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <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="erpStockId"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="原区域"
        align="center"
        prop="wmsStoreAreaId"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="原货位"
        align="center"
        prop="wmsStoreId"
        width="120"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="调入仓库"
        align="center"
        prop="stockIdNew"
        width="160"
      >
        <template slot-scope="scope">
          <StockSelect
            :ref="`stock-${scope.$index}`"
            v-model="scope.row.stockIdNew"
            filterable
            @change="
              ($event) => {
                stockChange($event, scope.$index);
              }
            "
          />
        </template>
      </el-table-column>
      <el-table-column
        label="调入区域"
        align="center"
        prop="areaIdNew"
        width="120"
      >
        <template slot-scope="scope">
          <AreaSelect
            :ref="`area-${scope.$index}`"
            v-model="scope.row.areaIdNew"
            filterable
            @change="
              ($event) => {
                areaChange($event, scope.$index);
              }
            "
          />
        </template>
      </el-table-column>
      <el-table-column
        label="调入货位"
        align="center"
        prop="locationIdNew"
        width="120"
      >
        <template slot-scope="scope">
          <LocationSelect
            :ref="`location-${scope.$index}`"
            v-model="scope.row.locationIdNew"
            filterable
            @change="
              ($event) => {
                locationChange($event, scope.$index);
              }
            "
          />
        </template>
      </el-table-column> -->
      <el-table-column label="操作人" align="center" prop="creator" />
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        :loading="loading"
        @click="handleAdjustLocation"
      >
        完成</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { stockTransfer } 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 {
      formData: {
        transferTypeCode: undefined,
        stockIdNew: undefined,
        areaIdNew: undefined,
        locationIdNew: undefined,
      },
      // 表单校验
      formRules: {
        transferTypeCode: [
          { required: true, message: "请选择调拨类型", trigger: "blur" },
        ],
        stockIdNew: [
          {
            required: true,
            message: "请选择调入仓库",
            trigger: "blur",
          },
        ],
        areaIdNew: [
          {
            required: true,
            message: "请选择调入区域",
            trigger: "blur",
          },
        ],
        locationIdNew: [
          {
            required: true,
            message: "请选择调入货位",
            trigger: "blur",
          },
        ],
      },
      // 遮罩层
      loading: false,
      // WMS库内业务-货位调整列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialLots: null,
        materialNo: null,
        materialName: null,
        inQty: null,
        erpStockId: null,
        storeId: null,
        stockIdNew: null,
        locationIdNew: null,
        remark: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialLots: [
          { required: true, message: "物料批次不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    open: {
      handler(val, oldVal) {
        if (val) {
          this.$nextTick(async () => {
            for (let x = 0; x < this.currentRows.length; x++) {
              const { stockCode, stockIdOld } = this.currentRows[x];
              await this.$refs[`stock`].fetchStockList();
              // this.stockChange({ id: Number(stockIdOld), code: stockCode }, 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,
        stockIdNew: undefined,
        locationIdNew: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    stockChange(item, index) {
      if (item) {
        const { id, code } = item;
        this.$refs[`stock`].value = Number(id);
        this.formData.stockCodeNew = code;
        this.formData.stockIdNew = id;

        this.$refs[`area`].value = "";
        this.$refs[`area`].options = [];
        this.$refs[`area`].fetchAreaList(id);
        this.$refs[`location`].options = [];
        this.$refs[`location`].value = "";
      }
    },
    areaChange(item, index) {
      if (item) {
        const { id, code } = item;
        this.formData.areaCodeNew = code;
        this.$refs[`location`].options = [];
        this.$refs[`location`].fetchLocationList(id);
      }
    },
    locationChange(item, index) {
      if (item) {
        const { code } = item;
        this.formData.locationCodeNew = code;
      }
    },
    selectDict(value) {
      let data = value.split("-");
      this.formData.transferTypeCode = data[0];
      this.formData.transferTypeName = data[1];
    },
    // 调整货位
    async handleAdjustLocation() {
      // 校验主表
      await this.$refs["ruleForm"].validate();
      let data = this.currentRows.map((v) => {
        return { ...v, ...this.formData };
      });
      const isEmpty = data.every(
        (item) => item.stockIdNew && item.areaIdNew && item.locationIdNew
      );
      if (!isEmpty) {
        this.$modal.msgWarning("仓库/区域/货位不能为空");
        return;
      }

      this.loading = true;
      stockTransfer(data)
        .then((res) => {
          this.$modal.msgSuccess("调拨成功");
          this.open = false;
          this.loading = false;
          this.$emit("complete", true);
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>
