AlertNoMoneyView.lua 3.27 KB
local AlertNoMoneyView = dialog.uinode("ui/AlertNoMoneyLayer.csb",import(".BaseView"))

function AlertNoMoneyView:ctor()
    AlertNoMoneyView.super.ctor(self);
end

function AlertNoMoneyView:show(shopID,okFun,cancelFun)
    local scene = cc.Director:getInstance():getRunningScene()
    local inst = AlertNoMoneyView:create();
    scene:addChild(inst, dialog.ZORDER_APP);

    inst:setContentSize(cc.size(display.width,display.height));
    ccui.Helper:doLayout(inst);

    local panel = inst:getChildByName("panel");
    panel:setScale(0.5);
    local scaleTo = cc.ScaleTo:create(0.2,1);
    panel:runAction(scaleTo);

    inst.shopID = shopID;
    inst.okFun = okFun;
    inst.cancelFun = cancelFun;
    inst.selectedIndex = 2;

    inst:init();

    return inst;
end

function AlertNoMoneyView:init()
    local shopInfo = ShopInfo.getShopInfo(self.shopID);
    local itemID = shopInfo.itemList[1].id;
    local itemInfo = ItemInfo.getItemInfo(itemID);

    self:getChildByName("panel"):getChildByName("txt"):setString(shopInfo.itemList[1].num);

    local btn_ok = self:getChildByName("panel"):getChildByName("btn_ok");
    btn_ok.fn = handler(self,self.onOk);
    btn_ok:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, btn_ok.fn)end);
    local costNum = shopInfo.costList[1].num;
    btn_ok:getChildByName("txt"):setString(costNum.."兑换");

    local btn_cancel = self:getChildByName("panel"):getChildByName("btn_cancel");
    btn_cancel.fn = handler(self,self.onCancel);
    btn_cancel:addTouchEventListener(function(ref,type) self:onButtonClick(ref,type, btn_cancel.fn)end);

    self.nodes[1] = {btn_cancel,btn_ok};
    self:updateSelectedState();
end

function AlertNoMoneyView:onOk()
    local shopInfo = ShopInfo.getShopInfo(self.shopID);
    local costID = shopInfo.costList[1].id;
    local costNum = shopInfo.costList[1].num;
    local myNum = UserModel.getItemNum(costID);
    if myNum >= costNum then
        LoadingManager.showDataLoading();
        ShopModel.buy(self.shopID,1,handler(self,self.onBuyCallBack),handler(self,self.onTimeoutFn));
    else
        local function okFun()
            local scene = cc.Director:getInstance():getRunningScene();
            local shopView = scene:getChildByName("ShopView");
            if shopView then
                shopView:setTab(1);
                self:close();
            else
                AppManager.show("ShopView");
            end
        end
        Alert.showTwo(strings.msg_1003,okFun);
    end
end

function AlertNoMoneyView:onBuyCallBack(params)
    LoadingManager.removeDataLoading();
    if params.errcode == EnumErrorCode.EC_SUCCESS then
        if self.okFun then
            self.okFun();
        end
        self:close();
    else
        if self.cancelFun then
            self.cancelFun();
        end
        EffectManager.showFntPop(strings.msg_2017);
    end
end

function AlertNoMoneyView:onTimeoutFn(params)
    LoadingManager.removeDataLoading();
    if self.cancelFun then
        self.cancelFun();
    end
    self:close();
end

function AlertNoMoneyView:onCancel()
    if self.cancelFun then
        self.cancelFun();
    end
    self:close();
end

--返回键处理函数
function AlertNoMoneyView:onKeypadBack()
    if self.cancelFun then
        self.cancelFun();
    end
    self:close();
end

return AlertNoMoneyView