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

namespace TopWinCMS.WebUI.Admin.Ask
{
    /// <summary>
    /// 问题管理界面
    /// </summary>
    public partial class AskManage : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();

        /// <summary>
        /// 模型ID
        /// </summary>
        int ModelID = 5;
        /// <summary>
        /// 内容名称
        /// </summary>
        string ContentName;

        #region 权限变量
        //private bool ContenReadOnly = RightsCheck.CheckPower("ContenReadOnly",true );
        private bool ContenDel = RightsCheck.HasRight("ContenDel", true);
        private bool ContenEdit = RightsCheck.HasRight("ContenEdit", true);
        private bool ContentPass = RightsCheck.HasRight("ContentPass", true);
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            ContentName = ModelManage.GetContentName(ModelID);
            if (!IsPostBack)
            {
                Check();

                this.Title = ContentName + "管理";
                CB_MyWork.Text += ContentName;

                new ClassManage().BindDDL(DDL_ClassID1, Class_BindOption.NoLink, "跳转栏目至...", ModelID);

                BindList();
            }
        }

        #region 进行各种权限检查和限制
        /// <summary>
        /// 进行各种权限检查和限制
        /// </summary>
        private void Check()
        {
            DDL_DoSomeThing.Items[1].Enabled = ContenDel;//删除
            DDL_DoSomeThing.Items[2].Enabled = ContenEdit;//置顶
            DDL_DoSomeThing.Items[3].Enabled = ContenEdit;//取消置顶
            DDL_DoSomeThing.Items[4].Enabled = ContenEdit;//设为推荐
            DDL_DoSomeThing.Items[5].Enabled = ContenEdit;//取消推荐


            DDL_DoSomeThing.Items[6].Enabled = ContentPass;//审核
            DDL_DoSomeThing.Items[7].Enabled = ContentPass;//取消审核
            DDL_DoSomeThing.Items[8].Enabled = ContentPass;//全部审核

            if (ContenDel == ContenEdit == ContentPass == false)
            {
                DDL_DoSomeThing.Items.Add(new ListItem("没有您能执行的操纵", "0"));
            }
        }
        #endregion

        #region 网页属性
        /// <summary>
        /// 是否处于搜索状态
        /// </summary>
        private bool IsSearch
        {
            get
            {
                if (ViewState["IsSearch"] != null)
                {
                    return Convert.ToBoolean(ViewState["IsSearch"]);
                }
                else
                {
                    return false;
                }
            }
            set
            {
                ViewState["IsSearch"] = value;
            }
        }
        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindList()
        {
            s_model.Fields = "ID,ClassID,ClassName,ContentTitle,Hits,State,UpdateTime, Editor,Pass";
            s_model.OrderField = "ID DESC";
            ContentManageManage MyClassBLL = new ContentManageManage(ModelID);

            string _ContentName = ModelManage.GetContentName(ModelID);

            s_model.WHERE = " Deleted=0";
            string strPath = string.Format("{0} → <a href='ContentManage.aspx?ModelID={1}'>{2}管理</a>",
               ModelManage.GetModelName(ModelID),
               ModelID,
               _ContentName
               );

            #region 构建查询条件
            if (this.IsSearch == true)
            {
                string strKeyWord = TB_KeyWord.Text.Trim();
                strKeyWord = MyString.SQL_ESC(strKeyWord);

                s_model.WHERE += " and " + DDL_Sort.SelectedValue + " like '%" + strKeyWord + "%'";

                strPath += string.Format(" → 搜索“{0}”", TB_KeyWord.Text.Trim());
            }
            if (DDL_ClassID1.SelectedValue != "0")
            {
                s_model.WHERE += @" and ClassID in(" + ClassManage.GetChildClass(int.Parse(DDL_ClassID1.SelectedValue)) + ")";

                strPath += new ClassManage().ClassPath(int.Parse( DDL_ClassID1.SelectedValue));
            }
            if (this.CB_MyWork.Checked == true)
            {
                s_model.WHERE += " and Editor='" + AdminLoginInfo.UserName + "'";

                strPath += string.Format(" → 我的{0}", _ContentName);
            }

            if (CB_State0.Checked == true)
            {
                s_model.WHERE += " and  State=0";

                strPath += " → 未解决";
            }
            else
            {
                if (CB_OnTop.Checked == true)
                {
                    s_model.WHERE += " and OnTop=1";

                    strPath += string.Format(" → 置顶{0}", _ContentName);
                }
                if (CB_Elite.Checked == true)
                {
                    s_model.WHERE += " and Elite=1";

                    strPath += string.Format(" → 推荐{0}", _ContentName);
                }

                if (CB_Passed.Checked == true && CB_UnPass.Checked == false)
                {
                    s_model.WHERE += " and Pass=1";

                    strPath += " → 已审核";
                }
                if (CB_Passed.Checked == false && CB_UnPass.Checked == true)
                {
                    s_model.WHERE += " and Pass=0";

                    strPath += " → 未审核";
                }
            }
            #endregion

            LB_Path.Text = strPath;

            MyClassBLL.BindList(GV_List, PageNavigation1, s_model);

            if (GV_List.Items.Count <= 0)
            {
                DDL_DoSomeThing.Enabled = false;
                BT_ToDo.Enabled = false;

            }
            else
            {
                DDL_DoSomeThing.Enabled = true;
                BT_ToDo.Enabled = true;

            }

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

        #region 动态修改链接
        /// <summary>
        /// 动态修改链接
        /// </summary>
        /// <param name="ContentTitle"></param>
        /// <param name="intLong"></param>
        /// <param name="ID"></param>
        /// <param name="Hits"></param>
        /// <returns></returns>
        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 href=\"#\" onclick=\"");
            if (ContenEdit)
            {
                winner.Append(string.Concat("return NewTabPage('修改", ContentName, "_", ContentTitle, "','修改", ContentName, "','", "Ask/Answer.aspx?AskID=", ID, "&ModelID=", ModelID + "');\""));
            }
            else
            {
                winner.Append("alert ('您缺少修改权限');return false;\"");
            }

            winner.AppendFormat(" title=\"　　ＩＤ：{0}", ID);
            winner.AppendFormat("&#13;&#10;　　标题：{0}", ContentTitle);
            winner.AppendFormat("&#13;&#10;点 击 数：{0}\">{1}</a>", Hits, strTitle);


            return winner.ToString();
        }
        #endregion

        protected void BT_ToDo_Click(object sender, System.EventArgs e)
        {
            new ContentManageManage(ModelID).BatchToDo(ModelID, GetSelectedRow.IDs(GV_List), DDL_DoSomeThing.SelectedValue, UpdatePanel1);

            if (DDL_DoSomeThing.SelectedValue == "批量删除" || DDL_DoSomeThing.SelectedValue == "批量推荐" || DDL_DoSomeThing.SelectedValue == "取消审核" || DDL_DoSomeThing.SelectedValue == "全部审核")
            {
                s_model.PageNo = PageNavigation1.PageNo;
                BindList();
            }
        }


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

        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (this.IsSearch == true || PageNavigation1.PageNo > 1)
            {
                return;
            }
            try
            {
                s_model.PageNo = PageNavigation1.PageNo;
                BindList();
            }
            catch { }
        }

        protected void BT_Search_Click(object sender, EventArgs e)
        {

            if (TB_KeyWord.Text.Length == 0)
            {
                Response.Redirect("ContentManage.aspx?ModelID=" + ModelID);
            }
            else
            {
                this.IsSearch = true;
                BindList();
            }
        }



        protected void DataFilter(object sender, EventArgs e)
        {
            if (CB_Passed.Checked == true && CB_UnPass.Checked == true)
            {
                CB_Passed.Checked = false; CB_UnPass.Checked = false;
            }
            s_model.PageNo = 1;
            PageNavigation1.PageNo = 1;
            BindList();
        }

        protected void GV_List_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Pass")
            {
                Label lb = e.Item.FindControl("LB_ID") as Label;

                new ContentManageManage(ModelID).PassOrNo(lb.Text, UpdatePanel1);

                s_model.PageNo = PageNavigation1.PageNo;
                BindList();
            }
        }

        protected void GV_List_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                LinkButton lb = e.Item.FindControl("LB_Pass") as LinkButton;
                if (lb.Text == "1")
                {
                    lb.Text = "<IMG alt=\"已审核\" src=\"../Images/success-true.gif\"> ";
                }
                else if (lb.Text == "0")
                {
                    lb.Text = "<IMG alt=\"未审核\" src=\"../Images/failed-false.gif\"> ";
                }

                if (ContenEdit == false)
                {
                    lb.Attributes.Add("onclick", "alert('你没有审核权限');return false;");
                }
            }
        }

    }
}
