using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Maticsoft.DBUtility;

public partial class AdvertType_Manage : PasePage
{
    
    JY.BLL.AdvertType bll = new JY.BLL.AdvertType();
    JY.Model.AdvertType model = new JY.Model.AdvertType();
    protected string ss = "";
    string[] numstr = { "Page", "page" };
    protected Dictionary<string, string> dicFunctionList = new Dictionary<string, string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        ss = Request.QueryString["fatherId"] == null ? "" : Request.QueryString["fatherId"];
        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)
        {
            string fid = Request.QueryString["typeid"] == null ? "" : Request.QueryString["typeid"].ToString();
            string oldid = Request.QueryString["oldid"] == null ? "" : Request.QueryString["oldid"].ToString();
            string typegrade = Request.QueryString["typegrade"] == null ? "" : Request.QueryString["typegrade"].ToString();
            if (!fid.Equals(""))
            {
                string str = "";
                DataTable dt = bll.GetList(" IsDelete=0 and Fid=" + fid+" order by Sort asc , Id desc").Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string strSpace = "";
                        for (int j = 1; j <= Convert.ToInt32(dt.Rows[i]["Depth"].ToString()); j++)
                        {
                            strSpace += "&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        string strImg = bll.GetList(" IsDelete=0 and Fid=" + dt.Rows[i]["id"].ToString()).Tables[0].Rows.Count > 0 ? strSpace + "<img id='img_" + oldid + "_" + dt.Rows[i]["id"].ToString() + "' onclick='loadSonData(this.id," + dt.Rows[i]["id"].ToString() + ",\"" + dt.Rows[i]["Depth"].ToString() + "\");' src='../images/folder_close.gif' style='cursor:pointer;' /><span style=\"color:Black\">" + dt.Rows[i]["TypeName"].ToString() + "</span>" : "<span style='padding-left:18px;color:Black'>" + strSpace + dt.Rows[i]["TypeName"].ToString() + "</span>";
                        str += "<tr onmouseover=\"this.style.background='#F3FBCA'\" onmouseout=\"this.style.background='#ffffff';\" id='tr_" + oldid + "_" + dt.Rows[i]["Id"].ToString() + "'>";
                        str += "<td style='text-align:left;padding-left:10px;' align='left'class='forms6_3'><div>" + strImg + "</div></td>";
                        str += "<td class='forms6_3' align='center'>   <input type='text'value='" + dt.Rows[i]["Sort"].ToString() + "' style='text-align:center;width:140px;height:18px;line-height:18px;'/>";
                        str += "<td class='forms6_4' align='left' nowrap><a href=\'AdvertType_Update.aspx?id=" + dt.Rows[i]["id"].ToString() + "&fatherId=" + Request.QueryString["fatherId"] + "\'><span>修改</span></a><a href=\'?type=del&AuthorityID=" + dt.Rows[i]["id"].ToString() + "&fatherId=" + Request.QueryString["fatherId"] + "\' onclick='return confirm(\'确认要删除此行信息吗？\')' style='margin-left:13px'><span>删除</span></a>";

                    }
                }
                Response.Write(str);
                Response.End();
            }
            if (Request.QueryString["AuthorityID"] != null && Request.QueryString["type"] == "del")
            {
                //得到需要删除的记录的编号（Id）
                clear();
            }
            BindData();
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    public void BindData()
    {
        try
        {
            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 = " AdvertType ";
            _page.SearchField = " * ";
            if (Request.QueryString["fatherId"] != null && Request.QueryString["fatherId"] != "")
            {
                _page.SearchSentence = " and isdelete=0 and id=" + Request.QueryString["fatherId"];
            }
            else
            {
                _page.SearchSentence = " and isdelete=0 and Fid=0";
            }
            _page.SortSentence = " order by AdvertType.sort asc,AdvertType.id desc ";
            _page.Primarykey = " AdvertType.Id ";
            DataTable dt = _page.GetDataTable();

            rptAdvertType.DataSource = dt;
            rptAdvertType.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;";

            tr1.Visible = _page.RecordCount <= 0;
        }
        catch (Exception ex)
        {
            publicHandle.ShowMessage(this, ex.Message);
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    protected void clear()
    {
        try
        {
            int ID = Convert.ToInt32(Request.QueryString["AuthorityID"].ToString());
            DataTable dt = bll.GetList(" IsDelete=0 and Fid=" + ID).Tables[0];
            if (dt.Rows.Count > 0)
            {
                publicHandle.ShowMessage(this.Page, "对不起，该类型下面还有子类型不能被删除！");
            }
            else
            {
                JY.BLL.Product prodbll = new JY.BLL.Product();
                DataSet ds = prodbll.GetList("AdvertTypeId=" + ID);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    publicHandle.ShowMessage(this.Page, "对不起，该类型已经使用不能被删除！");
                }
                else
                {
                    JY.Model.AdvertType model1 = new JY.Model.AdvertType();
                    model1 = bll.GetModel(ID);
                    model1.IsDelete = true;
                    if (bll.Update(model1))
                    {
                        string strUrl = "AdvertType_Manage.aspx";
                        if (Request.QueryString["fatherId"] != null && Request.QueryString["fatherId"] != "")
                        {
                            strUrl += "?fatherId=" + Request.QueryString["fatherId"];
                        }
                        publicHandle.ShowMessage(this.Page, "删除成功！", strUrl);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            publicHandle.ShowMessage(this, ex.Message);
        }
    }

    /// <summary>
    /// 得到图片
    /// </summary>
    /// <param name="id"></param>
    /// <param name="typeName"></param>
    /// <param name="depth"></param>
    /// <param name="fid"></param>
    /// <returns></returns>
    protected string getImg(string id, string typeName, string depth, string fid)
    {
        if (bll.GetList(" IsDelete=0 and Fid=" + id).Tables[0].Rows.Count > 0)
        {
            return "<img id='img_" + fid + "_" + id + "' onclick='loadSonData(this.id," + id + ",\"" + depth + "\");'  src='../images/folder_close.gif' style='cursor:pointer;'  /><span style=\"color:Black\">" + typeName + "</span>";
        }
        else
        {
            return "<span style='color:Black;padding-left:18px'>" + typeName + "</span>";
        }
    }
}