<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<script language="c#" runat="server">
	DataRow[] NRows;
	DataRow[] NRows1;
	DataRow[] NRows2;
	SqlConnection Source;
	DataSet userData;
	void Page_Load(object sender,EventArgs e){
		Join.Visible=false;
		Source = (SqlConnection)Session["NowWuz"];
		if(IsPostBack==false){
			Session["NowID"]=Request.QueryString["NowID"];
			SqlDataAdapter NComm1 = new SqlDataAdapter("select * from BuiltinCat", Source);
			SqlDataAdapter NComm2 = new SqlDataAdapter("select * from BuiltinParts where CombSubID like '" + Request.QueryString["NowID"] + "%'", Source);
			DataSet DS = new DataSet();
			NComm1.Fill(DS, "BuiltinCat");
			NComm2.Fill(DS, "BuiltinParts");
			Level1.DataSource=DS.Tables["BuiltinCat"].DefaultView;
			Level1.DataBind();
		 	DataColumn newCol;
			DataRow DRow;
			DataTable UserDataTable = new DataTable("UserTableS");
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "UserPartid";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "PartID";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "Photo";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "LocLeft";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "Color";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "Model";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.Double");
			newCol.ColumnName = "UpricePart";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "LocTop";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "LocZIndex";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "CatSubPartID";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "UserPartName";
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "CombSubID";
			newCol.Unique = true;
			UserDataTable.Columns.Add(newCol);
			newCol = new DataColumn();
			newCol.DataType = System.Type.GetType("System.String");
			newCol.ColumnName = "PartName";
			UserDataTable.Columns.Add(newCol);
			userData = new DataSet();
			userData.Tables.Add(UserDataTable);
			DataColumn[] PrimaryKeyColumns=new DataColumn[1];
			PrimaryKeyColumns[0] = userData.Tables[UserDataTable.TableName].Columns["CombSubID"];
			userData.Tables[UserDataTable.TableName].PrimaryKey = PrimaryKeyColumns;
			string n="";
			n="<div id=\"plan\" style=\"position: relative ; z-index:1; display:\"><img border=0 src=\"images\\CB\\Lpic\\Type\\UB-TYPE01.jpg\" alt=底图>";
			NRows = DS.Tables["BuiltinParts"].Select();
			for(int i=0 ; i <=NRows.Length-1;i++){
				SqlDataAdapter NComm3 = new SqlDataAdapter("select * from Parts where PartID like '" + NRows[i]["PartID"].ToString() + "'",Source);
				string k="";
				k+= NRows[i]["Partid"];
				NComm3.Fill(DS, "Parts");
				NRows1 = DS.Tables["Parts"].Select();
				n+= "<div id=\"" + NRows[i]["PartID"].ToString() + "\" style=\"position: absolute; left: " + NRows[i]["LocLeft"].ToString() + "; top:" + NRows[i]["LocTop"].ToString() + "; z-index:" + NRows[i]["LocZindex"].ToString() + ";display:\"><img border=0 src=\"" + NRows1[i]["CpathName"].ToString() + "\\" + NRows1[i]["CfileName"].ToString() + "\"></div>";
				if(IsPostBack==false){
					DRow = UserDataTable.NewRow();
					DRow["CombSubID"]=NRows[i]["CombSubID"].ToString();
					DRow["PartName"]=NRows[i]["PartName"].ToString();
					DRow["PartID"]=NRows[i]["PartID"].ToString();
					DRow["LocLeft"]=NRows[i]["LocLeft"].ToString();
					DRow["LocTop"]=NRows[i]["LocTop"].ToString();
					DRow["Color"]=NRows[i]["Color"].ToString();
					DRow["CatSubPartID"]=NRows[i]["CatSubPartID"].ToString();
					DRow["Model"]=NRows[i]["Model"].ToString();
					DRow["UpricePart"]=Convert.ToDouble(NRows1[i]["UpricePart"].ToString());
					DRow["LocZindex"]=NRows[i]["LocZindex"];
					DRow["Photo"]=NRows1[i]["CpathName"] + "\\" + NRows1[i]["CfileName"];
					UserDataTable.Rows.Add(DRow);
				}
			}
			Session["NowUserData"]=userData.Tables["UserTableS"];
			Level5.DataSource=userData.Tables["UserTableS"].DefaultView;
			n+= "</div>";
			s1.InnerHtml= n;
			Session["NowDSA"]=DS;
		}
		DataTable t=(DataTable)Session["NowUserData"];
		Level5.DataSource=t.DefaultView;
		Level5.DataBind();
		UserLevel5.DataSource=t.DefaultView;
		UserLevel5.DataBind();
	}
	void OKClick(object sender,EventArgs e){
		Response.Redirect("Result.aspx");
	}
	void Level1_Click(object sender,CommandEventArgs e){
		DataSet DS = (DataSet)Session["NowDSA"];
		for(int i = 0 ; i <= DS.Tables.Count-1;i++){
			if(DS.Tables[i].TableName=="BuiltinCatSub"){
				DS.Tables["BuiltinCatSub"].Rows.Clear();
				break;
			}
		}
		SqlDataAdapter NComm1 = new SqlDataAdapter("select * from BuiltinCatSub where CatPartID like '" + e.CommandArgument.ToString() + "'", Source);
		NComm1.Fill(DS, "BuiltinCatSub");
		Level2.DataSource=DS.Tables["BuiltinCatSub"].DefaultView;
		Level2.DataBind();
	}
	void Level2_Click(object sender ,CommandEventArgs e){
		DataSet DS = (DataSet)Session["NowDSA"];
		for(int i = 0 ; i <=DS.Tables.Count-1;i++){
			if(DS.Tables[i].TableName=="AvailableParts"){
				DS.Tables["AvailableParts"].Rows.Clear();
				break;
			}
		}
		DataRow[] j;
		DataTable p = (DataTable)Session["NowUserData"];
		j=p.Select("CatSubPartID like '" + e.CommandArgument.ToString() + "'");
		string x =j[0]["CombSubID"].ToString();
		DataColumn[] k=new DataColumn[1];
		DataColumn Col;
		string d="";
		d="SELECT AvailableParts.PartID, Parts.lpathname, Parts.lFilename,Parts.Cpathname + '/' + Parts.CFilename as Combine,Parts.lFilename,Parts.lpathname + '/' + Parts.lFilename as Large,Parts.spathname, Parts.sFilename,Parts.spathname + '/' + Parts.sFilename as small,";
		d+= "AvailableParts.AvailablePartID,AvailableParts.Material,AvailableParts.Specification, AvailableParts.PartName, ";
		d+= "AvailableParts.CombSubID, AvailableParts.CatSubPartID, AvailableParts.LocLeft, AvailableParts.LocTop, Parts.Color,Parts.UpricePart,Parts.Model,";
		d+= "AvailableParts.LocZindex FROM Parts INNER JOIN AvailableParts ON Parts.PartID = AvailableParts.PartID";
		d+= " where AvailableParts.CombSubID like '" + x + "'";
		SqlDataAdapter NComm2 = new SqlDataAdapter(d, Source);
		NComm2.Fill(DS, "AvailableParts");
		Col = DS.Tables["AvailableParts"].Columns["PartID"];
		k[0] = Col;
		DS.Tables["AvailableParts"].PrimaryKey = k;
		Session["NowBuiltSub"]=e.CommandArgument.ToString();
		detail.Visible=false;
		NRows2 = DS.Tables["AvailableParts"].Select();
		Level3.DataSource=DS.Tables["AvailableParts"].DefaultView;
		Level3.DataBind();
		Session["AvailableParts"]=DS.Tables["AvailableParts"];
	}
	void ShowDetail(object sender,EventArgs e){
		Join.Visible=true;
		detail.Visible=true;
		DataRow foundRow;
		DataTable t = (DataTable)Session["AvailableParts"];
		foundRow = t.Rows.Find(Level3.DataKeys[Level3.SelectedItem.ItemIndex]);
		string k;
		k="材质：" + foundRow["Material"].ToString() + "<br>" + "颜色：" + foundRow["color"].ToString() + "<br>" + "型号：" + foundRow["model"].ToString();
		detail.Text=k;
		Session["NowBuiltSub"]=foundRow["CombSubID"].ToString();
		Session["NowPart"]=Level3.DataKeys[Level3.SelectedItem.ItemIndex];
		Session["NowPartName"]=foundRow["PartName"].ToString();
		Session["NowPhoto"]=foundRow["Combine"].ToString();
		Session["LocLeft"]=foundRow["LocLeft"].ToString();
		Session["LocTop"]=foundRow["LocTop"].ToString();
		Session["Model"]=foundRow["Model"].ToString();
		Session["Color"]=foundRow["Color"].ToString();
		Session["UpricePart"]=foundRow["UpricePart"].ToString();
		Session["LocZindex"]=foundRow["LocZindex"].ToString();
	}
	void Join_Click(object sender,EventArgs e){
		string n="";
		n="<div  id=plan style=\"position: relative ; z-index:1; display:\"><img border=0 src=\"images\\CB\\Lpic\\Type\\UB-TYPE01.jpg\" alt=底图>";
		DataRow foundRow;
		DataTable t = (DataTable)Session["NowUserData"];
		foundRow = t.Rows.Find(Session["NowBuiltSub"].ToString());
		if(foundRow!=null){
			foundRow["UserPartID"]=Session["NowPart"].ToString();
			foundRow["UserPartName"]=Session["NowPartName"].ToString();
			foundRow["Photo"]=Session["NowPhoto"].ToString();
			foundRow["LocLeft"]=Session["LocLeft"].ToString();
			foundRow["LocTop"]=Session["LocTop"].ToString();
			foundRow["Model"]=Session["Model"].ToString();
			foundRow["Color"]=Session["Color"].ToString();
			foundRow["UpricePart"]=Session["UpricePart"].ToString();
			foundRow["LocZindex"]=Session["LocZindex"].ToString();
			DataTable t1 = (DataTable)Session["NowUserData"];
			Level5.DataSource=t1.DefaultView;
			Level5.DataBind();
			UserLevel5.DataSource=t1.DefaultView;
			UserLevel5.DataBind();
			foreach(DataRow Row in t.Rows){
				n+= "<div id=" + Row["PartID"] + " style=\"position: absolute; left: " + Row["LocLeft"] + "; top:" + Row["LocTop"] + "; z-index:" + Row["LocZindex"] + ";display:\"><img border=0 src=\"" + Row["photo"] + "\"></div>";
			}
			n+= "</div>";
			s1.InnerHtml= n;
		}
	}
</script>
</head>

<body background="../../images/winback.gif">
<form runat="server">
<asp:hyperlink text="回首页" NavigateUrl="Default.aspx" runat="server" />
<center>   <table border="1" width="100%">
   <tr>
   <td class="Tab_02_1" align="center">主要项目：</td>
   <td class="Tab_02_2" width="80%" >
    <asp:DataList id="Level1" runat="server"
        RepeatColumns="4"
		RepeatDirection="Horizontal"
		BorderColor="black"
        CellPadding="3"
        Font-Name="Verdana"
        Font-Size="10pt"
        HeaderStyle-BackColor="#aaaadd"
        >
		<ItemTemplate>
			<asp:LinkButton CommandArgument=<%# DataBinder.Eval(Container.DataItem, "CatPartID") %> Text=<%# DataBinder.Eval(Container.DataItem, "CatPartName") %> onCommand="Level1_Click" Runat="server" />
		</ItemTemplate>
	</asp:datalist>
   </td></tr>
   <tr>
   <td class="Tab_02_1" align="center">内　　容：</td>
   <td class="Tab_02_2">
	
    <asp:DataList id="Level2" runat="server"
        RepeatColumns="10"
		RepeatDirection="Horizontal"
        BorderColor="black"
        CellPadding="3"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        >
		<ItemTemplate>
			<asp:LinkButton CommandArgument=<%# DataBinder.Eval(Container.DataItem, "CatSubPartID") %> Text=<%# DataBinder.Eval(Container.DataItem, "CatSubPartName") %> onCommand="Level2_Click" Runat="server" />
		</ItemTemplate>
	</asp:datalist>
	</td>
	</tr>
	</table>
<table border="0"> <tr><td>
<table >
<tr>
<td>

<span ID="s1" RUNAT="SERVER"/>
</td>

<td>
</table>
</td>
<td valign="top" width="100%">
<table BORDER="1"><tr><td valign="middle" colspan="2" lign="center" class="Tab_02_1">细目内容
       </td>
       </tr>
       <tr><td valign="top">
       <asp:DataList id="Level3" runat="server"
            RepeatColumns="1"
			RepeatDirection="Horizontal"
			align="center"
			BorderColor="black"
            CellPadding="3"
            Font-Name="Verdana"
			SelectedItemStyle-bordercolor="Red"
			SelectedItemStyle-borderstyle="Solid"
			SelectedItemStyle-borderwidth="1"
            Font-Size="8pt"
			DataKeyField="PartID"
            HeaderStyle-BackColor="#aaaadd"
  			borderWidth="0"
			OnSelectedIndexChanged="ShowDetail"
            >
			<ItemTemplate> 
				<%# DataBinder.Eval(Container.DataItem, "PartName") %>
				<br>
				<asp:Hyperlink target="_blank" id="h1" NavigateUrl=<%# DataBinder.Eval(Container.DataItem,"Large") %> runat="server">
				<asp:Image id=Button1 ImageUrl=<%# DataBinder.Eval(Container.DataItem, "small") %> runat="server" />
				</asp:hyperlink>
				<asp:LinkButton id="Detail1" runat="server" Text="详细资料..." CommandArgument='<%# DataBinder.Eval(Container.DataItem, "CombSubID") %>' CommandName="select" />
			</ItemTemplate>
		</asp:datalist>
		</td>
		<td BORDER="1" valign="top">
			<asp:label id="detail" Font-Size="10pt" runat="server"/>
			<p><asp:linkButton text="选择" id="Join" onclick="Join_Click" runat="server" />
        </td>
        </tr>
</table>
</td></tr></table>

<asp:button id="ChoiceOK" onclick="OKClick" Text="选择确认"  Runat="server" />

<table border="1" width="100%">
<tr>
<td  class="Tab_02_1"  cellpadding="5"  cellspacing="0">
已选之内容：
</td>
</tr>
<tr>
<td>
<asp:Table id="Table1" Font-Name="Verdana" Font-Size="8pt" CellPadding=5 CellSpacing=0 BorderColor="black" BorderWidth="1" Gridlines="Both" runat="server"/>
<asp:label id="test" Visible=False runat="server"/>
    <ASP:DataGrid id="UserLevel5" runat="server"
		BorderColor="black"
        BorderWidth="1"
		columns="2"
	    GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="false"
		>
<Columns>
<asp:BoundColumn HeaderText="名称" DataField="PartName" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="颜色" DataField="Color" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="型号" DataField="Model" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="价格" DataField="UpricePart" DataFormatString="{0:C0}"  ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" />
</Columns>
</asp:datagrid>

</td>
</tr>
<tr>
<td>
Dataset内容：
    <ASP:DataGrid id="Level5" runat="server"
		BorderColor="black"
        BorderWidth="1"
		columns="2"
	    GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
        Visible="True"
		/>

</td>
</tr>
</table>
<asp:label id="nn" runat="server" />
</form>
</center>
</html>