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

public partial class item_Default : System.Web.UI.Page
{
    XabaraCom xc = new XabaraCom();
    dbDataFunction db = new dbDataFunction();
    xabaraAD ad = new xabaraAD();
    XabaraClass xClass = new XabaraClass();

    protected void Page_Load(object sender, EventArgs e)
    {        
        string classNav = "找店铺_ZDianPU.com";

        if (!IsPostBack)
        {
            ad2501.Text = ad.getAdCode(63, 0, 0, 1, 0, true, "");
            ad2502.Text = ad.getAdCode(64, 0, 0, 1, 0, true, "");

            DataTable dt;
            string strSql;
            string htmStr = string.Empty;
            string orderBy = " Order By isGood desc,volume DESC";

            string unionStr = string.Empty;
            string pageStr = string.Empty;
            string searchStr = xc.SafeSql(Server.UrlDecode(Request.QueryString["keyWord"]));

            long cidTemp = xc.SafeNum(Request.QueryString["id"]);    //分类           
            long ppID = 0;    //品牌
            long bqID = 0;   //标签
            long byID = 0;  //排序

            if (cidTemp < 1)
            {
                string urlID = Page.RouteData.Values["cID"].ToString();
                urlID = urlID.Replace(".htm", "");
                cidTemp = xc.SafeNum(urlID);

                string[] cid = urlID.Split(new char[] { '-' });
                if (cid.GetUpperBound(0) > 0)
                {
                    cidTemp = xc.SafeNum(cid[0].ToString());
                    ppID = xc.SafeNum(cid[1].ToString());
                    if (ppID > 0)
                    {
                        htmStr = " and taobaoke.ppID=" + ppID;
                    }
                    bqID = xc.SafeNum(cid[2].ToString());
                    /*if (bqID > 0)
                    {
                        htmStr += " and taobaoke.wordID=" + bqID;
                    }*/
                    byID = xc.SafeNum(cid[3].ToString());
                }
            }
            //Response.Write(cidTemp.ToString());
            string htmID = cidTemp.ToString() + "-" + ppID.ToString() + "-" + bqID.ToString();
            listTop.Text = "<a href=\"" + htmID + "-0.htm\" target=\"_top\">默认</a>";

            if (byID == 1)
            {
                listTop.Text += "&nbsp;&nbsp;<a href=\"" + htmID + "-1.htm\" style=\"color:#cb0201; font-weight:bold;\" target=\"_top\">销售</a>";
                orderBy = " Order By volume DESC";
            }
            else
            {
                listTop.Text += "&nbsp;&nbsp;<a href=\"" + htmID + "-1.htm\" target=\"_top\">销售</a>";
            }

            if (byID == 2)
            {
                listTop.Text += "&nbsp;&nbsp;<a href=\"" + htmID + "-2.htm\" style=\"color:#cb0201; font-weight:bold;\" target=\"_top\">价格</a>";
                orderBy = " Order By price ASC";
            }
            else
            {
                listTop.Text += "&nbsp;&nbsp;<a href=\"" + htmID + "-2.htm\" target=\"_top\">价格</a>";
            }
            htmID = htmID + "-" + byID.ToString();

            classNav = xClass.getClass(cidTemp, "", "");
            className.Text = classNav;

            //根分类读取
            dt = db.getDataTable("select Tid,TreeName from newsTree where TreeID=0 AND TreeType='taobaoke' order by ListID ASC, Tid ASC");
            for (int t = 0; t < dt.Rows.Count; t++)
            {
                string treeName = dt.Rows[t]["TreeName"].ToString().Trim();

                if (cidTemp == xc.SafeNum(dt.Rows[t]["Tid"].ToString().Trim()) || classNav.IndexOf(treeName) > -1)
                {
                    classTD.Text += "<td style=\"background-image:url('/images/classBg2.gif'); background-repeat:no-repeat; width:84px; height:44px; line-height:36px; text-align:center; color:white; font-size:16px;\">" + treeName + "</td><td style=\"width:10px; font-size:0px;\"></td>";                    
                }
                else
                {
                    classTD.Text += "<td style=\"background-image:url('/images/classBg.gif'); background-repeat:no-repeat; width:84px; height:44px; line-height:36px; text-align:center;\"><a href=\"" + dt.Rows[t]["Tid"].ToString().Trim() + "-0-0-0.htm\" target=\"_top\" class=\"classID\">" + treeName + "</a></td><td style=\"width:10px; font-size:0px;\"></td>";
                }
            }
            dt.Dispose();

            //二级分类读取
            dt = db.getDataTable("select Tid,TreeID,treeNameTxt,idLayerStr from newsTree where Tid=" + cidTemp.ToString());
            if (dt.Rows.Count > 0)
            {
                string tID = dt.Rows[0]["Tid"].ToString().Trim();
                string treeID = dt.Rows[0]["TreeID"].ToString().Trim();
                classNav = dt.Rows[0]["treeNameTxt"].ToString().Trim();

                string id = dt.Rows[0]["idLayerStr"].ToString().Trim();
                if (!string.IsNullOrEmpty(id as string))
                {
                    string[] bigID = id.Split(new char[] { '|' });
                    id = bigID[1];
                }
                else
                {
                    id = cidTemp.ToString();
                }
                
                dt = db.getDataTable("select Tid,TreeName from NewsTree where TreeID=" + id + " order by ListID ASC,Tid ASC");
                for (int l = 0; l < dt.Rows.Count; l++)
                {
                    string css = string.Empty;
                    string treeName = dt.Rows[l]["TreeName"].ToString().Trim();
                    long tidTree = xc.SafeNum(dt.Rows[l]["Tid"].ToString().Trim());
                    if (tidTree == cidTemp || classNav.IndexOf(treeName + "&nbsp;") > -1)
                    {
                        css = " style=\"color:#ff335c; font-weight:bold;\"";

                        searchStr += "%" + treeName.Substring(0, treeName.Length - 1);                       
                    }
                    navList.Text += "<a href=\"" + tidTree.ToString() + "-0-0-0.htm\" target=\"_top\" " + css + ">" + treeName + "</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                dt.Dispose();
            }
            dt.Dispose();
            ppClass.Text += "</table>";

            //读取品牌名称
            strSql = "select id,wordName from keywordid where isBad=0 and useType='pp' and treeID=" + cidTemp.ToString() + " order by ListID ASC,id ASC";
            //Response.Write(strSql);
            ppClass.Text = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
            dt = db.getDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                for (int p = 0; p < dt.Rows.Count; p++)
                {
                    if ((p + 1) % 8 == 1)
                    {
                        ppClass.Text += "<tr>";
                    }
                    string css = string.Empty;
                    long pidTree = xc.SafeNum(dt.Rows[p]["id"].ToString().Trim());
                    if (pidTree == ppID)
                    {
                        css = " style=\"color:#ff335c; font-weight:bold;\"";
                        classNav = dt.Rows[p]["wordName"].ToString().Trim() + "官方旗舰店 " + classNav;
                    }
                    ppClass.Text += "<td style=\"height:22px; line-height:22px; text-align:left; width:160px;\"><a href=\"" + cidTemp.ToString() + "-" + pidTree.ToString() + "-0-0.htm\" target=\"_top\" " + css + ">" + dt.Rows[p]["wordName"].ToString().Trim() + "</a></td>";
                    if ((p + 1) % 8 == 0)
                    {
                        ppClass.Text += "</tr>";
                    }
                }
            }
            else
            {
                classLable.Visible = false;
            }
            dt.Dispose();
            ppClass.Text += "</table>";

            string bqStr = string.Empty;
            if (cidTemp > 0)
            {
                string cidStr = xClass.getClassAllID("taobaoke", cidTemp); //得到所有ID

                if (cidStr.IndexOf(",") > 0)
                {
                    string[] ids = cidStr.Split(new char[] { ',' });
                    for (int i = 0; i <= ids.GetUpperBound(0); i++)
                    {
                        unionStr += " select taobaoke.num_iid,taobaoke.title,taobaoke.pic_url,taobaoke.price,taobaoke.click_url,taobaoke.commission,taobaoke.shop_click_url,taobaoke.seller_credit_score,taobaoke.volume,taobaoke.isGood from taobaoke where taobaoke.classID=" + ids[i].ToString().Trim() + htmStr + " union ";
                        bqStr += "select id,wordName,ListID from keywordid where isBad=0 and useType='bq' and treeID=" + ids[i].ToString() + " union ";
                    }

                    classLable.Visible = false;
                }
                else
                {
                    unionStr = " select taobaoke.num_iid,taobaoke.title,taobaoke.pic_url,taobaoke.price,taobaoke.click_url,taobaoke.commission,taobaoke.shop_click_url,taobaoke.seller_credit_score,taobaoke.volume,taobaoke.isGood from taobaoke where taobaoke.classID=" + cidTemp.ToString().Trim() + htmStr + " union ";

                    bqStr = "select id,wordName,ListID from keywordid where isBad=0 and useType='bq' and treeID=" + cidTemp.ToString() + " union ";
                }
            }
            else
            {
                unionStr = " (select taobaoke.num_iid,taobaoke.title,taobaoke.pic_url,taobaoke.price,taobaoke.click_url,taobaoke.commission,taobaoke.shop_click_url,taobaoke.seller_credit_score,taobaoke.volume,taobaoke.isGood from taobaoke where isBad=0 and commission>5 order by taobaoke.volume DESC Limit 0,3000) union ";

                classDiv.Visible = false;
                classLable.Visible = false;
            }

            int brNums = 0;//标签行数
            if (!string.IsNullOrEmpty(bqStr as string))
            {
                bqStr = "(" + bqStr.Substring(0, bqStr.Length - 6) + ") as news";
                bqStr = "select * from " + bqStr + " order by ListID ASC,ID ASC";
                
                DataTable dtLab = db.getDataTable(bqStr);
                if (dtLab.Rows.Count > 0)
                {
                    long wordNums = 0;
                    for (int ll = 0; ll < dtLab.Rows.Count; ll++)
                    {
                        string css = string.Empty;
                        string wordName = dtLab.Rows[ll]["wordName"].ToString().Trim();
                        int nameLen = new webPublic().wordLength(wordName);
                        
                        long bidTree = xc.SafeNum(dtLab.Rows[ll]["id"].ToString().Trim());
                        if (bidTree == bqID)
                        {
                            css = " style=\"color:#ff335c; font-weight:bold;\"";
                            searchStr += wordName;

                            classNav = wordName + "官方旗舰店 " + classNav;
                        }

                        if (lableHtm.Text.IndexOf(wordName) < 0)
                        {
                            wordNums += nameLen;

                            lableHtm.Text += "<a href=\"" + cidTemp.ToString() + "-0-" + bidTree.ToString() + "-" + byID.ToString() + ".htm\" target=\"_top\" " + css + ">" + wordName + "</a>";

                            if (wordNums > 26)
                            {
                                lableHtm.Text += "<br />";
                                wordNums = 0;
                                brNums += 1;
                            }
                            else
                            {
                                wordNums += 2;
                                lableHtm.Text += "&nbsp;&nbsp;";
                            }
                        }
                    }
                }
                else
                {
                    lableDiv.Visible = false;
                }
                dtLab.Dispose();
            }
            else
            {
                lableDiv.Visible = false;
            }

            //直接搜索标签关键词
            if (bqID > 0)
            {
                unionStr = " (select taobaoke.num_iid,taobaoke.title,taobaoke.pic_url,taobaoke.price,taobaoke.click_url,taobaoke.commission,taobaoke.shop_click_url,taobaoke.seller_credit_score,taobaoke.volume,taobaoke.isGood from taobaoke where isBad=0 and wordID=" + bqID.ToString() + " order by taobaoke.volume DESC Limit 0,3000) union ";

                unionStr += " (select taobaoke.num_iid,taobaoke.title,taobaoke.pic_url,taobaoke.price,taobaoke.click_url,taobaoke.commission,taobaoke.shop_click_url,taobaoke.seller_credit_score,taobaoke.volume,taobaoke.isGood from taobaoke where isBad=0 and title like '%" + searchStr + "%' order by taobaoke.volume DESC Limit 0,3000) union ";
            }

            unionStr = "(" + unionStr.Substring(0, unionStr.Length - 6) + ") as news";
            strSql = "select * from " + unionStr + orderBy + " Limit 0,3000";
            //Response.Write(strSql);
            //Response.End();
            lblCurrentPage.Text = db.RepeaterDB(RepeaterList, strSql, pageStr, 30, "page", "center", true);
            int count = this.RepeaterList.Items.Count;
            if (count < 1)
            {
                errInfo.Text = "目前暂没有任何信息！";
                errDiv.Visible = true;
            }
            else
            {
                errDiv.Visible = false;
            }

            int randNums = count / 6;
            if (count > 26)
            {
                randNums += 1;                
            }
            if (brNums < 6)
            {
                randNums += 1;
            }

            if (count > 9)
            {
                hotRepeater.DataSource = db.getDataTable("select * from " + unionStr + " Order By RAND() Limit 0," + randNums.ToString());
                hotRepeater.DataBind();
                hotRepeater.Dispose();
            }

            xc.webMeta(this.Page, classNav.Replace("&nbsp;", "").Replace("-", ",") + xc.readXmlData("xabara.config", "KeyWord"), xc.readXmlData("xabara.config", "metaStr"));
        }

        this.Page.Title = classNav + "频道";
    }

    public string money(string str)
    {
        return xc.getMoney(str);
    }
}