using System;
using System.Text;
using System.Web.UI.WebControls;
using TopWinCMS.BLL;
using TopWinCMS.Common;

namespace TopWinCMS.WebUI.Admin.Common
{
    public partial class Recyclebin : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();

        RecycleBin MyClassBLL;
        /// <summary>
        /// 模型ID
        /// </summary>
        int ModelID;
        protected void Page_Load(object sender, System.EventArgs e)
        { 
            ModelID =int.Parse( Request["ModelID"]);
            MyClassBLL = new RecycleBin(ModelID);

            if (!IsPostBack)
            {
                Check();

                BindList();

                string strPath = string.Format("{0} → <a href='ContentManage.aspx?ModelID={1}'>{2}管理</a> → <a href='Recyclebin.aspx?ModelID={1}'>回收站</a>",
               ModelManage.GetModelName(ModelID),
               ModelID,
               ModelManage.GetContentName(ModelID)
               );

                LB_Path.Text = strPath;

                BT_DelSeletedID.Attributes.Add("onclick", "return confirm('注意，删除后将不可恢复！');");
                BT_DelALL.Attributes.Add("onclick", "return confirm('注意，删除后将不可恢复！');");
               　
            }
        }

        #region 检查权限
        /// <summary>
        /// 检查权限
        /// </summary>
        private void Check()
        {
            RightsCheck.CheckWorkRight("ContenDel", true);

        }
        #endregion

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

            MyClassBLL.BindList(this.GV_List, PageNavigation1, s_model);

            if (this.GV_List.Rows.Count <= 0)
            {
                BT_DelSeletedID.Enabled = false;
                BT_DelALL.Enabled = false;
                BT_TrunSelectID.Enabled = false;
                BT_TrunAll.Enabled = false;
            }

            MyJavascript.RegJS(UpdatePanel1, "attachMouseEvent('" + this.GV_List.ClientID + "');");
        }

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

            MyClassBLL.DeleteAndUndelete(GetSelectedRow.IDs(this.GV_List), 1, UpdatePanel1);
            BindList();
        }

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

            MyClassBLL.DeleteAndUndelete(0.ToString(), 4, UpdatePanel1);
            BindList();
        }

        protected void TrunSelectID_Click(object sender, System.EventArgs e)
        {
            MyClassBLL.DeleteAndUndelete(GetSelectedRow.IDs(this.GV_List),3, UpdatePanel1);
            BindList();
        }

        protected void TrunAll_Click(object sender, System.EventArgs e)
        {
            MyClassBLL.DeleteAndUndelete(0.ToString(), 2, UpdatePanel1);
            BindList();
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = this.GV_List.DataKeys[e.RowIndex].Value.ToString();

            MyClassBLL.DeleteAndUndelete(ID, 1, UpdatePanel1);

            BindList();
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Disdeleted")
            {
                int Index = Convert.ToInt32(e.CommandArgument);
                string ID = this.GV_List.DataKeys[Index].Value.ToString();

                MyClassBLL.DeleteAndUndelete(ID,3, UpdatePanel1);
                BindList();
            }
        }
        protected string MyLink(object ContentTitle, int intLong, object ID, object Hits)
        {
            string strTitle = MyString.Left(ContentTitle.ToString(), intLong);

            StringBuilder winner = new StringBuilder();
            winner.Append("<a onclick='return false;' href='#'");
            winner.Append(" title=\"标题：");
            winner.Append(ContentTitle);
            winner.Append("\n点 击 数：");
            winner.Append(Hits);
            winner.Append("\">");
            winner.Append(strTitle);
            winner.Append("</a>");

            return winner.ToString();
        }

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