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 Admin : System.Web.UI.Page
    {
        int pages = HttpUtils.getQueryString("pages").ToInt();
        public String user;
        const int peritems = 5;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("/Account/Login.aspx");
            }
            user = Session["username"].ToString();
        }

        public String GetProductInfo()
        {
            String html = "";
            DataTable items = ProductManager.SearchProduct(user, "", "", "", "", pages, peritems);
            if (items.Rows.Count == 0)
                return "<tr><td>数据库中未找到产品</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 edit = "<a href=\"/Product/ProductEdit.aspx?id=" + id + "&type=edit\" >编辑</a>";
                String classname = ProductClassManager.getClassName(int.Parse(ProductManager.GetProductInfo(item, ProductManager.Info.Class)));
                String delete = "<a href=\"/Product/ProductEdit.aspx?id=" + id + "&type=delete\" >删除</a>";
                html += "<tr>";
                html += HttpUtils.addTd();
                html += HttpUtils.addTd(ProductManager.NameToUrl(id, name));
                html += HttpUtils.addTd(picurl);
                html += HttpUtils.addTd(price);
                html += HttpUtils.addTd(amount);
                html += HttpUtils.addTd(classname);
                html += HttpUtils.addTd(edit);
                html += HttpUtils.addTd(delete);
                html += "</tr>";
            }
            decimal total = ProductManager.SearchProduct(user, "", "", "", "").Rows.Count / peritems;
            int totalpages = (int)(Math.Ceiling(total));
            String allpage = "";
            for (int i = 1; i <= totalpages; i++)
            {
                String page = String.Format(" <a href=\"/Admin.aspx?pages={0}\">{1}</a> ", 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;
        }
    }
}