using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;


namespace TopWinCMS.WebUI.Admin.ControlPanel
{
    public partial class Help : System.Web.UI.Page
    {
        HelpManage MyClassBLL = new HelpManage();
        HelpInfo model = new HelpInfo();
        SQLModel s_model = new SQLModel();

        protected void Page_Load(object sender, System.EventArgs e)
        {

            //RightsCheck.CheckWorkPower("P_4");

            if (!IsPostBack)
            {
                string Action = Request["Action"];
                if (Action == "Add")
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    BT_Edit.Visible = false;
                }
                else if (Action == "Edit")
                {
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    BT_Edit.Visible = true;
                    BT_OK.Visible = false;


                    model = new HelpManage().EditLoad(Request["ID"]);
                    TB_Title.Text = model.Title;
                    CB_Pass.Checked = model.Pass == 1 ? true : false;
                    CE_Content.Text = model.Content;
                }

                else
                {

                    BindList();
                }
            }
        }

        private void BindList()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);
            s_model.Fields = "Pass,ID,Title,Admin,Content,CheckInDate";
            s_model.TableName = "TopWinCMS_Help";
            s_model.OrderField = "CheckInDate Desc";


            s_model.PageSize = 10;

            MyClassBLL.BindList(GV_List, PageNavigation1, s_model);

            if (GV_List.Rows.Count <= 0)
            {

                DelSeletedID.Enabled = false;
            }
        }

        protected void DelSeletedID_Click(object sender, System.EventArgs e)
        {
            MyClassBLL.Delete(GetSelectedRow.IDs(GV_List), UpdatePanel1);
            BindList();
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {
            s_model.PageNo = e.NewPageIndex;
            BindList();
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            model.ID ="0";
            model.Title = TB_Title.Text.Trim();
            model.Admin = AdminLoginInfo.UserName;
            model.Content = CE_Content.Text;
            model.Pass = CB_Pass.Checked == true ? 1 : 0;
            model.CheckInDate = DateTime.Now;

            string Err = null;
            MyClassBLL.Add(model, ref Err);

            if (string.IsNullOrEmpty(Err))
            {
                Panel1.Visible = false;
                Panel2.Visible = true;

                s_model.PageNo = 1;
                BindList();
            }
            else
            {
                MsgBox.Alert(Err, this.UpdatePanel1);
            }
        }

        protected void BT_Edit_Click(object sender, EventArgs e)
        {
            model.ID = Request["ID"];
            model.Title = TB_Title.Text;
            model.Admin = AdminLoginInfo.UserName;
            model.Content = CE_Content.Text;
            model.Pass = CB_Pass.Checked == true ? 1 : 0;
            model.CheckInDate = DateTime.Now;

            string Err = null;
            MyClassBLL.Add(model, ref Err);
            if (string.IsNullOrEmpty(Err))
            {
                Panel1.Visible = false;
                Panel2.Visible = true;

                s_model.PageNo = 1;
                BindList();
            }
            else
            {
                MsgBox.Alert(Err, this.UpdatePanel1);
            }
        }
        protected void BT_Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("Help.aspx");
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                MyClassBLL.PassOrNo(GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString());
                s_model.PageNo = PageNavigation1.PageNo;
                BindList();
            }

        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MyClassBLL.Delete(GV_List.DataKeys[e.RowIndex].Value.ToString(), UpdatePanel1);
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell tc = e.Row.Cells[4];
                LinkButton lb = tc.Controls[0] as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }
            }
        }
    }
}
