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

namespace TopWinCMS.WebUI.Admin
{
    /// <summary>
    /// Admin_Announce 的摘要说明。
    /// </summary>
    public partial class Admin_Announce : System.Web.UI.Page
    {
        AnnounceManage MyClassBLL = new AnnounceManage();
        AnnounceInfo model = new AnnounceInfo();
        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 AnnounceManage().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,Author,Content,[DateTime]";
            s_model.TableName = "TopWinCMS_Announce";
            s_model.OrderField = "[DateTime] 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.Title = TB_Title.Text.Trim();
            model.Author = AdminLoginInfo.UserName;
            model.Content = CE_Content.Text;
            model.Pass = CB_Pass.Checked == true ? 1 : 0;
            model.DateTime = DateTime.Now ;

            MyClassBLL.Add(model);
        }

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

            MyClassBLL.Edit(model);
        }
        protected void BT_Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("Announce.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\"> ";
                }
            }
        }

    }
}