using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VA.Utils;
using VA.Modules.JDingMember.Core.Business.Gift;
using VA.Modules.JDingMember.Core.Config;
using VA.Modules.JDingMember.Core.Model.Gift;
using VA.Modules.JDingMember.WebBase;
using System.Data;

namespace VA.Modules.JDingMember.gift
{
    public partial class uc_gift_book : System.Web.UI.UserControl
    {
        public string ShopCode
        {
            get;
            set;
        }

        public int MainHeight
        {
            get { return _MainHeight; }
            set { _MainHeight = value; }
        }
        int _MainHeight = 400;

        GiftCategoryDict category = new GiftCategoryDict();
        GiftDict gift= new GiftDict();

        DataTable dtbCategoryAll;
        DataTable dtbGiftAll;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.dtbCategoryAll = category.GetDataList("Status='" + Core.Enums.GiftCategoryStatus.Enabled.ToString("d") + "'", "Category_Code ASC");
                this.dtbGiftAll = gift.GetDataList("Item_Type='" + Core.Enums.GiftType.Product.ToString("d") + "' AND (Shop_Code='" + this.ShopCode + "' OR Shop_Code='" + Core.Enums.GlobalVariables.GLOBAL_SHOP_CODE + "') AND Status='" + Core.Enums.GiftStatus.Enabled.ToString("d") + "'", "Input_Code ASC");

                this.rpGiftCategory.DataSource = this.dtbCategoryAll;
                this.rpGiftCategory.DataBind();
            }
        }

        protected void rpGiftCategory_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rpGiftDict = (Repeater)e.Item.FindControl("rpGiftDict");

                //找到分类Repeater关联的数据项 
                DataRowView rowv = (DataRowView)e.Item.DataItem;

                //提取分类ID 
                string parentCode = rowv["Category_Code"].ToString();

                //从用户的所有节点权限集中筛选出数据该模块（模块ID为 moduleID ）的节点权限；
                if (this.dtbGiftAll != null)
                {
                    DataRow[] rows = this.dtbGiftAll.Select("Category_Code='" + parentCode + "'");
                    if (rows != null && rows.Length > 0)
                    {
                        DataTable dtbData = rows.CopyToDataTable();

                        rpGiftDict.DataSource = dtbData;
                        rpGiftDict.DataBind();
                    }
                }
            }
        }

        protected string ShowLetterIndex(string inputCode)
        {
            if (!String.IsNullOrEmpty(inputCode))
            {
                return inputCode.Substring(0, 1).ToUpper();
            }
            else
            {
                return "";
            }
        }
    }
}