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 System.Data.SqlClient;
using nspProduct;
using SqlOperate;
using Base;

public partial class Member_Trade_MemberProductAdd : nspBase.MemberBasePge
{


    private int pageSize = 10;//这里设置分页尺寸
    #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



    #region 分页事件
    protected void btn_pageFirst_Click(object sender, EventArgs e)
    {
        //首页事件
        this.Pageindex = 1;
        Bind();
    }
    protected void btn_pageLast_Click(object sender, EventArgs e)
    {
        //上一页
        this.Pageindex--;
        Bind();
    }
    protected void btn_pageNext_Click(object sender, EventArgs e)
    {
        //下一页
        this.Pageindex++;
        Bind();
    }
    protected void btn_pageFoot_Click(object sender, EventArgs e)
    {
        //末页
        this.Pageindex = PageCount % pageSize == 0 ? PageCount / pageSize : PageCount / pageSize + 1;
        Bind();
    }
    protected void btn_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 Page_Load(object sender, EventArgs e)
    {
        if (!CheckCookies())
        {
            Response.Redirect("../../login/login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            Bind();
            for (int i = 0; i < dlProducts.Items.Count; i++)
            {
                SqlDataReader sr = SqlHelper.ExecuteReader("select ProductId,Verify from W_UsersProducts where UserID=" + Convert.ToInt32(Base.GetRequest.GetCookie("UserID")));
                while (sr.Read())
                {
                    if (sr["ProductId"].ToString().Equals(dlProducts.DataKeys[i].ToString()))
                    {
                        dlProducts.Items[i].FindControl("btnAdd").Visible = false;
                        if (sr["Verify"].ToString().Equals("False"))
                        {
                            dlProducts.Items[i].FindControl("label1").Visible = true;
                            dlProducts.Items[i].FindControl("label2").Visible = false;
                            dlProducts.Items[i].FindControl("LinkButton1").Visible = false;
                        }
                        if(sr["Verify"].ToString().Equals("True"))
                        {
                            dlProducts.Items[i].FindControl("label1").Visible = false;
                            dlProducts.Items[i].FindControl("label2").Visible = true;
                            dlProducts.Items[i].FindControl("LinkButton1").Visible = true;
                        }
                    }
                  
                }

            }

        }

    }

    private void Bind()
    {
        string strWhere = "1=1";
        DataTable dt = Base.PageList.GetDataTable(strWhere, "W_Products as a", 0, "a.AddDate ", "a.ID,a.Name,a.TypeID,a.Picture,a.KeyWord,a.AddDate,a.Detail,a.Price", pageSize, Pageindex, 0);
        DataTable table = Base.PageList.GetDataTable(strWhere, "W_Products");
        PageCount = Convert.ToInt32(table.Rows[0]["Total"]);
        this.txt_pageCount.Text = PageCount.ToString();
        if (Pageindex == 1)
        {
            this.btn_pageFirst.Enabled = false;
            this.btn_pageLast.Enabled = false;
        }
        else
        {
            this.btn_pageFirst.Enabled = true;
            this.btn_pageLast.Enabled = true;
        }
        if (Pageindex == (PageCount % pageSize == 0 ? PageCount / pageSize : (PageCount / pageSize) + 1))
        {
            this.btn_pageNext.Enabled = false;
            this.btn_pageFoot.Enabled = false;
        }
        else
        {
            this.btn_pageNext.Enabled = true;
            this.btn_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();

        this.dlProducts.DataSource = dt.DefaultView;
        dlProducts.DataBind();
    }

    protected void dlProducts_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        int index = 0;
        if (e.Item.ItemIndex > -1)
        {
            index = e.Item.ItemIndex;
            int id = int.Parse(dlProducts.DataKeys[index].ToString());
            SqlDataReader sdr = SqlHelper.ExecuteReader("select U_UserInfo.UserID,LinkName,MobilePhone,CompanyName from U_UserInfo left join W_UsersProducts on U_UserInfo.UserID = W_UsersProducts.UserID  where ProductId=" + id);
            while (sdr.Read())
            {
                Table table = (Table)e.Item.FindControl("supply");
                TableRow tr = new TableRow();
                table.Rows.Add(tr);
                TableCell td1 = new TableCell();
                tr.Cells.Add(td1);
                LinkButton button1 = new LinkButton();
                button1.Text = sdr["CompanyName"].ToString();
                td1.Controls.Add(button1);
            }
        }
    }

    protected void btnAdd_Command(object sender, CommandEventArgs e)
    {
        int ProductId = int.Parse(e.CommandArgument.ToString());
        try
        {
        string insertStr = "insert into [W_UsersProducts] ([UserId],[ProductId]) values (@UserId,@ProductId)";
        SqlParameter[] parms ={ 
                new SqlParameter("@UserId", Convert.ToInt32( Base.GetRequest.GetCookie("UserID"))),
                new SqlParameter("@ProductId",ProductId)
            };
        SqlHelper.ExecuteNonQuery(CommandType.Text, insertStr, parms);
        Response.Redirect("MemberProductAdd.aspx");
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }
    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        int ProductId = int.Parse(e.CommandArgument.ToString());
        int userid = int.Parse(Base.GetRequest.GetCookie("UserID"));
        try
        {
            string updateStr = "delete from [W_UsersProducts]  where [UserID]=@UserId and [ProductID] = @ProductId";
            SqlParameter[] parms ={ 
                new SqlParameter("@UserId", Convert.ToInt32( Base.GetRequest.GetCookie("UserID"))),
                new SqlParameter("@ProductId",ProductId)
            };
            SqlHelper.ExecuteNonQuery(CommandType.Text, updateStr, parms);
            for (int i = 0; i < dlProducts.Items.Count; i++)
            {
                if (ProductId==int.Parse(dlProducts.DataKeys[i].ToString()))
                {
                    dlProducts.Items[i].FindControl("btnAdd").Visible = true;
                    dlProducts.Items[i].FindControl("label1").Visible = false;
                    dlProducts.Items[i].FindControl("label2").Visible = false;
                    dlProducts.Items[i].FindControl("LinkButton1").Visible = false;
                }
            }
            Response.Redirect("MemberProductAdd.aspx");
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }
}
