<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="open"
    width="1200px"
    append-to-body
  >
    <div class="app-container">
      <!--操作 -->
      <el-row :gutter="10" class="mb10 fr">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-check"
            :loading="ensureLoading"
            @click="handleSure"
          >
            确认入库</el-button
          >
        </el-col>
      </el-row>
      <UTable
        v-loading="loading"
        :data="list"
        size="mini"
        use-virtual
        :row-height="30"
        :height="400"
        border
      >
        <UTableColumn
          label="选择仓库"
          align="center"
          width="160"
          prop="wmsStockName"
        >
          <template slot-scope="scope">
            <StockSelect
              :ref="`stock-${scope.$index}`"
              v-model="scope.row.selectedStockId"
              filterable
              @change="
                ($event) => {
                  stockChange($event, scope.$index, scope.row);
                }
              "
            />
          </template>
        </UTableColumn>
        <UTableColumn label="选择区域" align="center" prop="wmsStoreAreaName">
          <template slot-scope="scope">
            <AreaSelect
              :ref="`area-${scope.$index}`"
              v-model="scope.row.selectedStoreAreaId"
              filterable
              @change="
                ($event) => {
                  areaChange($event, scope.$index, scope.row);
                }
              "
            />
          </template>
        </UTableColumn>
        <UTableColumn
          label="选择货位"
          align="center"
          prop="wmsStockLocationName"
        >
          <template slot-scope="scope">
            <LocationSelect
              :ref="`location-${scope.$index}`"
              v-model="scope.row.selectedStockLocationId"
              filterable
              @change="locationChange($event, scope.row)"
            />
          </template>
        </UTableColumn>
        <UTableColumn
          label="物料批次"
          align="center"
          width="150"
          prop="materialBatch"
          show-overflow-tooltip
        />
        <UTableColumn
          label="物料名称"
          align="center"
          width="120"
          prop="materialName"
          show-overflow-tooltip
        />
        <UTableColumn
          label="物料编码"
          align="center"
          width="120"
          prop="materialNo"
          show-overflow-tooltip
        />
        <UTableColumn
          label="入库数量"
          align="center"
          prop="rejectOrderNo"
          width="100"
        />
      </UTable>
    </div>
  </el-dialog>
</template>

<script>
import {
  createBatchIncomingRejectStock,
  getIncomingRejectDetailPage,
} from "@/api/wms/incoming/reject";
import StockSelect from "../../wareHousing/components/StockSelect.vue";
import AreaSelect from "../../wareHousing/components/AreaSelect.vue";
import LocationSelect from "../../wareHousing/components/LocationSelect.vue";
import { UTable, UTableColumn } from "umy-ui";

export default {
  name: "WareHousingPlan",
  components: {
    StockSelect,
    AreaSelect,
    LocationSelect,
    UTable,
    UTableColumn,
  },
  props: {
    warehousingList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      ensureLoading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: null,
        rejectNo: null,
        supplierCode: null,
        materialNo: null,
        receiveOrderNo: null,
        sendOrderNo: null,
        sendQty: null,
        rejectOrderNo: null,
        erpStockId: null,
        wmsStoreAreaId: null,
        wmsStoreId: null,
        inQty: null,
        createTime: [],
      },
      list: [],
      total: 0,
    };
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getIncomingRejectDetailPage(this.queryParams)
        .then((response) => {
          this.list = response.data.list;

          // this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 完成入库
    handleSure() {
      const isEmpty = this.list.every(
        (item) =>
          item.selectedStockId &&
          item.selectedStoreAreaId &&
          item.selectedStockLocationId
      );
      if (this.list.length === 0) {
        this.$modal.msgWarning("暂无入库数据，无法入库");
        return;
      }
      if (!isEmpty) {
        this.$modal.msgWarning("仓库/区域/货位不能为空");
        return;
      }
      this.ensureLoading = true;
      const rejectList = this.list.map((item) => {
        const {
          selectedStockId: erpStockId,
          selectedStoreAreaId: wmsStoreAreaId,
          selectedStockLocationId: wmsStoreId,
          inQty,
          rejectNo,
          mesDetailId,
          supplierCode,
          materialNo,
          receiveOrderNo,
          sendOrderNo,
          sendQty,
          rejectOrderNo,
        } = item;
        return {
          erpStockId,
          wmsStoreAreaId,
          wmsStoreId,
          inQty,
          rejectNo,
          mesDetailId,
          supplierCode,
          materialNo,
          receiveOrderNo,
          sendOrderNo,
          sendQty,
          rejectOrderNo,
        };
      });
      createBatchIncomingRejectStock(rejectList)
        .then((res) => {
          this.$modal.msgSuccess("入库成功");
          this.open = false;
          this.ensureLoading = false;
          this.$emit("complete");
          this.$parent.getList();
        })
        .finally(() => {
          this.ensureLoading = false;
        });
    },
    stockChange(item, index, row) {
      if (item) {
        const { id } = item;
        row.selectedStockId = id;
        row.wmsStoreAreaName = undefined;
        row.wmsStockLocationName = undefined;
        this.$refs[`area-${index}`].options = [];
        this.$refs[`area-${index}`].fetchAreaList(id);
      }
    },
    areaChange(item, index, row) {
      if (item) {
        const { id } = item;
        row.selectedStoreAreaId = id;
        row.wmsStockLocationName = undefined;
        this.$refs[`location-${index}`].options = [];
        this.$refs[`location-${index}`].fetchLocationList(id);
      }
    },
    locationChange(item, row) {
      row.selectedStockLocationId = item.id;
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding: 10px 20px 35px 20px !important;
}
</style>
