import { getGoodsUnitEnumItemName } from "@/common/constants/enumsName";
import APP from '@/services';
import { QueryDeliveryGoods } from "@/services/go/ermcp/goodsInfo";
import { Ermcp3Brand, Ermcp3Wrstandard, ErmcpDeliveryGoodsDetailEx, ErmcpDeliveryGoodsRsp } from "@/services/go/ermcp/goodsInfo/interface";
import { QueryWareHouse } from "@/services/go/ermcp/warehouse-info/index";
import { ErmcpWareHouseInfo } from "@/views/information/warehouse-info/list";
import { message } from "ant-design-vue";
import { reactive, ref, UnwrapRef } from "vue";
import { FormState } from "./interface";

// 表单
export function handleFormState() {
    const formRef = ref();
    function initFormData(): FormState {
        return {
            InOutType: 7, // int32 出入库类型 - 1:采购入库 2:销售出库 3:生产入库 4:生产出库
            WRStandardID: undefined, // uint64 现货商品ID
            SpotGoodsBrandID: undefined, // uint64 现货品牌ID
            DeliveryGoodsID: undefined, // uint64 现货品种ID
            WarehouseInfo: undefined, // uint64 现货仓库ID
            Qty: null, // double 数量
        }
    }
    const formState: UnwrapRef<FormState> = reactive(initFormData())
    const rules = {
        WRStandardID: [{ required: true, message: '请选择现货商品' }],
        SpotGoodsBrandID: [{ required: true, message: '请选择现货品牌' }],
        DeliveryGoodsID: [{ required: true, message: '请选择现货品种' }],
        WarehouseInfo: [{ required: true, message: '请选择现货仓库' }],
        Qty: [{ required: true, message: '请输入数量' }],
    }
    return { rules, formState, formRef, initFormData }
}

// 仓库类别
export function handleWarehouseType(formState: UnwrapRef<FormState>) {
    const warehouseType = [
        { name: '生产入库', key: 7 },
        { name: '生产出库', key: 8 },
    ]
    /**
     *
     * 是否入库
     * @return true: 入库； false：出库
     */
    function isIn(): boolean {
        return formState.InOutType === 7
    }
    function inOrOut() {
        return isIn() ? '入库' : '出库'
    }
    return { warehouseType, inOrOut }
}

// 仓库列表
export function handleWarehouseList() {
    const wareHouseList = ref<ErmcpWareHouseInfo[]>([]);
    function getWarehouseList() {
        QueryWareHouse('1').then((res) => {
            wareHouseList.value = res;
        });
    }
    return { wareHouseList, getWarehouseList }
}

/**
 * 处理现货商品
 */
export function handleDeliveryGoods(formState: UnwrapRef<FormState>) {
    // 现货品种
    const deliveryGoodsList = ref<ErmcpDeliveryGoodsRsp[]>([])
    // 品牌
    const gblist = ref<Ermcp3Brand[]>([])
    // 品类列表
    const gmlist = ref<Ermcp3Wrstandard[]>([])

    // 查询现货商品
    function getDeliveryGoods() {
        QueryDeliveryGoods({}).then(res => {
            console.log('查询现货商品', res);
            deliveryGoodsList.value = res.filter(x=>x.isvalid === 1);
        }).catch(err => {
            message.error(err)
        })
    }

    // 切换现货商品
    function deliveryGoodsChange(value: number | undefined) {
        if (value !== undefined) {
            getDeliveryGoodsDetail(value)
        } else {
            gblist.value.length = 0
            gmlist.value.length = 0
        }
        formState.WRStandardID = undefined;
        formState.SpotGoodsBrandID = undefined;
    }

    // 查询现货商品详情
    function getDeliveryGoodsDetail(deliverygoodsid: number) {
        const temp = APP.get('DeliveryGoodsList').find((e: ErmcpDeliveryGoodsDetailEx) => e.data.deliverygoodsid === deliverygoodsid && e.data.isvalid == 1)
        if (temp) {
            gblist.value = temp.gblist
            gmlist.value = temp.gmlist
        }
    }

    const numberUnit = ref<string>('')

    // 品类变更
    function WrStandardChange(value: number) {
        const obj = gmlist.value.find((e) => e.wrstandardid === value);
        if (obj) {
            //标仓系数
            // 单位
            numberUnit.value = getGoodsUnitEnumItemName(obj.unitid)
        }
    }

    return {
        deliveryGoodsList,
        gblist,
        gmlist,
        numberUnit,
        WrStandardChange,
        getDeliveryGoods,
        getDeliveryGoodsDetail,
        deliveryGoodsChange
    }
}