
import { getInventory } from '../api/index'

export let locationModule = {
    methods: {
        //选择货位
        selectLocation(data) {
            console.log(data,data.adjustBillType,'data.adjustBillType');
            // 1：报损单2：报溢单 根据类型判断调用哪个接口弹哪个界面
            var params = {}
            if (data.adjustBillType === '1') {
                params = {
                    num: 'list_instock_locator',
                    // id:'locator_stock_list',
                    params: {
                        warehouseId: data.warehouseId,
                        cargoOwnerId: data.cargoOwnerId,
                        itemIds: [data.itemId],
                        showProductionDate: data.showProductionDate,
                        showExpiredDate: data.showExpiredDate,
                        manufacturerLotNum: data.manufacturerLotNum,
                        status: data.stockStatus,
                        pageNum: 1,
                        pageSize: 15,
                    },
                }
                getInventory(params).then((res) => {
                    if (!res.success) {
                        this.$exception(res)
                        return
                    }
                    this.inventoryData = res.data?.list
                    this.detailData = this.locationData
                    this.breakage = true
                    this.rowData = data
                    this.newLocation = this.newLocationData
                })

            } else {
                params = {
                    num: 'locator_info_list',
                    params: {
                        warehouseId: data.warehouseId,
                        cargoOwnerId: data.cargoOwnerId,
                        itemId: data.itemId,
                        showProductionDate: data.showProductionDate,
                        showExpiredDate: data.showExpiredDate,
                        manufacturerLotNum: data.manufacturerLotNum,
                        lotStatus: data.stockStatus,
                        pageNum: 1,
                        pageSize: 15,
                    },
                }
                getInventory(params).then((res) => {
                    if (!res.success) {
                        this.$exception(res)
                        return
                    }
                    this.overflowInventoryData = res.data?.list
                    this.overflowDetailData = this.locationData
                    this.overflow = true
                    this.overflowRowData = data
                })
            }

        },
        // 校验
        checkNumber(params) {
            var flg = true
            if (this.locationData.length < 1) {
                this.$message.warning("暂未指定库位，无法生成损溢单")
                return
            }
            if (this.newLocationData.length < 1) {
                console.log('1', this.locationData);
                this.locationData.forEach((t) => {
                    //当库别是整件库时，损溢数量必须是包装数量的整数倍；
                    if (t.whAreaCategory === "ZJK") {
                        if (t.pieceQuantity % t.conversionRate != 0) {
                            flg = false
                            this.$message.warning('商品' + t.itemName + '库别是整件库时，损溢数量必须是包装数量的整数倍，请检查！')
                            return
                        }
                    }
                })
                return
            } else {
                console.log('2', this.newLocationData);
                this.newLocationData.forEach((t) => {
                    if (t.whAreaCategory === "ZJK") {
                        if (t.pieceQuantity % t.conversionRate != 0) {
                            flg = false
                            this.$message.warning('商品' + t.itemName + '库别是整件库时，损溢数量必须是包装数量的整数倍，请检查！')
                            return
                        }
                    }
                })
            }
            params.forEach((item, i) => {
                //数量不能超过计划损溢数量，也不能少于计划损溢数量
                if (item.prePieceQuantity != item.pieceQuantity) {
                    flg = false
                    this.$message.warning('第' + (i + 1) + "行" + item.itemName + '商品损溢实际数量与当前商品计划损溢数量不符，请检查')
                    return false
                }
                //报损每个货位的报损数量不能超过货位库存数量
                if (item.adjustBillType === '1') {
                    console.log(item.pieceQty, '库位数量');
                    if (item.pieceQuantity > item.pieceQty) {
                        flg = false
                        this.$message.warning('第' + (i + 1) + "行" + item.itemName + '商品报损实际数量与当前库位总数量不符，请检查')
                        return false
                    }
                }
            })
            return flg
        },

        // 数量校验
        checkQty(record, value) {
            // 每个货位的报损数量不能超过货位库存数量
            if (record.adjustBillType === '1') {
                if (value > record.pieceQty) {
                    this.$message.warning('当前实际数量大于库位库存数量，请检查')
                }
            }
            if (record.whAreaCategory === "ZJK") {
                if (value % record.conversionRate != 0) {
                    this.$message.warning('当库别是整件库时，损溢数量必须是包装数量的整数倍，请检查！')
                }
            }

        }



    }

}