import gameToast from "../Network/gameToast"
cc.Class({
    extends: cc.Component,

    properties: {
        saleName: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        saleAmount: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        inputInviteValue: {
            default: 0,
            visible: false,
            serializable:false
        },
        //当前操作的果实信息
        fruitInfo: {
            default: null,
            serializable: true,
            visible: false
        },
        upTarget: {
            default: null,
            type: cc.Node,
            tooltip: "把当前操作的ui按钮传进来"
        },
        inputContainer: {
            default: null,
            type: cc.Node,
            tooltip: "inputEditbox"
        },

        isPlayingSnb: false,

        /**
         * 预计收入显示
         */
        estimateAmount: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
    },
    inputValue(value, e) {

        var numberTemp = new RegExp("^[A-Za-z0-9]+$");
        if (numberTemp.test(value)) {
            if (Number(value) >= 1) {
                this.inputInviteValue = Number(value);
            } else {
                this.inputInviteValue = 0;
                this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
            }
        } else {
            this.inputInviteValue = 0;
            this.inputContainer.getComponent(cc.EditBox).string = this.inputInviteValue;
            console.log("请输入20整数的倍数", this.inputInviteValue);
        }

        this.estimateAmount.string = Number(this.inputInviteValue * this.fruitInfo.priceSnb)+" SNB";
    },
    setInfo(saleName, amount) {
        this.saleName.string = saleName;
        this.saleAmount.string = amount;
    },
    onclose() {
        this.node.destroy();
    },

    //确定销售果实
    onLeaseSaleFruit() {
        //todo 判断一下amount
        console.log(this.inputInviteValue, Number(this.saleAmount.string));
        if (this.inputInviteValue > Number(this.saleAmount.string)) {
            GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "销售的果实数量过多！", 1);
            return;
        }
        if (this.inputInviteValue <=0) {
            GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "输入不为零的果实数量！", 1);
            return;
        }
        // if (this.isPlayingSnb) {
        //     console.warn("同时触发支付SNB过快！");
        //     return;
        // }
        // this.isPlayingSnb = true;

        if (GlobalD.GameData.isOnSaleFruit) return;
        GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "销售果实中...", 5);
        GlobalD.GameData.onSaleFruit({ fruitId: this.fruitInfo.id, amount: this.inputInviteValue }, (res, value) => {
            // this.isPlayingSnb = false;
            // console.log(value);
            if (value.code === 0) {
                // let NumLabel = this.upTarget.getComponent("Content_Button").NumLabel.getComponent(cc.Label);
                // if (parseInt(NumLabel.string) <= this.inputInviteValue) {
                //     //这里只隐藏
                //     this.upTarget.active = false;
                // } else {
                //     NumLabel.string = parseInt(NumLabel.string) - this.inputInviteValue;
                //     this.saleAmount.string = parseInt(this.saleAmount.string) - this.inputInviteValue;
                // }
                var BuildingView = cc.find("Canvas/UICamera/BuildingContainer/BuildingView").getComponent("BuildingView");
                BuildingView.onUpdateList();

                //更新ui snb
                GlobalD.GameData.onGetUserSnb((res, value) => {
                    this.saleAmount.string = parseInt(this.saleAmount.string) - this.inputInviteValue;
                });
                // GlobalD.GameData.SetSNB(GlobalD.GameData.GetSNB() + (this.inputInviteValue * this.fruitInfo.priceSnb));
                // this.saleAmount.string = parseInt(this.saleAmount.string) - this.inputInviteValue;
                GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), "成功售卖了！", 1);

                //更新日志列表
                //更新日志数据
                cc.find("GameNode/ManageDapp").getComponent("ManageDapp").onUpdateSnbList();

            } else {
                GlobalD.GameData.showToast(cc.find('Canvas/UICamera'), value.msg, 1);
            }
        });
    }
});
