using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TopWinCMS.Common;
using TopWinCMS.BLL;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.AdminMenu
{
    public partial class Menu_Manage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["Action"] == null)
                {
                    Bind();


                    Panel1.Visible = true;
                    Panel2.Visible = false;
                }
                else
                {
                    Bind2();
                }
            }
        }
        private void Bind()
        {
            new AdminMenuDefaultManage().GetMenuList(GV_List);
        }
        private void Bind2()
        {
            GV_List2.DataSource = new AdminMenuHeadManage().DataList();
            GV_List2.DataBind();
            Panel1.Visible = false;
            Panel2.Visible = true;
        }



        protected void GV_List2_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow)
            {
                return;
            }
            TableCell myTableCell = e.Row.Cells[2];

            LinkButton btnShow = (LinkButton)myTableCell.Controls[0];
            string strTemp = btnShow.Text.Trim();
            bool b = Convert.ToBoolean(strTemp);
            if (b)
            {
                btnShow.Text = "显示";
                btnShow.Attributes.Add("onclick", "return confirm('确定隐藏吗？');");
            }
            else
            {
                btnShow.Text = "隐藏";
                btnShow.Attributes.Add("onclick", "return confirm('确定要显示吗？');");
            }

        }

        protected void GV_List2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int Index = Convert.ToInt32(e.CommandArgument);
            AdminMenuHeadInfo model = new AdminMenuHeadInfo();

            model.ID = GV_List2.DataKeys[Index].Value.ToString();

            TableCell myTableCell = GV_List2.Rows[Index].Cells[2];
            LinkButton btnShow = (LinkButton)myTableCell.Controls[0];
            if (btnShow.Text == "显示")
            {
                model.Show = false;
            }
            else
            {
                model.Show = true;
            }

            string strErr = null;

            new AdminMenuHeadManage().Edit(model, out strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
            }
            else
            {
                string str = new AdminMenuHeadManage().GetMenu();
                str = str.Replace("'", @"\'");
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "AdminMenuHead", "AdminMenuHead('" + str + "');", true);

                Bind2();
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = GV_List.DataKeys[e.RowIndex].Value.ToString();
            new AdminMenuDefaultManage().Delete(ID);
            
            Bind();
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_List.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
           
            AdminMenuDefaultInfo model = new AdminMenuDefaultInfo();
            model.Title = (GV_List.Rows[e.RowIndex].FindControl("TB_Title") as TextBox).Text;
            model.Link = (GV_List.Rows[e.RowIndex].FindControl("TB_Link") as TextBox).Text;
            model.UserName = AdminLoginInfo.UserName;
            model.OrderID = (GV_List.Rows[e.RowIndex].FindControl("TB_OrderID") as TextBox).Text;
            model.ID = GV_List.DataKeys[e.RowIndex].Value.ToString();

            string strErr = null;

            new AdminMenuDefaultManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
                return;
            }
             



            GV_List.EditIndex = -1;
            Bind();
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_List.EditIndex = -1;
            Bind();
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            Add();

            Bind();
        }

        #region 添加 
        private void Add()
        {

            AdminMenuDefaultInfo model = new AdminMenuDefaultInfo();
            model.Title = TB_Title.Text.Trim();
            model.Link = TB_Link.Text.Trim();
            model.UserName = AdminLoginInfo.UserName;
            model.OrderID = TB_OrderID.Text;
            model.ID = "0";

            string strErr = null;

            new AdminMenuDefaultManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
                return;
            }
            
        }
        #endregion
    }
}