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 Information_Manage : PasePage
{
    JY.BLL.Information bll = new JY.BLL.Information();
    JY.Model.Information model = new JY.Model.Information();
    JY.BLL.ComDataList combll = new JY.BLL.ComDataList();
    protected Dictionary<string, string> dicFunctionList = new Dictionary<string, string>();
    string Ids = "";
    string[] numstr = { "Page", "page", "informationname", "informationtypeids", "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();
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    protected void BindDate()
    {
        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 = " Information ";
            _page.SearchField = " * ";
            if (Request.QueryString["informationtypeids"] != null && Request.QueryString["informationtypeids"] != "")
            {
                _page.SearchSentence = " and Information.IsDelete=0 ";
            }
            else
            {
                if (Request.QueryString["TypeId"] != null && Request.QueryString["TypeId"] != "")
                {
                    getIds(Request.QueryString["TypeId"]);
                    _page.SearchSentence = " and Information.IsDelete=0 and InformationTypeId in(" + Ids + Request.QueryString["TypeId"] + ")";
                }
                else
                {
                    _page.SearchSentence = " and Information.IsDelete=0 ";
                }
            }

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

            #endregion

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

            rptInformation.DataSource = dt;
            rptInformation.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.Information bll1 = new JY.BLL.Information();
            if (bll.Delete(ID))
            {
                string strUrl = "Information_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>
    protected string getInformationTypeName(string typeId)
    {
        JY.BLL.InformationType ptbll = new JY.BLL.InformationType();
        JY.Model.InformationType ptmodel = new JY.Model.InformationType();
        if (typeId != "0" && typeId != null && typeId != "")
        {
            ptmodel = ptbll.GetModel(Convert.ToInt64(typeId));
            if (ptmodel != null)
            {
                return ptmodel.TypeName;
            }
            else 
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }

    /// <summary>
    /// 一级菜单下拉框
    /// </summary>
    protected void bing_dd_type()
    {
        try
        {
            ddlInformationType.Items.Clear();
            DataTable dt = new DataTable();
            if (Request.QueryString["TypeId"] == null || Request.QueryString["TypeId"] == "" || Request.QueryString["TypeId"] == "0")
            {
                ddlInformationType.Items.Add(new ListItem("所有资讯类型", "0,-1"));
                dt = combll.GetComList(0, " id,TypeName,Depth ", " InformationType ", " isdelete=0 and depth=0 and Fid=0 ", " Sort asc,id desc ");
            }
            else
            {
                dt = combll.GetComList(0, " id,TypeName,Depth ", " InformationType ", " 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();
                    ddlInformationType.Items.Add(li);
                    BindChild(dt.Rows[i]["id"].ToString(), Convert.ToInt32(dt.Rows[i]["Depth"].ToString()) + 1);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 绑定子下拉框
    /// </summary>
    protected void BindChild(string fatherid, int depth)
    {
        try
        {
            DataTable dt = combll.GetComList(0, " id,TypeName,Depth ", " InformationType ", " 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();
                    ddlInformationType.Items.Add(li);
                    BindChild(dt.Rows[i]["id"].ToString(), Convert.ToInt32(dt.Rows[i]["Depth"].ToString()) + 1);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// 提交事件
    /// </summary>
    protected void btnquery_Click(object sender, EventArgs e)
    {
        string url = Request.Url.ToString();
        url = url.Substring(url.LastIndexOf('/') + 1);
        url = GetSubStr(numstr, url);
        string informationname = this.txtInformationName.Text.Trim();
        string informationtypeids = this.ddlInformationType.SelectedValue;
        getIds(this.ddlInformationType.SelectedValue.Split(new char[] { ',' })[0]);
        if (url.IndexOf('?') > -1)
        {
            Response.Redirect(url + "&informationname=" + informationname + "&informationtypeids=" + informationtypeids + "&Ids=" + Ids);
        }
        else
        {
            Response.Redirect(url + "?informationname=" + informationname + "&informationtypeids=" + informationtypeids + "&Ids=" + Ids);
        }
    }

    /// <summary>
    /// 获取Ids
    /// </summary>
    /// <param name="fid"></param>
    private void getIds(string fid)
    {
        DataTable dt11 = combll.GetComList(0, " id", "InformationType", "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 = "Information_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 rptInformation_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Del")
            {
                JY.BLL.Information bll = new JY.BLL.Information();
                JY.Model.Information model = bll.GetModel(Convert.ToInt32(e.CommandArgument.ToString()));
                model.IsDelete = true;

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