import { Button } from 'antd';
import styles from "./index.less";
import { notification } from 'antd';
import quickAPI from "../../service";

const StepsButton = (props) => {

    const onClickNext = () => {
        switch (props.current) {
            case 0:
                if (props.storeConfig?.id) {
                    props.next();
                } else {
                    notification.error({
                        message: '门店配置出错',
                        description: '请选择门店！',
                        placement: 'topRight'
                    });
                }
                break;
            case 1:
                if (props.deviceConfig?.id) {
                    props.next();
                } else {
                    notification.error({
                        message: '设备配置出错',
                        description: '请选择设备！',
                        placement: 'topRight'
                    });
                }
                break;
            case 2:
                if (props.foodMenuConfig?.length > 0) {
                    props.next();
                } else {
                    notification.error({
                        message: '菜谱配置出错',
                        description: '请至少选择一个菜谱！',
                        placement: 'topRight'
                    });
                }
                break;
            case 3:
                for(let i = 0; i < props.foodMenuConfig.length; i++) {
                    const goodsList = props.foodMenuConfig[i]?.goodsConfig;
                    if (!goodsList || goodsList?.length === 0) {
                        notification.error({
                            message: '商品配置出错',
                            description: `请为菜谱【${props.foodMenuConfig[i].name}】添加商品！`,
                            placement: 'topRight'
                        });
                        return;
                    }
                }
                props.next();
                break;
            case 4:
                for(let i = 0; i < props.foodMenuConfig.length; i++) {
                    const goodsList = props.foodMenuConfig[i]?.goodsConfig;
                    for(let j = 0; j < goodsList.length; j++) {
                        const bomList = goodsList[j]?.bomConfig;
                        if (!bomList || bomList?.length === 0) {
                            notification.error({
                                message: '配方配置出错',
                                description: `请为商品【${goodsList[j].name}】添加配方！`,
                                placement: 'topRight'
                            });
                            return;
                        }
                    }
                }
                props.next();
                break;
            case 5:
                for(let i = 0; i < props.foodMenuConfig.length; i++) {
                    const goodsList = props.foodMenuConfig[i]?.goodsConfig;
                    for(let j = 0; j < goodsList.length; j++) {
                        const bomList = goodsList[j]?.bomConfig;
                        for(let k = 0; k < bomList.length; k++) {
                            const materialList = bomList[k].materialConfig;
                            if (!materialList || materialList?.length === 0) {
                                notification.error({
                                    message: '物料配置出错',
                                    description: `请为配方【${bomList[k].name}】添加物料！`,
                                    placement: 'topRight'
                                });
                                return;
                            }
                            for(let n = 0; n < materialList.length; n++) {
                                if (!materialList[n].BomQty) {
                                    notification.error({
                                        message: '物料配置出错',
                                        description: `请为物料【${materialList[n].name}】设置数量！`,
                                        placement: 'topRight'
                                    });
                                    return;
                                }
                            }
                        }
                    }
                }
                props.next();
                break;
        }
    }

    const onCreateConfig =  async () => {
        const jsonData = {
            storeId: props.storeConfig.id,
            deviceId: props.deviceConfig.id,
            foodMenuList: []
        };
        props.foodMenuConfig.forEach((foodMenu, foodMenuIndex) => {
            jsonData.foodMenuList[foodMenuIndex] = {
                id: foodMenu.id,
                goodsList: []
            }
            foodMenu?.goodsConfig.forEach((foodItem, foodIndex) => {
                jsonData.foodMenuList[foodMenuIndex].goodsList[foodIndex] = {
                    id: foodItem.id,
                    price: foodItem.price,
                    vipPrice: foodItem.vipPrice,
                    isdevice: true,
                    BOMList: []
                }
                foodItem?.bomConfig.forEach((bomItem, bomIndex) => {
                    jsonData.foodMenuList[foodMenuIndex].goodsList[foodIndex].BOMList[bomIndex] = {
                        id: bomItem.id,
                        BatchingList: []
                    }
                    bomItem?.materialConfig.forEach((materialItem, materialIndex) => {
                        jsonData.foodMenuList[foodMenuIndex].goodsList[foodIndex].BOMList[bomIndex].BatchingList[materialIndex] = {
                            id: materialItem.id,
                            BomQty: materialItem.BomQty
                        }
                    });
                });
            });
        });
        const response = await quickAPI.QuickStartAdd(jsonData);
        if (response.data.isSuccess) {
            notification.success({
                message: '快速开始',
                description: '配置成功！',
                placement: 'topRight'
            });
            props.success();
        } else {
            notification.error({
                message: '快速开始',
                description: '配置失败！请重试！',
                placement: 'topRight'
            });
        }
    }

    return <div className={styles.steps_btns}>
        {props.current > 0 && (
            <Button
                style={{
                    margin: '0 8px',
                }}
                onClick={props.prev}
            >
                上一步：{props.steps[props.current - 1]}
            </Button>
        )}

        {props.current < props.steps.length - 1 && (
            <Button type="primary" htmlType="submit" onClick={onClickNext}>
                下一步：{props.steps[props.current + 1]}
            </Button>
        )}

        {props.current === props.steps.length - 1 && (
            <Button type="primary" onClick={onCreateConfig}>
                创建配置
            </Button>
        )}
    </div>
}

export default StepsButton;