using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;
using TopWinCMS.Model.Enum;
namespace TopWinCMS.WebUI.Admin.Common
{
    /// <summary>
    ///  Class 的摘要说明。
    /// </summary>
    public partial class Class : System.Web.UI.Page
    {

        ClassManage MyClassBLL = new ClassManage();

        #region 权限变量
        private bool ContenDel = RightsCheck.HasRight("ContenDel", true);
        private bool ClassAdd = RightsCheck.HasRight("ClassAdd", true);
        private bool ClassDel = RightsCheck.HasRight("ClassDel", true);
        private bool ClassEdit = RightsCheck.HasRight("ClassEdit", true);
        #endregion

      protected  int _ModelID;
        protected void Page_Load(object sender, System.EventArgs e)
        {
            _ModelID = int.Parse(Request["ModelID"]);
            if (!IsPostBack)
            {
                switch (Request["Action"])
                {
                    case null:
                        ManageUI();
                        break;
                    case "MergeClass":
                        MergeClassUI();
                        break;
                    case "Attribute":
                        AttributeUI();
                        break;
                    case "Move":
                        MoveUI();
                        break;
                }
            }
        }

        private void ManageUI()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);

            MyClassBLL.BindList(GV_List, _ModelID);

            MyJavascript.RegJS(UpdatePanel1, "attachMouseEvent('" + GV_List.ClientID + "');");
            GV_List.Visible = true;
            Panel1.Visible = false;
            Panel2.Visible = false; Panel3.Visible = false;
            this.Title = "栏目管理";
        }

        private void MergeClassUI()
        {
            RightsCheck.CheckWorkRight("ContenEdit", true);


            MyClassBLL.BindDDL(DDL_OldClass, Class_BindOption.NoLink, "<请选择>", _ModelID);
            MyClassBLL.BindDDL(DDL_NewClass, Class_BindOption.NoLink, "<请选择>", _ModelID);

            if (ClassDel == false)
            {
                CB_DeleteClass.Enabled = false;
                CB_DeleteClass.Checked = false;
            }

            GV_List.Visible = false;
            Panel1.Visible = true;
            Panel2.Visible = false;
            Panel3.Visible = false;
            this.Title = "合并栏目";
        }

        private void AttributeUI()
        {
            MyClassBLL.BindDDL(LB_Class, Class_BindOption.NoLink, null, _ModelID);
            GV_List.Visible = false;
            Panel1.Visible = false;
            Panel2.Visible = true; Panel3.Visible = false;
            this.Title = "批量设置";
        }

        private void MoveUI()
        {
            MyClassBLL.BindDDL(this.LB_OldClass, Class_BindOption.NoLink, null, _ModelID);
            MyClassBLL.BindDDL(this.LB_NewClass, Class_BindOption.NoLink, null, _ModelID);
            GV_List.Visible = false;
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;

            this.Title = "移动栏目";
        }
 

        protected string LinkURL(object URL)
        {
            if (URL.ToString().Length > 0)
            {
                return string.Format("<a href=\"{0}\" target=_blank><font color=#0066FF>[外部链接]</font></a>", URL);
            }
            else
            {
                return string.Empty;
            }
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ClassID = GV_List.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();

            if (ClassManage.CheckAllowClass(ClassID, _ModelID, false) == false)
            {
                MsgBox.Alert("您不是该栏目的管理员！");

                return;
            }

            if (e.CommandName == "down")
            {
                MyClassBLL.OrderByMove(ClassID, "down", _ModelID);
                ManageUI();
            }
            if (e.CommandName == "up")
            {
                MyClassBLL.OrderByMove(ClassID, "up", _ModelID);
                ManageUI();
            }

            if (e.CommandName == "Clear")
            {
                MyClassBLL.Clear(ClassID, _ModelID, UpdatePanel1);
            }
            if (e.CommandName == "DelClass")
            {
                MyClassBLL.Delete(ClassID, _ModelID, UpdatePanel1);
                ManageUI();
            }
        }


        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[6].Attributes.Add("style", "display:none");
                e.Row.Cells[7].Attributes.Add("style", "display:none");
                e.Row.Cells[8].Attributes.Add("style", "display:none");
                e.Row.Cells[9].ColumnSpan = 4;
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell myTableCell = e.Row.Cells[4];

                switch (myTableCell.Text)
                {
                    case "0":
                        myTableCell.Text = "不生成静态";
                        break;
                    case "1":
                        myTableCell.Text = "静态规则一";
                        break;
                    case "2":
                        myTableCell.Text = "静态规则二";
                        break;
                    case "3":
                        myTableCell.Text = "静态规则三";
                        break;
                    case "4":
                        myTableCell.Text = "静态规则四";
                        break;
                }

                myTableCell = e.Row.Cells[5];

                switch (myTableCell.Text)
                {
                    case "0":
                        myTableCell.Text = "不生成静态";
                        break;
                    case "1":
                        myTableCell.Text = "静态规则一";
                        break;
                    case "2":
                        myTableCell.Text = "静态规则二";
                        break;
                    case "3":
                        myTableCell.Text = "静态规则三";
                        break;
                    case "4":
                        myTableCell.Text = "静态规则四";
                        break;
                }

                #region 权限

                if (ClassAdd == false)
                {
                    myTableCell = e.Row.Cells[6];
                    LinkButton btnAdd = (LinkButton)myTableCell.Controls[0];
                    btnAdd.Attributes.Add("onclick", "alert('您没有添加权限！');return false;");
                }
                if (ClassEdit == false)
                {
                    myTableCell = e.Row.Cells[2];//向上
                    LinkButton btnEdit = (LinkButton)myTableCell.Controls[0];
                    btnEdit.Attributes.Add("onclick", "alert('该功能需要修改权限！');return false;");

                    myTableCell = e.Row.Cells[3];//向下
                    btnEdit = (LinkButton)myTableCell.Controls[0];
                    btnEdit.Attributes.Add("onclick", "alert('该功能需要修改权限！');return false;");

                    myTableCell = e.Row.Cells[7];
                    btnEdit = (LinkButton)myTableCell.Controls[0];
                    btnEdit.Attributes.Add("onclick", "alert('您没有修改权限！');return false;");
                }
                if (ClassDel == false)
                {
                    myTableCell = e.Row.Cells[8];
                    LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
                    btnDel.Attributes.Add("onclick", "alert('您没有删除权限！');return false;");
                }
                else
                {
                    myTableCell = e.Row.Cells[8];
                    LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
                    btnDel.Attributes.Add("onclick", "return confirm('确定要删除这个栏目及其子栏目吗？');");
                }

                if (ClassDel == ContenDel == false)
                {
                    myTableCell = e.Row.Cells[9];
                    LinkButton btnClear = (LinkButton)myTableCell.Controls[0];
                    btnClear.Attributes.Add("onclick", "alert('该功能需要删除栏目权限和删除内容的权限！');return false;");
                }
                else
                {
                    TableCell myTableCell1 = e.Row.Cells[9];
                    LinkButton btnClear = (LinkButton)myTableCell1.Controls[0];
                    btnClear.Attributes.Add("onclick", "return confirm('确定要清空该栏目及其子栏目的数据吗？');");
                }
                #endregion

            }
        }

        protected void BT_Merge_Click(object sender, EventArgs e)
        {
            string Err = null;
            MyClassBLL.MergeClass(_ModelID, DDL_OldClass.SelectedValue, DDL_NewClass.SelectedValue, CB_ChildClass.Checked, CB_DeleteClass.Checked, ref  Err);

            if (string.IsNullOrEmpty(Err))
            {
                MergeClassUI();
                MsgBox.Alert("操作成功！", UpdatePanel1);
            }
            else
            {
                MsgBox.Alert(Err, UpdatePanel1);
            }
        }

        protected void CB_ChildClass_CheckedChanged(object sender, EventArgs e)
        {
            CB_DeleteClass.Enabled = CB_ChildClass.Checked;
            CB_DeleteClass.Checked = CB_ChildClass.Checked;
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            /*
             TemplateFileName
ClassIntro
ClassPic
ClassDescription
ClassKeyWord
ContentAdmin
ClassAdmin
OpenGrade
EnableContribute
EnableComment
FilePostfix
FilePrefix
URLStyle
ClassFilePostfix
ClassFilePrefix
ClassURLStyle

             */

            List<bool> Selected = new List<bool>();
            List<object> Values = new List<object>();

            Selected.Add(CB_TemplateFileName.Checked);
            Values.Add(TB_TemplateFileName.Text);

            Selected.Add(CB_ClassIntro.Checked);
            Values.Add(TB_ClassIntro.Text);

            Selected.Add(CB_ClassPic.Checked);
            Values.Add(TB_ClassPic.Text);

            Selected.Add(CB_ClassDescription.Checked);
            Values.Add(TB_ClassDescription.Text);

            Selected.Add(CB_ClassKeyWord.Checked);
            Values.Add(TB_ClassKeyWord.Text);

            Selected.Add(CB_ContentAdmin.Checked);
            Values.Add(UC_AdminSelect1.BT_SelectAdmin.Text);

            Selected.Add(CB_ClassAdmin.Checked);
            Values.Add(UC_AdminSelect2.BT_SelectAdmin.Text);

            Selected.Add(CB_OpenGrade.Checked);
            Values.Add(RBL_OpenGrade.SelectedValue);

            Selected.Add(CB_EnableContribute.Checked);
            Values.Add(RBL_EnableContribute.SelectedValue);

            Selected.Add(CB_EnableComment.Checked);
            Values.Add(RBL_EnableComment.SelectedValue);

            Selected.Add(CB_FilePostfix.Checked);
            Values.Add(TB_FilePostfix.Text);

            Selected.Add(CB_FilePrefix.Checked);
            Values.Add(TB_FilePrefix.Text);

            Selected.Add(CB_URLStyle.Checked);
            Values.Add(RBL_URLStyle.SelectedValue);

            Selected.Add(CB_ClassFilePostfix.Checked);
            Values.Add(TB_ClassFilePostfix.Text);

            Selected.Add(CB_ClassFilePrefix.Checked);
            Values.Add(TB_ClassFilePrefix.Text);

            Selected.Add(CB_ClassURLStyle.Checked);
            Values.Add(RBL_ClassURLStyle.SelectedValue);

            MyStringBuilder IDs = new MyStringBuilder();
            foreach (ListItem li in this.LB_Class.Items)
            {
                if (li.Selected)
                    IDs.Append(li.Value + ",");
            }
            IDs.TrimEnd();

            MyClassBLL.BatchUpData(Selected, Values, IDs.ToString(), _ModelID, UpdatePanel1);
        }

        protected void BT_Move_Click(object sender, EventArgs e)
        {
            object Err;
            MyClassBLL.Move(LB_NewClass.SelectedValue, GetSelectedRow.IDs(LB_OldClass), _ModelID, out Err);

            if (Err == null || Err.ToString().Length == 0)
            {
                MoveUI();
                MsgBox.Alert("操作成功！", UpdatePanel1);
            }
            else
            {
                MsgBox.Alert(Err.ToString(), UpdatePanel1);
            }
        }
    }
}