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 webXabara_shop_pop : System.Web.UI.Page
{
    XabaraCom xc = new XabaraCom();
    XabaraClass xClass = new XabaraClass();
    dbDataFunction db = new dbDataFunction();

    protected void Page_Load(object sender, EventArgs e)
    {
        xc.CheckAdminLogin(string.Empty);

        if (!IsPostBack)
        {
            string pageStr = string.Empty;
            string searchStr = Server.UrlDecode(Request.QueryString["keyWord"]);
            string searchIf = string.Empty;
            string orderBy = " Order By taobaopop.id desc";            

            if (!string.IsNullOrEmpty(searchStr as string))
            {
                if (xc.SafeNum(searchStr) > 0)
                {
                    searchIf = " and taobaoke.num_iid=" + searchStr;
                }
                else
                {
                    searchIf = " and taobaoke.title like '%" + searchStr + "%'";
                }

                searchWord.Text = searchStr;
                pageStr += "&keyWord=" + searchStr;
            }

            string strSql = "select taobaopop.id,taobaopop.num_iid,taobaopop.keyWord,taobaopop.buyDays,taobaopop.adFrom,taobaopop.adTo,taobaoke.title,taobaoke.pic_url,taobaoke.price,taobaoke.volume,taobaoke.updateDate from taobaopop left join taobaoke on taobaoke.num_iid=taobaopop.num_iid where taobaopop.buyDays>0 " + searchIf + orderBy;
            //Response.Write(strSql);
            //Response.End();
            lblCurrentPage.Text = db.RepeaterDB(RepeaterList, strSql, pageStr, 30, "page", "center", true);
            if (this.RepeaterList.Items.Count < 1)
            {
                errInfo.Text = "目前暂没有任何信息！";
                errDiv.Visible = true;
            }
            else
            {
                errDiv.Visible = false;
            }
        }
    }

    public string cutWords(string str, int len)
    {
        return xc.CutWord(xc.SafeHtml(str), len);
    }

    protected void search_Click(object sender, EventArgs e)
    {
        Response.Redirect("keyWord=" + Server.UrlEncode(searchWord.Text), true);
    }    

    protected void del_Click(object sender, EventArgs e)
    {
        DateTime now = DateTime.Now;
        string ids = Request.Form["batDel"];
        if (!string.IsNullOrEmpty(ids as string))
        {
            db.DelDB("taobaopop", "num_iid", ids, "", false, string.Empty);
            db.DelDB("taobaoke", "num_iid", ids, "", false, string.Empty);
            Response.Redirect(Server.UrlDecode(xc.errorUrlTwo), true);
        }
        else
        {
            xc.divError("", "请先选择后再操作！", 350, 150, Server.UrlDecode(xc.errorUrlTwo) + "#foot", "goto");
        }
    }

    protected void checkPOP_Click(object sender, EventArgs e)
    {
        this.checkFlag();

        Response.Redirect(Server.UrlDecode(xc.errorUrlTwo), true);
    }

    private void checkFlag()
    {
        DateTime now = DateTime.Now;
        string[] strValue = new string[5];
        strValue[0] = "99999999999999999";

        DataTable dt = db.getDataTable("SELECT num_iid FROM taobaopop WHERE adTo<'" + now + "' UNION SELECT num_iid FROM taobaopop WHERE flagID=0");
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                db.InsertUpdateDB("taobaoKe", "@isPOP", strValue, " num_iid=" + dt.Rows[i]["num_iid"].ToString().Trim());
                strValue[0] = "0";
                strValue[1] = now.ToString("yyyy-MM-dd HH:mm:ss");
                db.InsertUpdateDB("taobaopop", "@flagID,@adTo", strValue, " num_iid=" + dt.Rows[i]["num_iid"].ToString().Trim());
            }
        }
        dt.Dispose();
    }
}