<template>
  <el-dialog
    v-dialogDrag
    title="入库明细"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @open="dialogOpen"
  >
    <el-table v-loading="loading" :data="list" border>
      <el-table-column
        label="erp单号"
        align="center"
        prop="erpWriteNo"
        width="140"
        fixed="left"
      />
      <el-table-column
        :key="Math.random()"
        label="入库单号"
        align="center"
        prop="warehousingOrderNo"
        width="160"
        fixed="left"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="物料编码"
        align="center"
        prop="materialNo"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="物料名称"
        align="center"
        prop="materialName"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="计划数量"
        align="center"
        prop="planAmount"
      />
      <el-table-column
        :key="Math.random()"
        label="BOM版本"
        align="center"
        prop="mesBomVersion"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="生产订单"
        align="center"
        prop="workOrderNo"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="箱号"
        align="center"
        prop="caseNo"
        width="170"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="拆箱状态"
        align="center"
        prop="splitFlag"
        width="120"
      >
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.WMS_QUERY_UNPACKING_STATUS"
            :value="scope.row.splitFlag"
          />
        </template>
      </el-table-column>
      <el-table-column
        :key="Math.random()"
        label="装箱数量"
        align="center"
        prop="inQty"
      />
      <el-table-column
        :key="Math.random()"
        label="仓库"
        align="center"
        prop="wmsStockId"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="区域"
        align="center"
        prop="wmsStockAreaId"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="货位"
        align="center"
        prop="wmsStockLocationId"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        :key="Math.random()"
        label="操作人"
        align="center"
        prop="creator"
      />
      <el-table-column
        :key="Math.random()"
        label="入库时间"
        align="center"
        prop="createTime"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:incoming-produce-detail:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="text-danger"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
import {
  getIncomingProduceDetailPage,
  createBatchIncomingProduceDetail,
} from "@/api/wms/incoming/production";
import { getProduceStockedPage } from "@/api/wms/inquirePurchase/complexDocuments";
import { deleteIncomingProduceDetail } from "@/api/wms/inquirePurchase/complexDocuments";
import StockSelect from "../../wareHousing/components/StockSelect.vue";
import AreaSelect from "../../wareHousing/components/AreaSelect.vue";
import LocationSelect from "../../wareHousing/components/LocationSelect.vue";

export default {
  name: "ProductWareHousingPlanAlready",
  components: {
    StockSelect,
    AreaSelect,
    LocationSelect,
  },
  props: {
    currentRows: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      ensureLoading: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
      },
    };
  },
  methods: {
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id } = row;
      this.$modal
        .confirm('是否确认删除生产入库已入库单号为"WMS-' + id + '"的数据项?')
        .then(function () {
          return deleteIncomingProduceDetail(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功,请在erp系统同步删除！");
        })
        .catch(() => {});
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams,
        storageStatus: 1,
      };
      getProduceStockedPage(params)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 完成入库
    handleSure() {
      if (this.list.length <= 0) {
        this.$modal.msgWarning("暂无入库数据，无法入库");
        return;
      }
      const isEmpty = this.list.every(
        (item) =>
          item.wmsStockId &&
          item.wmsStoreAreaId &&
          item.wmsStockLocationId &&
          item.workOrderNo &&
          item.warehousingOrderNo &&
          item.productCode &&
          item.detailErpId
      );
      if (!isEmpty) {
        this.$modal.msgWarning("数据不能为空");
        return;
      }
      this.ensureLoading = true;
      const wareHousingList = this.list.map((item) => {
        const {
          wmsStockId: stockCode,
          wmsStoreAreaId: areaCode,
          wmsStockLocationId: locationCode,
          masterId,
          warehousingOrderNo,
          detailErpId,
          productCode,
          planAmount,
          workOrderNo,
          caseNo,
          inQty,
          wmsStockLogicId,
        } = item;
        return {
          stockCode,
          areaCode,
          locationCode,
          masterId,
          warehousingOrderNo,
          detailErpId,
          productCode,
          planAmount,
          workOrderNo,
          caseNo,
          inQty,
          wmsStockLogicId,
        };
      });
      createBatchIncomingProduceDetail(wareHousingList)
        .then((res) => {
          this.$modal.msgSuccess("入库成功");
          this.open = false;
          this.ensureLoading = false;
          this.$parent.getList();
          this.$emit("complete");
        })
        .finally(() => {
          this.loading = false;
          this.ensureLoading = false;
        });
    },
    stockChange(item, index, row) {
      if (item) {
        const { id } = item;
        row.wmsStockId = 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.wmsStoreAreaId = id;
        row.wmsStockLocationName = undefined;
        this.$refs[`location-${index}`].options = [];
        this.$refs[`location-${index}`].fetchLocationList(id);
      }
    },
    locationChange(item, row) {
      row.wmsStockLocationId = item.id;
    },
    dialogOpen() {
      this.$nextTick(() => {
        this.getList();
      });
    },
  },
};
</script>

<style></style>
