using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
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.Shop
{
    public partial class Order : System.Web.UI.Page
    {
        SQLModel s_model = new SQLModel();

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

        OrdersManage MyClassBLL = new OrdersManage();
        protected void Page_Load(object sender, EventArgs e)
        {
            bool b = int.TryParse(Request["ModelID"], out  ModelID);
            if (b == false)
            {
                ErrClass.Redirect("缺少 ModelID 参数。");
            }
            ContentName = ModelManage.GetContentName(ModelID);
            BaseModel = ModelManage.GetBaseModel(ModelID);

            if (!IsPostBack)
            {

                BindList();
            }
        }

        #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()
        {
            GV_List.EmptyDataTemplate = TopWinCMS.WebFactory.PageBase.GetTemplate(this);


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

            #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 (this.DDL_Status.SelectedValue != "-1")
                {
                    s_model.WHERE += " and Status =" + DDL_Status.SelectedValue;
                }
                if (this.DDL_DeliverStatus.SelectedValue != "-1")
                {
                    s_model.WHERE += " and DeliverStatus =" + DDL_DeliverStatus.SelectedValue;
                }

            }

            #endregion

            LB_Path.Text = strPath;

            MyClassBLL.BindList(GV_List, PageNavigation1, s_model);

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

            }
            else
            {

            }

            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, object OrderID)
        {
            StringBuilder winner = new StringBuilder();
            winner.Append(" <a href=\"#\" onclick=\"");
            //if (ContenEdit)
            {
                winner.Append(string.Concat("return NewTabPage('", ContentTitle, "','", ContentTitle, "','", "Shop/OrderList.aspx?OrderID=", OrderID, "');"));
            }
            //else
            //{
            //    winner.Append("alert ('您缺少修改权限');return false;\"");
            //}


            winner.AppendFormat(" \">{0}</a>", ContentTitle);


            return winner.ToString();
        }
        #endregion

        protected string Status(object value)
        {
            int status = Convert.ToInt32(value);


            return MyClassBLL.GetOrderStatusDescription((TopWinCMS.BLL.Shop.Status)status);
        }

        protected void BT_Search_Click(object sender, EventArgs e)
        {
            this.IsSearch = true;
            BindList();
        }

        protected void BT_Delete_Click(object sender, EventArgs e)
        {
            MyClassBLL.Delete(GetSelectedRow.IDs(GV_List), UpdatePanel1);

            BindList();
        }

        protected void BT_Cancel_Click(object sender, EventArgs e)
        {
            string IDs = GetSelectedRow.IDs(GV_List);
            foreach (string id in IDs.Split(','))
            {
                MyClassBLL.UpdateOrderStatus(int.Parse(id), TopWinCMS.BLL.Shop.Status.WaitForConfirm);
            }
            s_model.PageNo = PageNavigation1.PageNo;
            BindList();
        }

        protected void BT_Confirmed_Click(object sender, EventArgs e)
        {
            string IDs = GetSelectedRow.IDs(GV_List);
            foreach (string id in IDs.Split(','))
            {
                MyClassBLL.UpdateOrderStatus(int.Parse(id), TopWinCMS.BLL.Shop.Status.Confimed);
            }
            s_model.PageNo = PageNavigation1.PageNo;
            BindList();
        }

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

        }
    }
}
