using System;
using System.Collections.Generic;
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.Common;
using TopWinCMS.BLL;
using TopWinCMS.Model;

namespace TopWinCMS.WebUI.Admin.Shop
{
    public partial class Brand : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();
        BrandManage MyClassBLL = new BrandManage();
        int ModelID;
        protected void Page_Load(object sender, EventArgs e)
        {
            ModelID = int.Parse(Request.QueryString["ModelID"]);
            if (!IsPostBack)
            {
                if (Request["Action"] == null && Request["ID"] == null)
                {
                    LB_Path.Text = string.Format("{0} → 参数设置 → <a href='Brand.aspx?ModelID={1}'>品牌管理</a>", ModelManage.GetModelName(ModelID), ModelID);
                    Panel1.Visible = false;
                    Panel2.Visible = true;
                    BindList();
                }
                else if (Request["ID"] != null && Request["Action"] == "Eidt")
                {
                    LB_Path.Text = string.Format("{0} → 参数设置 → <a href='Brand.aspx?ModelID={1}'>品牌管理</a> → 修改", ModelManage.GetModelName(ModelID), ModelID);
                    //“修改”界面
                    BT_OK.Text = "修改";
                    Edit_Load();

                    Panel1.Visible = true;
                    Panel2.Visible = false;
                }
                else if (Request["ID"] == null && Request["Action"] == "Add")
                {
                    LB_Path.Text = string.Format("{0} → 参数设置 → <a href='Brand.aspx?ModelID={1}'>品牌管理</a> → 添加", ModelManage.GetModelName(ModelID), ModelID);
                    //“添加”界面
                    BT_OK.Text = "添加";
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                }
            }
        }

        #region 添加或修改
        private void Add()
        {

            BrandInfo model = new BrandInfo();

            model.Name = TB_Name.Text.Trim();
            model.SrcList = TB_SrcList.Text.Trim();
            model.SrcDetail = TB_SrcDetail.Text.Trim();
            model.Website = TB_Website.Text.Trim();

            model.Content = TB_Content.Text.Trim();
            model.IsShow = CB_IsShow.Checked == true ? 1 : 0;
            model.Title = TB_Title.Text.Trim();
            model.Keywords = TB_Keywords.Text.Trim();
            model.Description = TB_Description.Text.Trim();
            model.Pinyin = TB_Pinyin.Text.Trim();
            model.Company = TB_Company.Text.Trim();
            if (Request["ID"] == null)
            {
                model.ID = 0;
            }
            else if (Request["ID"] != null && Request["Action"] == "Edit")
            {
                model.ID = int.Parse(Request["ID"]);
            }

            string strErr = null;

            new BrandManage().Add(model, ref strErr);

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

        }
        #endregion

        #region 加载
        private void Edit_Load()
        {
            BrandInfo model = new BrandManage().EditLoad(int.Parse(Request["ID"]));
            if (model != null)
            {

                TB_Name.Text = model.Name;
                TB_SrcList.Text = model.SrcList;
                TB_SrcDetail.Text = model.SrcDetail;
                TB_Website.Text = model.Website;

                TB_Content.Text = model.Content;
                CB_IsShow.Checked = model.IsShow == 1 ? true : false;
                TB_Title.Text = model.Title;
                TB_Keywords.Text = model.Keywords;
                TB_Description.Text = model.Description;
                TB_Pinyin.Text = model.Pinyin;
                TB_Company.Text = model.Company;
            }
            else
            {
                //转向错误页面
                ErrClass.Redirect("数据不存在。");
            }
        }
        #endregion

        private void BindList()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);
            s_model.Fields = "*";
            s_model.TableName = "TopWinCMS_Brand";
            s_model.OrderField = "ID Desc";


            s_model.PageSize = 15;

            MyClassBLL.BindList(GV_List, PageNavigation1, s_model);

            if (GV_List.Rows.Count <= 0)
            {

                BT_DelSeletedID.Enabled = false;
            }
        }

        protected void BT_OK_Click(object sender, EventArgs e)
        {
            Add();
        }

        protected void PageNavigation1_PageChanging(object src, TopWinCMS.WebControls.PageChangingEventArgs e)
        {

            s_model.PageNo = e.NewPageIndex;
            BindList();
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lb = e.Row.FindControl("LB_IsShow") 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\"> ";
                }

                TableCell myTableCell = e.Row.Cells[GV_List.Columns.Count - 1];
                if (myTableCell.Controls.Count > 0)
                {
                    LinkButton btnDel = (LinkButton)myTableCell.Controls[0];

                    //if 
                    //{
                    btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");
                    //}
                    //else
                    //{
                    //    btnDel.Attributes.Add("onclick", "alert('您没有删除权限？');return false");
                    //}
                }
            }
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MyClassBLL.Delete(GV_List.DataKeys[e.RowIndex].Value.ToString(), UpdatePanel1);
            BindList();
        }

        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "IsShow")
            {
                int ID = (int)e.CommandArgument;

                BrandManage MyClassBLL = new BrandManage();

                BrandInfo model = MyClassBLL.EditLoad(ID);
                model.IsShow = (1 - model.IsShow);
                string Err = null;
                MyClassBLL.Add(model, ref Err);


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