using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Product;
using Base;

public partial class admin_Trade_PurchaseManage : BasePage
{
    private int pageSize = 12;//这里设置分页条数
    #region
    public int Pageindex
    {
        //页下标属性
        set
        {
            this.ViewState["pgindex"] = value;
        }
        get
        {
            int index = 1;
            object o = this.ViewState["pgindex"];
            if (o != null)
            {
                index = (int)o;
            }
            return index;
        }
    }
    public int PageCount
    { 
        //总页数
        set { this.ViewState["pagecount"] = value; }
        get
        {
            int pagecot = 1;
            object od = this.ViewState["pagecount"];
            if (od != null)
            {
                pagecot = (int)od;
            }
            return pagecot;
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("column", "A1");
        if (!IsPostBack)
        {
            Bind();
        }
    }
    #region 绑定数据方法
    private void Bind()
    {
        btnDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
        string strWhere = "1=1";// "C_Purchase.UserID=U_UserInfo.UserID";
        if (GetRequest.GetQueryStringValue("V") != "")
        {
            switch (GetRequest.GetQueryStringValue("v"))
            {
                case "1":
                    this.Label2.CssClass = "pl";
                    strWhere += " and C_Purchase.Verify<3";
                    break;
                case "2":
                    strWhere += " and C_Purchase.Verify=3";
                    this.Label3.CssClass = "pl";
                    break;
                case "3":
                    strWhere += " and C_Purchase.Recommend=1";
                    this.Label4.CssClass = "pl";
                    break;
                default:
                    this.Label1.CssClass = "pl";
                    break;
            }
        }
        else
        {
            this.Label1.CssClass = "pl";
        }

        string TitleName = txtTitleName.Text.Trim();
        // string LoginName = txtLoginName.Text.Trim();
        if (TitleName != "")
            strWhere += " and Name like '%" + TitleName + "%'";
        //if (LoginName != "")
        //    strWhere += " and LoginName like '%" + LoginName + "%'";


        pbind(strWhere, Pageindex, "");

    }
    #endregion
    #region 获取数据并分页
    private void pbind(string strWhere, int intPageIndex, string strUrl)
    {
        //取得数据
        //第一次
        DataTable dtAll = PageList.GetDataTable(strWhere, "C_Purchase", 1, "AddDate", "C_Purchase.UserID,C_Purchase.IsUser", pageSize, intPageIndex, 0);

        ////第二次
        string str1 = strWhere + " AND C_Purchase.UserID=U_UserInfo.UserID AND C_Purchase.IsUser=1";
        DataTable dt = PageList.GetDataTable(str1, "C_Purchase,U_UserInfo", 1, "AddDate", "C_Purchase.*,U_UserInfo.UserID,U_UserInfo.LoginName", pageSize, intPageIndex, 0);
        ////第三次
        str1 = strWhere + " AND C_Purchase.UserID=U_Visitor.ID AND C_Purchase.IsUser=0";
        DataTable dtVisitor = PageList.GetDataTable(str1, "C_Purchase,U_Visitor", 1, "C_Purchase.AddDate", "C_Purchase.*,U_Visitor.LinkMan as LoginName,U_Visitor.ID", pageSize, intPageIndex, 0);

        dt.Merge(dtVisitor);
        //取得总的记录
        DataTable table = PageList.GetDataTable(strWhere, "C_Purchase");
        PageCount = Convert.ToInt32(table.Rows[0]["Total"]);
        this.txt_pageCount.Text = PageCount.ToString();
        if (Pageindex == 1)
        {
            this.txt_pageFirst.Enabled = false;
            this.txt_pageLast.Enabled = false;
        }
        else
        {
            this.txt_pageFirst.Enabled = true;
            this.txt_pageLast.Enabled = true;
        }
        if (Pageindex == (PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1))
        {
            this.txt_pageNext.Enabled = false;
            this.txt_pageFoot.Enabled = false;
        }
        else
        {
            this.txt_pageNext.Enabled = true;
            this.txt_pageFoot.Enabled = true;
        }
        this.txt_begin.Text = Pageindex.ToString();
        this.txt_pageAll.Text = Convert.ToString((PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1));
        this.txt_PageNum.Text = Pageindex.ToString();
        grdPurchase.DataSource = dt.DefaultView;
        grdPurchase.DataBind();
    }

    private void pbindd(string strWhere, int intPageIndex, string strUrl, string IsUser)
    {
        //取得数据
        DataTable dt = new DataTable();
        if (IsUser.Equals("True"))
        {
            string str = strWhere + " AND C_Purchase.UserID=U_UserInfo.UserID";
            dt = PageList.GetDataTable(str, "C_Purchase,U_UserInfo", 1, "AddDate", "C_Purchase.*,U_UserInfo.UserID,U_UserInfo.LoginName", pageSize, intPageIndex, 0);

        }
        else if (IsUser.Equals("False"))
        {
            string str = strWhere + " AND C_Purchase.UserID=U_Visitor.ID";
            dt = PageList.GetDataTable(str, "C_Purchase,U_Visitor", 1, "C_Purchase.AddDate", "C_Purchase.*,U_Visitor.LinkMan as LoginName,U_Visitor.ID", pageSize, intPageIndex, 0);
        }
        //取得总的记录
        DataTable table = PageList.GetDataTable(strWhere, "C_Purchase");
        PageCount = Convert.ToInt32(table.Rows[0]["Total"]);
        this.txt_pageCount.Text = PageCount.ToString();
        if (Pageindex == 1)
        {
            this.txt_pageFirst.Enabled = false;
            this.txt_pageLast.Enabled = false;
        }
        else
        {
            this.txt_pageFirst.Enabled = true;
            this.txt_pageLast.Enabled = true;
        }
        if (Pageindex == (PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1))
        {
            this.txt_pageNext.Enabled = false;
            this.txt_pageFoot.Enabled = false;
        }
        else
        {
            this.txt_pageNext.Enabled = true;
            this.txt_pageFoot.Enabled = true;
        }
        this.txt_begin.Text = Pageindex.ToString();
        this.txt_pageAll.Text = Convert.ToString((PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1));
        this.txt_PageNum.Text = Pageindex.ToString();
        grdPurchase.DataSource = dt.DefaultView;
        grdPurchase.DataBind();
    }

    #endregion




    /// <summary>
    /// 搜索该会员发布的信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lnbCheckUser_Command(object sender, CommandEventArgs e)
    {
        string IsUser = e.CommandArgument.ToString();
        if (IsUser.Equals("True"))
        {
            string Content = e.CommandName.ToString();
            string strWhere = " C_Purchase.UserID=" + Content;
            pbindd(strWhere, 1, "&uid=" + Content, "True");
        }
        else if (IsUser.Equals("False"))
        {
            string Content = e.CommandName.ToString();
            string strWhere = " C_Purchase.UserID=" + Content;
            pbindd(strWhere, 1, "&uid=" + Content, "False");
        }
    }



    protected void lbtnUpdate_Command(object sender, CommandEventArgs e)
    {
        TPurchase TPur = new TPurchase();
        string Content = e.CommandArgument.ToString();
        int index = Content.IndexOf("|");
        if (index < -1 || index + 1 > Content.Length)
            return;

        TPur.ID = int.Parse(Content.Substring(0, index));
        TPur.Verify = int.Parse(Content.Substring(index + 1));
        IPurchase RPur = new RPurchase();
        RPur.UpdateVerify(TPur);
        Bind();
    }


    public string GetNewName(string Name, string IsUser)
    {
        if (IsUser.Equals("True"))
        {
            Name += "(会员)";
        }
        else if (IsUser.Equals("False"))
        {
            Name += "(游客)";
        }
        return Name;
    }


    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= grdPurchase.Rows.Count - 1; i++)
        {

            CheckBox cbox = (CheckBox)grdPurchase.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {

                int ID = Convert.ToInt32(grdPurchase.DataKeys[i].Value);
                IPurchase RUser = new RPurchase();
                int m = RUser.Delete(ID);
            }
        }
        Bind();
    }
    protected void lbtnUpdateRecommend_Command(object sender, CommandEventArgs e)
    {
        TPurchase TPur = new TPurchase();
        string Content = e.CommandArgument.ToString();
        int index = Content.IndexOf("|");
        if (index < -1 || index + 1 > Content.Length)
            return;

        TPur.ID = int.Parse(Content.Substring(0, index));
        TPur.Recommend = Convert.ToBoolean(Content.Substring(index + 1));
        IPurchase RPur = new RPurchase();
        RPur.UpdateRecommend(TPur);
        Bind();
    }

    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strWhere = "1=1";
        string TitleName = txtTitleName.Text.Trim();
        if (TitleName != "")
            strWhere += " and Name like '%" + TitleName + "%'";

        pbind(strWhere, Pageindex, "t=" + TitleName + "&");
    }
    protected void lnkdelete_Command(object sender, CommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        IPurchase rs = new RPurchase();
        int m = rs.Delete(id);
        Bind();
    }
    #region 分页事件方法
    protected void txt_pageFirst_Click(object sender, EventArgs e)
    {
        //首页
        this.Pageindex = 1;
        Bind();
    }
    protected void txt_pageLast_Click(object sender, EventArgs e)
    {
        //上一页
        this.Pageindex--;
        Bind();
    }
    protected void txt_pageNext_Click(object sender, EventArgs e)
    {
        //下一页
        this.Pageindex++;
        Bind();
    }
    protected void txt_pageFoot_Click(object sender, EventArgs e)
    {
        //末页
        this.Pageindex = PageCount % pageSize == 0 ? PageCount / pageSize : PageCount / pageSize + 1;
        Bind();
    }
    protected void Lk_go_Click(object sender, EventArgs e)
    {
        //跳到指定的页
        string txtNumber = this.txt_PageNum.Text.ToString().Trim();
        int index = 1;
        if (Base.Common.CheckNum(txtNumber))
        {
            index = Convert.ToInt32(txtNumber);
            if (index >= 1 && index <= (PageCount % pageSize == 0 ? PageCount / pageSize : PageCount / pageSize + 1))
            {
                Pageindex = index;
                Bind();
            }
            else
            {
                Pageindex = 1;
                this.txt_PageNum.Text = "输入错误";
            }
        }
        else
        {
            Pageindex = 1;
            this.txt_PageNum.Text = "输入错误";
        }
    }
    #endregion


    protected void grdPurchase_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");
            e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);");
        }
    }




    /// <summary>
    /// 审核通过
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButEigible_Click(object sender, EventArgs e)
    {
        string strID = "";
        for (int i = 0; i <= grdPurchase.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)grdPurchase.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                strID += "," + Convert.ToInt32(grdPurchase.DataKeys[i].Value);
            }
        }
        if (strID == "")
        {
            Base.BasePage.ShowMessage("您还没有选择信息！", "Trade/PurchaseManage.aspx");
            return;
        }

        if (Solely.Other.UpOnlyDate("C_Purchase", "Verify=1", "id in(" + strID.Substring(1) + ")"))
        {
            Bind();
        }
        else
        {
            Base.BasePage.ShowMessage("审核出现错误！", "Trade/PurchaseManage.aspx");
        }

    }


    /// <summary>
    /// 审核未通过
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButDisqualification_Click(object sender, EventArgs e)
    {
        string strID = "";
        for (int i = 0; i <= grdPurchase.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)grdPurchase.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                strID += "," + Convert.ToInt32(grdPurchase.DataKeys[i].Value);
            }
        }

        if (strID == "")
        {
            Base.BasePage.ShowMessage("您还没有选择信息！", "Trade/PurchaseManage.aspx");
            return;
        }

        if (Solely.Other.UpOnlyDate("C_Purchase", "Verify=2", "id in(" + strID.Substring(1) + ")"))
        {
            Bind();
        }
        else
        {
            Base.BasePage.ShowMessage("审核出现错误！", "Trade/PurchaseManage.aspx");
        }
    }


    /// <summary>
    /// 绑定审核状态
    /// </summary>
    /// <param name="strVerify"></param>
    /// <returns></returns>
    public static string ShowVerify(string strVerify)
    {
        if (strVerify == "1")
        {
            return "通过";
        }
        else if (strVerify == "2")
        {
            return "未通过";
        }
        else
        {
            return "未审";
        }
    }
    protected void grdPurchase_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    public string GetNewTitle(string Title, string strID)
    {
        string Restr = "<a href=\"" + Base.BasePage.Config.WebURL + "Purchase/Detail.aspx?id=" + strID + "\" target=\"blank\">" + Title + "</a>";
        return Restr;
    }

}
