using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using SysCore;
using SysCore.Control;

public partial class M_Order_Cart_List_Load : System.Web.UI.Page
{
    public Hashtable xview = new Hashtable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            new LoginState().CheckLogin();
            CreateGrid();
        }
    }
    string sql_list = "";
    /// <summary>
    /// 绑定控件数据源
    /// </summary>
    public void CreateGrid()
    {
        int uid = new LoginInfo().uid;
        //列表查询用的sql
        sql_list = @"
            select ct.cart_id,ct.quantity,ct.tax_unit_price,i.i_name from Sale_Cart ct
            left join Base_Inventory i on ct.i_id=i.i_id
            where ct.uid=" + uid;

        int page = 1;//当前页
        if (Request["page"] != null)
            page = Convert.ToInt32(Request["page"]);
        int page_size = new PhoneConfig().default_page_size;//每页显示条数

        Hashtable ht = new Hashtable();
        //排序
        string order_field = "order by cart_id desc";
        ht.Add("order_field", order_field);
        ht.Add("page", page);
        ht.Add("page_size", page_size);
        //查询条件
        string sql_end = "";
        if (!Request["keyword"].Equals(""))
        {
            //要搜索关键字
            string keyword = Request["keyword"].ToString();
            //要搜索的字段
            string search_fields = "i_name";
            sql_end = FastSearh.MakeSearchSqlForWhere(keyword, search_fields);
        }


        ht.Add("sql_end", sql_end);
        //查询后返回DataTable
        DataTable dt = GetList(ht);
        string append = @"<table style='width:100%;'>
                        <tr style='color:black'><td style='width:50%;'>产品</td><td style='width:80px'>单价</td><td style='width:80px'>数量</td><td style='width:80px'>操作</td></tr>";
        foreach (DataRow dr in dt.Rows)
        {
            string html = @"
                        <tr><td style='height:50px'>
                        @i_name</td><td>@tax_unit_price</td><td><input type='input' value='@quantity' style='width:30px' onblur='change_cart(@cart_id,this.value)'/></td><td><a href='javascript:delete_cart(@cart_id)'>删除</a></td></tr>";
            html = html.Replace("@cart_id", dr["cart_id"].ToString())
                       .Replace("@i_name", dr["i_name"].ToString())
                       .Replace("@quantity", dr["quantity"].ToString())
                       .Replace("@tax_unit_price", dr["tax_unit_price"].ToString());
            append += html;

        }
        Response.Write(append + "</table>");
    }

    /// <summary>
    /// 得到列表
    /// </summary>
    /// <param name="sql_select_field">格式定义指定显示列</param>
    /// <returns>DataTable</returns>
    public DataTable GetList(Hashtable pg)
    {
        string order_field = pg["order_field"].ToString();
        string sql = @"select top 1000000000 ROW_NUMBER() OVER(@order_field) AS row_num,* 
                from(@sql_list)t where 1=1 ";
        sql = sql.Replace("@sql_list", sql_list)
                 .Replace("@order_field", order_field);
        //分页处理与查询条件
        PageHelper ph = new PageHelper();
        sql = ph.GetPageSQL(sql, pg, true);//得到分页后的SQL
        //Response.Write(sql);
        //Response.End();
        DataTable dt = new SqlUtil().GetDataTable(sql, null);
        return dt;
    }
}