local this = class("pageBagUseSelectGift", G_UIPageBase)
local _commonItemChangeTpl = L_GameTpl:getCommonItemChangeTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
end

function this.bind()
  return {
    txt_title = "",
    cell_before = L_Const.ModuleInfo.CellIcon,
    cell_after = L_Const.ModuleInfo.CellIcon,
    module_selectNum = L_Const.ModuleInfo.SelectNum,
    txt_num_before = "",
    txt_num_after = "",
    module_commonTipBoard = L_Const.ModuleInfo.CommonTipBoard
  }
end

function this.methods()
  return {
    onClick_close = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_cancel = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_confirm = function(self)
      L_UI:close(self.pageName, nil, function()
        local data = {
          itemType = self._itemType,
          itemId = self._itemId,
          itemNum = self._selectNum
        }
        L_BagManager:reqExchangeItem(data)
      end)
    end
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self._itemType = options.itemType or L_Const.resType.commonItem
  self._itemId = options.itemId
  self:initPage()
end

function this:initPage()
  self.modules.cell_before:setGeneralContent(self._itemType, self._itemId)
  local tpl_change = _commonItemChangeTpl:getTplByTypeAndId(self._itemType, self._itemId)
  local reward = L_DataUtil.parseRewardConfigItem(_commonItemChangeTpl:getChange(tpl_change)[1])
  self.modules.cell_after:setGeneralContent(reward.itemType, reward.itemId)
  self.bind.txt_title = _commonItemChangeTpl:getTitle(tpl_change)
  local max = math.floor(L_ItemTplManager:getItemNum(self._itemType, self._itemId) / _commonItemChangeTpl:getItemNum(tpl_change))
  self._selectNum = max
  local func = function(num)
    self._selectNum = num
    self:changeNum()
  end
  self.modules.module_selectNum:initModule(func, max, 1, self._selectNum)
  func(self._selectNum)
end

function this:changeNum()
  self.bind.txt_num_before = string.format("X%s", self._selectNum)
  local tpl_change = _commonItemChangeTpl:getTplByTypeAndId(self._itemType, self._itemId)
  local reward = L_DataUtil.parseRewardConfigItem(_commonItemChangeTpl:getChange(tpl_change)[1])
  self.bind.txt_num_after = string.format("X%s", reward.itemNum * self._selectNum)
end

return this
