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

public partial class huan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["wuid"] == null || Request.QueryString["wuid"].ToString() == "")
        {
            Response.Redirect("default.aspx");
        }
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            SqlDataReader Dr;
            basesql SqlObj = new basesql();
            String SqlStr;
            SqlStr = "select * from wu_info where wuid='" + Request.QueryString["wuid"].ToString() + "' and wu_user=" + int.Parse(HttpContext.Current.User.Identity.Name.ToString());
            Dr = SqlObj.ExecuteReader(SqlStr);
            if (Dr.Read())
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('对不起，您不可与自已的物品进行交换！');history.back();", true);
            }
            Dr.Close();
            SqlDataSource2.SelectParameters["userid"].DefaultValue = HttpContext.Current.User.Identity.Name.ToString();
            DataView Dv;
            Dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
            PlaceHolder PH;
            PH = (PlaceHolder)FormView3.FindControl("PlaceHolder1");
            if (Dv.Count > 0)
            {
                PH.Visible = false;
            }
            else
            {
                PH.Visible=true;                
            }
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('请先登录！');window.location='login.aspx';", true);
        }
    }

    protected void FormView3_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        SqlDataReader Dr;
        String SqlStr;
        basesql SqlObj = new basesql();
        RadioButtonList theRadio;
        theRadio = (RadioButtonList)FormView3.FindControl("RadioButtonList1");
        String SelWP = theRadio.SelectedValue.ToString();
        if (SelWP == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('请选择您要交换的物品！');", true);
            e.Cancel = true;
        }
        else
        {
            e.Values["wu_zid"] = SelWP;
        }
        String NewJHID = System.DateTime.Now.Year.ToString() + ((System.DateTime.Now.Month < 10) ? ("0" + System.DateTime.Now.Month.ToString()) : System.DateTime.Now.Month.ToString()) + ((System.DateTime.Now.Day<10) ? ("0" + System.DateTime.Now.Day.ToString()) : System.DateTime.Now.Day.ToString());
        SqlStr = "select * from huan_info where left(jhid,8)='" + NewJHID + "' order by jhid desc";
        Dr = SqlObj.ExecuteReader(SqlStr);
        if (Dr.Read())
        {
            String Str1;
            int i;
            int j;
            Str1 = (int.Parse(Dr["jhid"].ToString().Substring(8, 4)) + 1).ToString();
            j = Str1.Length;
            for (i = 0; i <4-j; i++)
            {
                Str1 = "0" + Str1;
            }
            NewJHID = NewJHID + Str1;
        }
        else
        {
            NewJHID = NewJHID + "0001";
        }
        e.Values["jhid"] = NewJHID;
        Dr.Close();
    }

    protected void FormView3_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "info", "alert('您的请求信息已送出，请等待回复！系统将返回主页！');window.location='default.aspx';", true);
    }
}
