using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using JY.Model;
using JY.BLL;
using Maticsoft.DBUtility;

public partial class Product_Manage : PasePage
{
    JY.BLL.Product bll = new JY.BLL.Product();
    JY.Model.Product model = new JY.Model.Product();
    JY.BLL.ComDataList combll = new JY.BLL.ComDataList();
    protected Dictionary<string, string> dicFunctionList = new Dictionary<string, string>();
    string Ids = "";
    string[] numstr = { "Page", "page", "productname", "producttypeids", "Ids" };
    protected void Page_Load(object sender, EventArgs e)
    {
        dicFunctionList.Add("添加", "false");
        dicFunctionList.Add("修改", "false");
        dicFunctionList.Add("删除", "false");
        string url = Request.Url.ToString();
        if (url.IndexOf("adminsys") > -1)
        {
            url = url.Replace("adminsys", "Adminsys");
        }
        url = url.Substring(url.IndexOf("Adminsys") + 9);
        url = GetSubStr(numstr, url);
        if (!IsPostBack)
        {
            if (Request.QueryString["DeID"] != null)
            {
                //得到需要删除的记录的编号（Id）
                clear();
            }
            else
            {
                Session["urlLink"] = Request.RawUrl;
            }
            bing_dd_type();
            BindDate();
        }
    }
    // 下载于www.51aspx.com
    /// <summary>
    /// 绑定数据 
    /// </summary>
    protected void BindDate()
    {
        try
        {
            //Session["urlLink"] = Request.RawUrl;

            int pageIndex = GetInt(Request.QueryString["Page"], 1);
            Pagination.Pagination _page = new Pagination.Pagination();

            _page.ItemUnit = "个号码";
            _page.ImagesPath = "../Images/ui_form/";
            _page.Sequence = pageIndex;
            _page.PageSize = pager.PageSize;
            _page.TableName = " Product ";
            _page.SearchField = " * ";
            if (Request.QueryString["producttypeids"] != null && Request.QueryString["producttypeids"] != "")
            {
                _page.SearchSentence = " and Product.IsDelete=0 ";
            }
            else
            {
                if (Request.QueryString["TypeId"] != null && Request.QueryString["TypeId"] != "")
                {
                    getIds(Request.QueryString["TypeId"]);
                    _page.SearchSentence = " and Product.IsDelete=0 and ProductTypeId in(" + Ids + Request.QueryString["TypeId"] + ")";
                }
                else
                {
                    _page.SearchSentence = " and Product.IsDelete=0 ";
                }
            }

            #region 按查询条件查询数据
            if (Request.QueryString["productname"] != null && Request.QueryString["productname"] != "")
            {
                this.txtProductName.Text = Request.QueryString["productname"].ToString();
                _page.SearchSentence += " and productname like '%" + Request.QueryString["productname"] + "%'";
            }
            if (Request.QueryString["producttypeids"] != null && Request.QueryString["producttypeids"] != "")
            {
                this.ddlProductType.SelectedValue = Request.QueryString["producttypeids"].ToString();
                string aa = Request.QueryString["Ids"];
                _page.SearchSentence += " and ProductTypeId in(" + Request.QueryString["Ids"] + Request.QueryString["producttypeids"].Split(new char[] { ',' })[0] + ")";
            }

            #endregion

            _page.SortSentence = " order by Product.ID desc ";
            _page.Primarykey = " Product.id ";
            DataTable dt = _page.GetDataTable();

            rptProduct.DataSource = dt;
            rptProduct.DataBind();

            pager.RecordCount = _page.RecordCount;
            //显示记录信息   [ 共1508条 第1页 共151页 ]
            pager.CustomInfoHTML = "【 共<b>" + pager.RecordCount.ToString() + "</b> 条&nbsp;";
            pager.CustomInfoHTML += " 当前第<font color=\"red\"><b>" + pageIndex.ToString() + "</b></font>页&nbsp;";
            pager.CustomInfoHTML += " 共<b>" + pager.PageCount.ToString() + "</b>页 】&nbsp;&nbsp;";

            trRecord.Visible = _page.RecordCount / _page.PageSize > 0;
            btndel.Visible = _page.RecordCount > 0;
            data_page_div.Visible = _page.RecordCount > 0;
        }
        catch (Exception ex)
        {
            publicHandle.ShowMessage(this, ex.Message);
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    protected void clear()
    {
        try
        {
            long ID = Convert.ToInt64(Request.QueryString["DeID"].ToString());
            JY.BLL.Product bll1 = new JY.BLL.Product();
            if (bll.Delete(ID))
            {
                string strUrl = "Product_Manage.aspx";
                if (Session["urlLink"] != null)
                {
                    strUrl = Session["urlLink"].ToString();
                    Session["urlLink"] = null;
                }
                publicHandle.ShowMessage(this.Page, "删除成功！", strUrl);
            }
        }
        catch (Exception ex)
        {
            publicHandle.ShowMessage(this, ex.Message);
        }
    }

    /// <summary>
    /// 获取商品类型名称
    /// </summary>
    /// <param name="typeId"></param>
    /// <returns></returns>
    protected string getProductTypeName(string typeId)
    {
        JY.BLL.ProductType ptbll = new JY.BLL.ProductType();
        JY.Model.ProductType ptmodel = new JY.Model.ProductType();
        if (typeId != "0" && typeId != null && typeId != "")
        {
            ptmodel = ptbll.GetModel(Convert.ToInt64(typeId));
            if (ptmodel != null)
            {
                return ptmodel.TypeName;
            }
            else 
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 绑定品牌
    /// </summary>
    /// <param name="typeId"></param>
    /// <returns></returns>
    protected string getBrandName(string typeId)
    {
        JY.BLL.Brand brbll = new JY.BLL.Brand();
        JY.Model.Brand brmodel = new JY.Model.Brand();
        if (typeId != "0" && typeId != null && typeId != "")
        {
            brmodel = brbll.GetModel(Convert.ToInt64(typeId));
            if (brmodel != null)
            {
                return brmodel.BrandName;
            }
            else
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 一级菜单下拉框
    /// </summary>
    protected void bing_dd_type()
    {
        try
        {
            ddlProductType.Items.Clear();
            DataTable dt = new DataTable();
            if (Request.QueryString["TypeId"] == null || Request.QueryString["TypeId"] == "" || Request.QueryString["TypeId"] == "0")
            {
                ddlProductType.Items.Add(new ListItem("所有商品类型", "0,-1"));
                dt = combll.GetComList(0, " id,TypeName,Depth ", " ProductType ", " isdelete=0 and depth=0 and Fid=0 ", " Sort asc,id desc ");
            }
            else
            {
                dt = combll.GetComList(0, " id,TypeName,Depth ", " ProductType ", " isdelete=0 and id=" + Request.QueryString["TypeId"], " Sort asc,id desc ");
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem li = new ListItem();
                    li.Text = "╋" + dt.Rows[i]["TypeName"].ToString();
                    li.Value = dt.Rows[i]["id"].ToString() + "," + dt.Rows[i]["Depth"].ToString();
                    ddlProductType.Items.Add(li);
                    BindChild(dt.Rows[i]["id"].ToString(), Convert.ToInt32(dt.Rows[i]["Depth"].ToString()) + 1);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 绑定子下拉框
    /// </summary>
    /// <param name="fatherid"></param>
    /// <param name="depth"></param>
    protected void BindChild(string fatherid, int depth)
    {
        try
        {
            DataTable dt = combll.GetComList(0, " id,TypeName,Depth ", " ProductType ", " isdelete=0 and Depth=" + depth + " and Fid=" + fatherid + " ", " Sort asc,id desc ");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string separator = "├──";
                    if (depth > 1)
                    {
                        for (int j = 2; j <= depth; j++)
                        {
                            separator += "──";
                        }
                    }
                    ListItem li = new ListItem();
                    li.Text = separator + dt.Rows[i]["TypeName"].ToString();
                    li.Value = dt.Rows[i]["id"].ToString() + "," + dt.Rows[i]["Depth"].ToString();
                    ddlProductType.Items.Add(li);
                    BindChild(dt.Rows[i]["id"].ToString(), Convert.ToInt32(dt.Rows[i]["Depth"].ToString()) + 1);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnquery_Click(object sender, EventArgs e)
    {
        string url = Request.Url.ToString();
        url = url.Substring(url.LastIndexOf('/') + 1);
        url = GetSubStr(numstr, url);
        string productname = this.txtProductName.Text.Trim();
        string producttypeids = this.ddlProductType.SelectedValue;
        getIds(this.ddlProductType.SelectedValue.Split(new char[] { ',' })[0]);
        if (url.IndexOf('?') > -1)
        {
            Response.Redirect(url + "&productname=" + productname + "&producttypeids=" + producttypeids + "&Ids=" + Ids);
        }
        else
        {
            Response.Redirect(url + "?productname=" + productname + "&producttypeids=" + producttypeids + "&Ids=" + Ids);
        }
    }
    
    /// <summary>
    /// 获取Ids
    /// </summary>
    /// <param name="fid"></param>
    private void getIds(string fid)
    {
        DataTable dt11 = combll.GetComList(0, " id", "ProductType", "Fid=" + fid, "");
        if (dt11.Rows.Count > 0)
        {
            for (int i = 0; i < dt11.Rows.Count; i++)
            {
                Ids += dt11.Rows[i][0].ToString() + ",";
                getIds(dt11.Rows[i][0].ToString());
            }
        }
    }

    /// <summary>
    /// 批量删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btndel_Click(object sender, EventArgs e)
    {
        try
        {
            if (hidBatchDelId.Value.Trim() != "")
            {
                if (bll.DeleteList(hidBatchDelId.Value))
                {
                    //Response.Write("<script type='text/javascript'>alert('批量删除成功！')</script>");
                    //BindDate();
                    string strUrl = "Product_Manage.aspx";
                    if (Session["urlLink"] != null)
                    {
                        strUrl = Session["urlLink"].ToString();
                        Session["urlLink"] = null;
                    }
                    publicHandle.ShowMessage(this.Page, "批量删除成功", strUrl);
                }
                else
                {
                    publicHandle.ShowMessage(this.Page, "批量删除失败！");
                }
            }
        }
        catch (Exception ex)
        {
            publicHandle.ShowMessage(this, ex.Message);
        }
    }

    /// <summary>
    /// 触发活动表的ItemCommand事件
    /// </summary>
    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Del")
            {
                JY.BLL.Product bll = new JY.BLL.Product();
                JY.Model.Product model = bll.GetModel(Convert.ToInt32(e.CommandArgument.ToString()));
                model.IsDelete = true;

                bll.Update(model);
            }
            this.BindDate();
        }
        catch (Exception er)
        {
            publicHandle.ShowMessage(this, er.Message);
        }
    }
}
