<%@ Page Language="C#" ContentType="text/html" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="My" TagName="MyHead" Src="head.ascx" %>
<%@ Register TagPrefix="My" TagName="MyFoot" Src="foot.ascx" %>
<%@ Register TagPrefix="My" TagName="MyMessage" Src="message.ascx" %>

 <!-- #INCLUDE file="../inc/Function.aspx" -->
<script runat="server">

     String strSQL;
	 String UserName;
     String strConn = ConfigurationSettings.AppSettings["ConnectionString"];
	 string AdvUserName; 
     int AdvListId;
	 string Action;
	 int id;
	 string Temp;
	 int isAcceptAppearance=2;  //在修改模式下记录当前记录的状态
	 
protected void Page_Load(Object Src, EventArgs E)
{
    //------判断用户是否登录---
     if(Session["Login_UserName"]==null || Session["IsLogin"]==null || Session["Userclass"]==null || Session["Login_UserName"]=="" || Session["IsLogin"].ToString().ToLower()!="true" )
   {   
      Response.Write("<script language=javascript>alert ('登录超时');document.location='../ ';<");
	  Response.Write("/script>");
	  Response.End();
    }
   AdvUserName=Session["Login_UserName"].ToString();
  //-------------------------


   AdvListId=Convert.ToInt32(Codeschar(Request.Params["AdvListId"]));
   Action=Codeschar(Request.Params["Action"]).ToLower();
   Temp=Codeschar(Request.Params["id"]);
   
   if(IsNum(Temp))
    {
	 id=Convert.ToInt32(Codeschar(Request.Params["id"].Trim()));
	 }
	else
	{
	 id=0;
	}
	
 strSQL="Select * from AdvList where AdvUserName ='"+AdvUserName+"' and istemp=1 and id="+AdvListId;
   SqlConnection cn = new SqlConnection(strConn);
    SqlCommand cmd= new SqlCommand(strSQL,cn);
	cn.Open();
	//Response.Write(strSQL);
	//Response.End();
	SqlDataReader myReader=cmd.ExecuteReader();
	  if (myReader.Read())
	  {
	   if (!IsPostBack)
	    {
		 AdvType.Text=myReader["AdvType"].ToString(); 
		 AdvTitle.Text=myReader["AdvTitle"].ToString();
		 if(Convert.ToInt32(myReader["AdvType"])==2)
		 {
		 reg.Visible=false;
		 ImgAddress.Text="0";
		 Format.Text="0";
		 }
		 else
		 {
		 reg.Visible=true;
		 }
		}
		cn.Close();
		
	   }
	   else
	   {
	    cn.Close();
	    alertmsg("出错!!记录不存","MangerAdvList.aspx");
		Response.End();
	    }
		
 if(Action=="edit" && id>0)
  {
   strSQL="Select * from AdvDetailed where AdvUserName ='"+AdvUserName+"' and istemp=1 and id="+id;
   cn = new SqlConnection(strConn);
   cmd= new SqlCommand(strSQL,cn);
   cn.Open();
   myReader=cmd.ExecuteReader();
   if (myReader.Read())
   {
    Button1.Text="提交修改";
	isAcceptAppearance=Convert.ToInt32(myReader["AcceptAppearance"]);
	 if (!IsPostBack)
	 {
	  Format.Text=myReader["Format"].ToString();
	  widthheight.SelectedValue=myReader["width"].ToString()+"_"+ myReader["height"].ToString();
	  AdvTitle.Text=myReader["AdvTitle"].ToString();
	  url.Text=myReader["url"].ToString();
	  UrlParameter.Text=myReader["UrlParameter"].ToString();
	  ImgAddress.Text=myReader["ImgAddress"].ToString();
	  }
   }
   else
   {
	    cn.Close();
	    alertmsg("出错!!记录不存","MangerAdvList.aspx");
		Response.End();
    }
  }
   else
  {
    //为添加新记录
   }
 
}


// 添加记录
 public void AddAdvDetailed(object sender , EventArgs E)
 {
    int TempID;
	int IsTemp;
	
	if(Action=="edit" && id>0)  //是否为修改记录
	 {
	  TempID=id;
	  IsTemp=0;
	 }
	 else
	 {
	  IsTemp=1;
	  TempID=0;
	 }
    
   SqlConnection cn = new SqlConnection(strConn);
   SqlCommand cmd= new SqlCommand(strSQL,cn);
   
   
 
 if(IsTemp==0 && isAcceptAppearance==2)  
   {
    strSQL="update AdvDetailed set AdvTitle='"+ AdvTitle.Text.ToString() +"',";
  if (Convert.ToInt32(AdvType.Text)==2)
  {
     strSQL+="Format=0 ,"; 
     strSQL+="width=0 ,";
	 strSQL+="height=0 ,";
	 strSQL+="ImgAddress=0 ,";
  }
  else
  {
   strSQL+= "Format="+Format.Text +",";
   char [] delimiter = "_".ToCharArray();
   strSQL+="width="+widthheight.SelectedItem.Value.ToString().Trim().Split(delimiter)[0]+",";
   strSQL+="height="+widthheight.SelectedItem.Value.ToString().Trim().Split(delimiter)[1]+",";
   strSQL+="ImgAddress='"+ ImgAddress.Text.ToString() +"',";
   }
	strSQL+= " UrlParameter='"+UrlParameter.Text +"'";
	strSQL+= " where AdvUserName ='"+AdvUserName+"' and istemp=1 and id="+id;
    	 cn=new SqlConnection(strConn);
	     cmd= new SqlCommand(strSQL,cn);
	     cn.Open();
		 cmd.ExecuteNonQuery();  //添加记录
	     cn.Close();
		 //Response.Write(strSQL);
		 //Response.End();
		alertmsg("提交修改成功!!","ManageAdvDetailed.aspx?AdvListId="+AdvListId); 
   }
   else
   {
    
	if(IsTemp==0)
	{
	 strSQL="delete from AdvDetailed where AdvUserName ='"+AdvUserName+"' and TempID="+id+" and  istemp=0 and AdvListId="+AdvListId;
	    cn=new SqlConnection(strConn);
	     cmd= new SqlCommand(strSQL,cn);
	     cn.Open();
		 cmd.ExecuteNonQuery();  //添加记录
	     cn.Close();
	  strSQL="update AdvDetailed set AcceptAppearance=3 where AdvUserName ='"+AdvUserName+"' and istemp=1 and id="+id;
	    cn=new SqlConnection(strConn);
	     cmd= new SqlCommand(strSQL,cn);
	     cn.Open();
		 cmd.ExecuteNonQuery();  //添加记录
	     cn.Close();	   
	 }
   
  strSQL="insert into AdvDetailed (AdvListId,AdvUserName,AdvTitle,AcceptAppearance,Format,width,height,ImgAddress,url,UrlParameter,TempID,IsTemp,AddData)values (";
  strSQL+=AdvListId +",";
  strSQL+="'"+AdvUserName +"',";
  strSQL+="'"+ AdvTitle.Text.ToString() +"',";
  strSQL+= 2 +",";                       //对新添加的记录初使状态为 1 通过审核 2 等待审核
  
  if (Convert.ToInt32(AdvType.Text)==2)
  {
     strSQL+=0 +","; 
     strSQL+=0 +","; 
	 strSQL+=0 +",";
	 strSQL+=0 +",";
  }
   else
  {
   strSQL+= Format.Text +",";
   char [] delimiter = "_".ToCharArray();
   strSQL+=widthheight.SelectedItem.Value.ToString().Trim().Split(delimiter)[0]+",";
   strSQL+=widthheight.SelectedItem.Value.ToString().Trim().Split(delimiter)[1]+",";
   strSQL+="'"+ ImgAddress.Text.ToString() +"',";
   }
   
   strSQL+="'"+ url.Text.ToString() +"',";
   strSQL+="'"+ UrlParameter.Text.ToString() +"',";
    strSQL+=TempID +",";
    strSQL+=IsTemp +",";
    strSQL+="'"+ System.DateTime.Now +"')";
	    cn=new SqlConnection(strConn);
	     cmd= new SqlCommand(strSQL,cn);
	     cn.Open();
		 cmd.ExecuteNonQuery();  //添加记录
	     cn.Close();
		 
	  if(IsTemp==0)
	   {
	    alertmsg("修改申请成功！！请等街审核","ManageAdvDetailed.aspx?AdvListId="+AdvListId); 
	   }else {
		alertmsg("新广告位添加成功","ManageAdvDetailed.aspx?AdvListId="+AdvListId); 
		}
	
  }
    	
 	    //alertmsg("操作成功","ManageAdvDetailed.aspx?AdvListId="+AdvListId);
		//Response.End();
  }
  
  
 protected string Adv_AdvType(string str)
  {
   switch(str)
    {
	 case "1":
	  return "<font color=#339900>点击广告</font>";
	 break;
	 case "2":
	  return "<font color=#0000ff>弹窗广告</font>";
	 break;
	 case "3":
	  return "CPA记费";
	 break;
	 default:
	  return "<font color=#ff0000>点击广告</font>";
	  break;
	 }
   }



 //上传文件
 public void UploadFile(object sender , EventArgs E)
 {
   //检查上传文件不为空
   if(myFile.PostedFile!=null)
   {     
  string nam = myFile.PostedFile.FileName ;
  //取得文件名(抱括路径)里最后一个"."的索引
  int i= nam.LastIndexOf(".");
  //取得文件扩展名
  string newext =nam.Substring(i);
     newext=newext.ToLower();
   string myFileType=myFile.PostedFile.ContentType.ToLower();
   
  if(myFileType=="image/gif" || myFileType=="application/x-shockwave-flash" ||  myFileType=="image/pjpeg" || myFileType=="image/bmp") 
   {
    if(newext.Substring(1)=="gif" || newext.Substring(1)=="jpg"||newext.Substring(1)=="bmp" || newext.Substring(1)=="swf")
    {
	 
	 if(newext.Substring(1)=="swf")
	  {Format.Text="1";}else{Format.Text="2";}
	  
     DateTime now = DateTime.Now; 
     string newname=now.ToString().Replace(" ","").Replace("-","").Replace(":","")+myFile.PostedFile.ContentLength.ToString();
	  myFile.PostedFile.SaveAs(Server.MapPath("../count/img/" + newname + newext));
   //fname.Text=newname;
   //fname.Text=myFile.PostedFile.FileName;
  //fname.Text=myFile.PostedFile.ContentType;
  //fname.Text=(myFile.PostedFile.ContentLength/1024).ToString();
  ImgAddress.Text="/count/img/" + newname + newext;
   
   } else
   { Response.Write("<script>alert ('只允许上传 gif,jpg,bmp,swf格式的文件')</"); Response.Write("script>");}
   }else 
   {  Response.Write("<script>alert ('格式不对 只允许上传图片和flash的文件 ')</"); Response.Write("script>");}
  }
 }

</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>5507广告联盟 - 广告商管理后台 - 修改广告列表</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head>
<body>
<my:MyHead runat="server"/>

<table width="980" border="0" align="center" cellpadding="0" cellspacing="0" class="table_3">
  <tr>
    <td width="20" height="15"><img src="../web/images/body_1.gif" /></td>
    <td width="926" background="../web/images/body_2.gif"></td>
    <td width="20"><img src="../web/images/body_3.gif" width="20" height="15" /></td>
  </tr>
</table>
<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="15" background="../web/images/body_7.gif" bgcolor="#FFFFFF"><p>&nbsp;</p>
        <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td width="953" valign="top" bgcolor="#FFFFFF"><table width="950" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="140" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30"><table width="98%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="6%"><div align="center"><img src="../images/adv_list.gif" width="25" height="25" /></div></td>
                <td width="30%"><strong>添加详细广告位</strong></td>
                <td width="64%"><span class="STYLE1">注：新添加的广告要等审核后才能投放 带 * 的不能为空 </span></td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="30">
			  <form runat="server" enctype="multipart/form-data">
			<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E4E4E4">
              <tr>
                <td height="25" colspan="2" background="../web/images/top_img.gif" bgcolor="#FFFFFF"><div align="center"><strong>详细参数</strong></div></td>
                </tr>
              <tr>
                <td width="24%" height="25" bgcolor="#FFFFFF"><div align="right">广告名称：</div></td>
                <td width="76%" bgcolor="#FFFFFF"><asp:TextBox ID="AdvTitle" runat="server" />
				 <asp:requiredFieldValidator controltovalidate="AdvTitle" ErrorMessage="广告名称不能为空"  display="Dynamic" runat="server"></asp:requiredFieldValidator>
                  <span class="STYLE1">*</span></td>
                </tr>
              <tr>
                <td height="25" bgcolor="#FFFFFF"><div align="right">链接地址:</div></td>
                <td bgcolor="#FFFFFF"><asp:TextBox ID="url" runat="server" />
				 <asp:requiredFieldValidator controltovalidate="url" ErrorMessage="链接地址不能为空"  display="Dynamic" runat="server"></asp:requiredFieldValidator>
				<asp:RegularExpressionValidator Display="Dynamic" controltovalidate="url" runat="server" ErrorMessage="URL格式不正确" ValidationExpression="http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" /> 
				
                  <span class="STYLE1">*</span></td>
              </tr>
              <tr>
                <td height="25" bgcolor="#FFFFFF"><div align="right">链接可选参数:</div></td>
                <td bgcolor="#FFFFFF"><asp:TextBox ID="UrlParameter" runat="server" />
                  地址后面的内容 可用变量 </td>
              </tr>
            </table>
			
			<asp:panel id="reg" Runat="server">
			<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E4E4E4" class="table_3">
              <tr>
                <td height="25" colspan="2" background="../web/images/top_img.gif" bgcolor="#FFFFFF"><div align="center"><strong>广告参数</strong></div></td>
              </tr>
              <tr>
                <td width="24%" height="25" bgcolor="#FFFFFF"><div align="right">尺寸:</div></td>
                <td width="76%" bgcolor="#FFFFFF">
				<asp:DropDownList ID="widthheight" runat="server">
	             <asp:ListItem Value="760_80" Text="760×80" runat="server"></asp:ListItem>
	             <asp:ListItem Value="760_60" Text="760×60" runat="server"></asp:ListItem>
	             <asp:ListItem Value="760_30" Text="760×30" runat="server"></asp:ListItem>
	             <asp:ListItem Value="580_80" Text="580×80" runat="server"></asp:ListItem>
	             <asp:ListItem Value="580_60" Text="580×60" runat="server"></asp:ListItem>
	             <asp:ListItem Value="468_80" Text="468×80" runat="server"></asp:ListItem>
	             <asp:ListItem Value="468_60" Text="468×60" runat="server"></asp:ListItem>
	             <asp:ListItem Value="300_300" Text="300×300" runat="server"></asp:ListItem>
	             <asp:ListItem Value="250_250" Text="250×250" runat="server"></asp:ListItem>
	             <asp:ListItem Value="160_250" Text="160×250" runat="server"></asp:ListItem>
	             <asp:ListItem Value="150_150" Text="150×150" runat="server"></asp:ListItem>
	             <asp:ListItem Value="150_200" Text="150×200" runat="server"></asp:ListItem>
	             <asp:ListItem Value="150_300" Text="150×300" runat="server"></asp:ListItem>	 
	             <asp:ListItem Value="100_100" Text="100×100" runat="server"></asp:ListItem>
	             <asp:ListItem Value="100_200" Text="100×200" runat="server"></asp:ListItem>
	             <asp:ListItem Value="100_300" Text="100×300" runat="server"></asp:ListItem>
	             <asp:ListItem Value="90_90" Text="90×90" runat="server"></asp:ListItem>			 
				</asp:DropDownList>
				其它规格请与我们联系</td>
              </tr>
              <tr>
                <td height="25" bgcolor="#FFFFFF"><div align="right">上传文件:</div></td>
                <td bgcolor="#FFFFFF">
				  <input type="file" name="myFile" runat="server" OnServerClick="UploadFile" ID="myFile" >
				  <asp:Button ID="Button2" runat="server" Text="上传" CausesValidation="false" OnClick="UploadFile"/>
				  
				  只能上传 gif、jpg、bmp、swf文件                 </td>
              </tr>
              <tr>
                <td height="25" bgcolor="#FFFFFF"><div align="right">图片地址:</div></td>
                <td bgcolor="#FFFFFF">
				<asp:TextBox ID="ImgAddress" runat="server" Columns="50" ReadOnly="true" />
				 <asp:requiredFieldValidator controltovalidate="ImgAddress" ErrorMessage="图片地址不能为空"  display="Dynamic" runat="server"></asp:requiredFieldValidator>
                  <span class="STYLE1">* </span></td>
              </tr>
            </table>
			</asp:panel>
			
			<p align="center">
              <asp:TextBox ID="AdvType" runat="server" Visible="false" />
			   <asp:TextBox ID="Format" runat="server" Visible="false" />
			  <asp:Button ID="Button1" Text="添加广告" OnClick="AddAdvDetailed" runat="server" />			  </p>
			  </form>			</td>
          </tr>
          <tr>
            <td height="30">
			
			
			<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#E4E4E4" class="table_3">
              <tr>
                <td height="81" bgcolor="#FDFDFD"><p class="text_20">
				<span class="STYLE1">注：</span>链接可选参数是用来动态替换地址参数说明如下<br />　　
                  #WebUserName#　点击广告的用户名 <br />
　　#FromUrl# 广告点击来源<br />
　　#Parameter# 为每一个广告分配的对应参数 <br />
　　#FromIp# 点击来源ＩＰ<br />
例：链接地址为: http://www.abc.com/　链接参数为: ?user=#WebUserName#&amp;From=#FromUrl#<br />
如果当前投广告广告用户名为 test 来源是 http://www.test.com　则广告转向地址为 http://www.abc.com/?user=test&amp;from=http://www.test.com
</p>
                  </td>
              </tr>
            </table></td>
          </tr>
          
        </table>
		 
		</td>
        <td width="220" valign="top"><my:MyMessage runat="server"/></td>
      </tr>
    </table></td>
    <td width="15" background="../web/images/body_8.gif" bgcolor="#FFFFFF">&nbsp;</td>
  </tr>
</table>

<table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="20"><img src="../web/images/body_4.gif" width="20" /></td>
    <td width="926" background="../web/images/body_5.gif">&nbsp;</td>
    <td width="20"><div align="right"><img src="../web/images/body_6.gif" width="20" height="20" /></div></td>
  </tr>
</table>

<my:MyFoot runat="server"/>

</body>
</html>
