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 += html += HttpUtils.addThs("商品名称", "商品图片", "商品价格", "商品数量", "商品分类", "编辑", "删除");
            html += "<tbody class=\"text-center\">";
            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 += HttpUtils.addTds(ProductManager.NameToUrl(id, name), picurl, price, amount, classname, edit, delete);
            }
            decimal total = ProductManager.SearchProduct(user, "", "", "", "").Rows.Count / peritems;
            int totalpages = (int)(Math.Ceiling(total));
            String allpage = "<ul class=\"pagination\">";
            for (int i = 1; i <= totalpages; i++)
            {
                String page = String.Format("<li><a href=\"/Admin.aspx?pages={0}\">{1}</a><li>", i, i);
                allpage += page;
            }
            allpage += String.Format("<li><a href=\"#\">共{0}页</a><li>", totalpages);
            allpage += "</ul>";
            html += String.Format("<tr><td colspan=\"6\" align=\"right\"></td><td colspan=\"2\">{0}</td></tr>", allpage);
            html += "</tbody>";
            return html;
        }
    }
}