using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Base;
using Help;

public partial class admin_Help_HelpTypeList : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("column", "D1");
        if (!IsPostBack)
        {
            Bind();
        }
    }
    int ParentID = 0;
    private void Bind()
    {
        string strWhere = "1=1";

        if (Request.QueryString["ParentId"] != null && Request.QueryString["ParentId"] != "")
        {
            ParentID = int.Parse(Request.QueryString["ParentId"]);
        }
        strWhere += " AND ParentID=" + ParentID;
        pbind(strWhere, Pageindex);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtType.Text == string.Empty)
        {
            Response.Write("<script>alert('信息类别不能为空！')</script>");
            return;
        }
        int id = 0;
        if (Request.QueryString["ParentID"] != null && Request.QueryString["ParentID"] != "")
            id = int.Parse(Request.QueryString["ParentID"].ToString());
        IHelpType RHelp = new RHelpType();
        THelpType THelp = new THelpType();
        THelp.ParentID = id;
        THelp.Name = txtType.Text.Trim();
        try
        {
            RHelp.Insert(THelp);
            Bind(); 
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.txtType.Text = string.Empty;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < gvHelpTypeList.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvHelpTypeList.Rows[i].FindControl("chkSelectAll");
                if (chk != null && chk.Checked == true)
                {
                    int id = int.Parse(gvHelpTypeList.DataKeys[i]["ID"].ToString());
                    IHelpType RHelp = new RHelpType();
                    RHelp.Delete(id);
                }
            }
            Bind();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }
    protected void gvHelpTypeList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvHelpTypeList.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void gvHelpTypeList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int id = int.Parse(gvHelpTypeList.DataKeys[e.RowIndex]["ID"].ToString());
            string name = ((TextBox)gvHelpTypeList.Rows[e.RowIndex].FindControl("txtName")).Text;
            THelpType THelp = new THelpType();
            THelp.ID = id;
            THelp.Name = name;
            IHelpType RHelp = new RHelpType();
            RHelp.update(THelp);
            gvHelpTypeList.EditIndex = -1;
            Bind();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }
    protected void gvHelpTypeList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvHelpTypeList.EditIndex = -1;
        Bind();
    }
    protected void gvHelpTypeList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id = int.Parse(gvHelpTypeList.DataKeys[e.RowIndex]["ID"].ToString());
            IHelpType RHelp = new RHelpType();
            RHelp.Delete(id);
            Bind();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

    public bool IsCount(string s)
    {
        if (!s.Equals("0"))
        {
            return true;
        }
        else
            return false;
    }

    private int pageSize = 12;//这里设置分页尺寸
    #region
    public int Pageindex
    {
        //页下标属性
        set
        {
            this.ViewState["pgindex"] = value;
        }
        get
        {
            int index = 1;
            object o = this.ViewState["pgindex"];
            if (o != null)
            {
                index = (int)o;
            }
            return index;
        }
    }
    public int PageCount
    {
        //总页数
        set { this.ViewState["pagecount"] = value; }
        get
        {
            int pagecot = 1;
            object od = this.ViewState["pagecount"];
            if (od != null)
            {
                pagecot = (int)od;
            }
            return pagecot;
        }
    }
    #endregion

    #region 获取数据并分页
    private void pbind(string strWhere, int Pageindex)
    {
        string table = "W_HelpType a";
        string select = "a.ID,a.Name,a.ParentID,(select count(*) from W_HelpType where ParentID=a.ID) as time";
        DataTable dt = PageList.GetDataTable(strWhere, table, 1, "a.ID", select, pageSize, Pageindex, 0);
        if (dt.Rows.Count == 0)
        {

        }
        DataTable dtb = Base.PageList.GetDataTable(strWhere, table);
        PageCount = Convert.ToInt32(dtb.Rows[0]["Total"]);
        this.txt_pageCount.Text = PageCount.ToString();
        if (Pageindex == 1)
        {
            this.btn_pageFirst.Enabled = false;
            this.btn_pageLast.Enabled = false;
        }
        else
        {
            this.btn_pageFirst.Enabled = true;
            this.btn_pageLast.Enabled = true;
        }
        if (Pageindex == (PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1))
        {
            this.btn_pageNext.Enabled = false;
            this.btn_pageFoot.Enabled = false;
        }
        else
        {
            this.btn_pageNext.Enabled = true;
            this.btn_pageFoot.Enabled = true;
        }
        if (PageCount == 0)
        {
            this.btn_pageNext.Enabled = false;
            this.btn_pageFoot.Enabled = false;
        }
        this.txt_begin.Text = Pageindex.ToString();
        this.txt_pageAll.Text = Convert.ToString((PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1));
        this.txt_PageNum.Text = Pageindex.ToString();
        gvHelpTypeList.DataSource = dt.DefaultView;
        gvHelpTypeList.DataBind();
    }
    #endregion

    #region 分页事件
    protected void btn_pageFirst_Click(object sender, EventArgs e)
    {
        //首页事件
        this.Pageindex = 1;
        Bind();
    }
    protected void btn_pageLast_Click(object sender, EventArgs e)
    {
        //上一页
        this.Pageindex--;
        Bind();
    }
    protected void btn_pageNext_Click(object sender, EventArgs e)
    {
        //下一页
        this.Pageindex++;
        Bind();
    }
    protected void btn_pageFoot_Click(object sender, EventArgs e)
    {
        //末页
        this.Pageindex = PageCount % pageSize == 0 ? PageCount / pageSize : PageCount / pageSize + 1;
        Bind();
    }
    protected void btn_go_Click(object sender, EventArgs e)
    {
        //跳到指定的页
        string txtNumber = this.txt_PageNum.Text.ToString().Trim();
        int index = 1;
        if (Base.Common.CheckNum(txtNumber))
        {
            index = Convert.ToInt32(txtNumber);
            if (index >= 1 && index <= (PageCount % pageSize == 0 ? PageCount / pageSize : PageCount / pageSize + 1))
            {
                Pageindex = index;
                Bind();
            }
            else
            {
                Pageindex = 1;
                this.txt_PageNum.Text = "输入错误";
            }
        }
        else
        {
            Pageindex = 1;
            this.txt_PageNum.Text = "输入错误";
        }
    }
    #endregion
    protected void lbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("HelpTypeList.aspx");
    }
    protected void gvHelpTypeList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (Request["ParentId"] != null && Request["ParentId"] != "")
        {
            HyperLink hlkName = e.Row.Cells[1].FindControl("hlkAdd") as HyperLink;
            if (hlkName != null)
            {
                hlkName.Enabled = false;
            }

        }
    }
    protected void gvHelpTypeList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");
            e.Row.Attributes.Add("onMouseOut", "SetOldColor(this)");
        }
    }
}