using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CitySunlight.Product;

namespace CitySunlight
{
    public partial class Main : System.Web.UI.Page
    {
        int pages = HttpUtils.getQueryString("pages").ToInt();
        public String classid = HttpUtils.getQueryString("classid");
        public String search = HttpUtils.getQueryString("search");
        public String classoption;
        const int peritems = 5;
        protected void Page_Load(object sender, EventArgs e)
        {
            classoption = ProductClassManager.getOptionString("classid", classid);
        }
        public String GetProductInfo()
        {
            String html = "";
            DataTable items = ProductManager.SearchProduct("", search, classid, "", "", pages, peritems);
            if (items.Rows.Count == 0)
                return "<tr><td style=\"color:red\" colspan=\"8\" align=\"center\">数据库中未找到产品</td></tr>";
            else
                html += String.Format("<tr>{0}</tr>",
                    HttpUtils.addTd() +
                    HttpUtils.addTd("商品名称") +
                    HttpUtils.addTd("商品图片") +
                    HttpUtils.addTd("商品价格") +
                    HttpUtils.addTd("商品数量") +
                    HttpUtils.addTd("商品分类") +
                    HttpUtils.addTd("所属卖家") +
                    HttpUtils.addTd());

            foreach (DataRow item in items.Rows)
            {
                int id = int.Parse(ProductManager.GetProductInfo(item, ProductManager.Info.ID));
                String name = ProductManager.GetProductInfo(item, ProductManager.Info.ItemName);
                String url = ProductManager.GetProductInfo(item, ProductManager.Info.PicUrl);
                String picurl = String.Format("<img src=\"../Picture/{0}\"alt=\"\" width=\"64\" height=\"64\"/>", url);
                String price = ProductManager.GetProductInfo(item, ProductManager.Info.Price);
                String amount = ProductManager.GetProductInfo(item, ProductManager.Info.Amount);
                String user = ProductManager.GetProductInfo(item, ProductManager.Info.UserName);
                String classname = ProductClassManager.getClassName(int.Parse(ProductManager.GetProductInfo(item, ProductManager.Info.Class)));

                html += String.Format("<tr>{0}</tr>",
                    HttpUtils.addTd("") +
                    HttpUtils.addTd(ProductManager.NameToUrl(id, name)) +
                    HttpUtils.addTd(picurl) +
                    HttpUtils.addTd(price) +
                    HttpUtils.addTd(amount) +
                    HttpUtils.addTd(classname) +
                    HttpUtils.addTd(user) +
                    HttpUtils.addTd(""));
            }
            decimal total = ProductManager.SearchProduct("", search, classid, "", "").Rows.Count / peritems;
            int totalpages = (int)(Math.Ceiling(total));
            String allpage = "";
            for (int i = 1; i <= totalpages; i++)
            {
                String page = String.Format(" <a href=\"/Main.aspx?search={0}&classid={1}&pages={2}\">{3}</a> ", search, classid, i, i);
                allpage += page;
            }
            html += String.Format("<tr><td colspan=\"6\" align=\"right\"></td><td colspan=\"2\">{0} 共{1}页</td></tr>", allpage, totalpages);
            return html;
        }

        protected void searchB_Click(object sender, EventArgs e)
        {
            String search = HttpUtils.getElementsbyName("search");
            String classid = HttpUtils.getElementsbyName("classid");
            String url = String.Format("/Main.aspx?search={0}&classid={1}&pages=1", search, classid == "0" ? "" : classid);
            Response.Redirect(url);
        }

    }
}