local this = class("pageHomeProductSelectMaterial", G_UIPageBase)

function this.bind()
  return {
    list_item = L_Const.ModuleInfo.CellIconBag,
    txt_desc = "",
    moduleCommonTipMedium = L_Const.ModuleInfo.CommonTipMedium
  }
end

function this.methods()
  return {
    moduleCommonTipMedium = {
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_confirm = function(self)
        self:onClickConfirm()
      end,
      onClick_bg = function(self)
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:preOpen(options)
  local groupData = options.groupData
  local select = options.select
  local selectNum = options.selectNum
  local dataList = {}
  for i, itemData in pairs(groupData) do
    local itemHasNum = L_BagStore:getItemNum(itemData.itemId)
    local itemNeedNum = itemData.itemNum * selectNum
    local dataListItem = {
      callback = function()
        self:onClickItem(itemData.itemId, i)
      end,
      itemType = L_Const.resType.commonItem,
      itemId = itemData.itemId
    }
    if itemHasNum < itemNeedNum then
      dataListItem.itemNumTxt = string.format("<color=#ef5743>%d</color>/%d", itemHasNum, itemNeedNum)
    else
      dataListItem.itemNumTxt = string.format("%d/%d", itemHasNum, itemNeedNum)
    end
    table.insert(dataList, dataListItem)
  end
  self.bind.list_item:clear()
  self.bind.list_item:insert_array(dataList)
  self._callback = options.callback
  self:onClickItem(groupData[select].itemId, select)
  self:initCommonTipModule()
end

function this:onClickItem(id, rar)
  for i, module in ipairs(self.modules.list_item) do
    module:setSel(id == module.bind.itemId)
  end
  self._selectedRar = rar
  local _itemTpl = L_GameTpl:getCommonItemTpl()
  local tpl = _itemTpl:getTplById(id)
  self.bind.txt_desc = _itemTpl:getSpecialDesc(tpl)
end

function this:onClickConfirm()
  if self._selectedRar then
    self._callback(self._selectedRar)
    L_UI:close("pageHomeProductSelectMaterial")
  end
end

function this:close()
end

function this:initCommonTipModule()
  local moduleTip = self.modules.moduleCommonTipMedium
  moduleTip:initModule({
    txtTitle = L_WordsTpl:getValue("home_product_material_select"),
    txtConfirm = "\228\189\191\231\148\168"
  })
end

return this
