using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TopWinCMS.BLL;
using TopWinCMS.Common;
using TopWinCMS.Model;
namespace TopWinCMS.WebUI.Admin.Common
{
    public partial class TopWinCMS_ContentPropertyManage : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();
        int ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ModelID = int.Parse(Request["ModelID"]);
            if (!IsPostBack)
            {
                RightsCheck.CheckWorkRight("OP", true);
                BindList();

                string strPath = ModelManage.GetModelName(ModelID);

                ContentPropertyOption CPO = (ContentPropertyOption)int.Parse(Request["Type"]);
                switch (CPO)
                {
                    //1文章作者，2文章来源，3软件作者\公司，4软件作者\公司的电邮,
                    //5软件解压密码
                    case ContentPropertyOption.文章作者:
                        strPath += string.Format(" → {0}作者", ModelManage.GetContentName(ModelID));
                        this.Title = string.Format("{0}作者管理", ModelManage.GetContentName(ModelID));
                        break;
                    case ContentPropertyOption.文章来源:
                        strPath += string.Format(" → {0}来源", ModelManage.GetContentName(ModelID));
                        this.Title = string.Format("{0}来源管理", ModelManage.GetContentName(ModelID));
                        break;
                    case ContentPropertyOption.软件作者:
                        strPath += string.Format(" → {0}作者", ModelManage.GetContentName(ModelID));
                        this.Title = string.Format("{0}作者", ModelManage.GetContentName(ModelID));
                        break;
                    case ContentPropertyOption.软件作者的电邮:
                        strPath += string.Format(" → {0}作者电邮", ModelManage.GetContentName(ModelID));
                        this.Title = string.Format("{0}作者电邮", ModelManage.GetContentName(ModelID));
                        break;
                    case ContentPropertyOption.软件解压密码:
                        strPath += string.Format(" → {0}解压密码", ModelManage.GetContentName(ModelID));
                        this.Title = string.Format("{0}解压密码", ModelManage.GetContentName(ModelID));
                        break;
                }
                this.LB_Path.Text = strPath;
                
            }
        }


        public void BindList()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);
            s_model.WHERE = " ModelID=" + ModelID + " and Type=" + Request["Type"];
            s_model.PageSize = 10;

            new ContentPropertyManage().BindList(GV_List, this.PageNavigation1, s_model);
        }

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

        protected void BT_Add_Click(object sender, EventArgs e)
        {
            ContentPropertyInfo model = new ContentPropertyInfo();
            model.ID = "0";
            model.ModelID = ModelID.ToString();
            model.Name = this.TB_Name.Text.Trim();
            model.Type = int.Parse(Request["Type"]);

            string strErr = null;
            new ContentPropertyManage().Add(model, ref strErr);


            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Alert(strErr, UpdatePanel1);
            }
            else
            {
                s_model.PageNo = 1;
                BindList();
            }
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GV_List.EditIndex = e.NewEditIndex;
            s_model.PageNo = PageNavigation1.PageNo;
            BindList();
        }

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

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string strID = GV_List.DataKeys[e.RowIndex].Value.ToString();

            ContentPropertyInfo model = new ContentPropertyInfo();
            model.ID = strID;

            model.Name = ((TextBox)GV_List.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            model.UpDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            string strErr = string.Empty;
            new ContentPropertyManage().Add(model, ref strErr);

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

            GV_List.EditIndex = -1;



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

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV_List.EditIndex = -1;
            s_model.PageNo = PageNavigation1.PageNo;
            BindList();
        }

    }
}