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

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

 <!-- #INCLUDE file="../inc/Function.aspx" -->
 
<script runat="server">
     String strSQL;
	 String UserName;
     String strConn = ConfigurationSettings.AppSettings["ConnectionString"];
	 string AdvUserName; 
		 const int Record_Per_Page=20; //每页的记录数
         int nPagecount; //总页数
         int nRecCount; //定义总记录数
         int nPage; //定义当前页
		 DataSet ds=new DataSet();
         SqlDataAdapter da = new SqlDataAdapter();

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();
  //-------------------------
   
      CreateDataSetObject();
   if(!IsPostBack)
    {
	 nPage=0;
	 CmdDataBind();
	 Page_nPage.Text=(nPage+1).ToString();
	 Page_nRecCount.Text=nPagecount.ToString();
	 IsFirstLastPage(); 
	}
    
}


protected void CreateDataSetObject()
 { 
     strSQL= "Select * From AdvList where AdvUserName='" + AdvUserName + "' and IsTemp=1 order by id desc";
	  da = new SqlDataAdapter(strSQL,strConn);
	  //da.SelectCommand.CommandType=CommandType.Text; 
	 da.Fill(ds,"AdvList");
	 //DataGrid1.DataSource=ds;
	 //DataGrid1.DataBind();
	//Response.Write(strSQL);
	 
	 nRecCount=ds.Tables[0].Rows.Count;
	 
	 if(nRecCount%Record_Per_Page==0)
	 {
	  nPagecount=nRecCount/Record_Per_Page;
	  }else
	   {
	   nPagecount=nRecCount/Record_Per_Page+1;
	   }
  }
  
  
 protected void CmdDataBind()
  {
   
    ds.Clear();
    da.Fill(ds,nPage*Record_Per_Page,Record_Per_Page,"AdvList");
    DataGrid1.DataSource=ds;
	DataGrid1.DataBind(); 
	ispages.Text=nPage.ToString();
	//Console.WriteLine(ds.Tables["AdvList"].Rows[1]);
	
	//DataGrid1.Tables[0].Rows[1].Page_Del.Enabled=false;	
   }
   
  
 protected string Adv_AdvAppearance(string str)
  {
   switch(str)
    {
	 case "0":
	  return "<font color=#339900>未结算</font>";
	 break;
	 case "1":
	  return "<font color=#0000ff>正常</font>";
	 break;
	 case "2":
	  return "结束";
	 break;
	 case "3":
	  return "<font color=#ff0000>停止</font>";
	 break;
	 case "4":
	  return "<font color=#ff0000>暂停</font>";
	 break;
	 default:
	  return "<font color=#ff0000>暂停</font>";
	  break;
	 }
   }
  
  protected string Adv_AcceptAppearance(string str)
  {
   switch(str)
    {
	 case "1":
	  return "<font color=#339900>通过</font>";
	 break;
	 case "2":
	  return "<font color=#0000ff>等待</font>";
	 break;
	 case "3":
	  return "挂起";
	 break;
	 default:
	  return "<font color=#ff0000>等待</font>";
	  break;
	 }
   }
   
 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;
	 }
   }
   
 protected string Adv_IsExamine(string str)
  {
   switch(str)
    {
	 case "1":
	  return "不要";
	 break;
	 case "2":
	  return "<font color=#0000ff>需要</font>";
	 break;
	 case "3":
	  return "已满";
	 break;
	 default:
	  return "<font color=#ff0000>暂停</font>";
	  break;
	 }
   }



 

protected void MyDataGrid_Delete(Object Src, DataGridCommandEventArgs E)

{
  int ProductID =(int)DataGrid1.DataKeys[(int)E.Item.ItemIndex]; 
   
 }
  


//分页
 private void Page_Click(Object Src, EventArgs E)
  {
   string commangArg=((LinkButton)Src).CommandArgument;
   if(!IsNum(ispages.Text))
    {
	 nPage=0;
	 }
	 else
	 {
     nPage=Convert.ToInt32(ispages.Text);
	 }
   switch(commangArg)
    {
	 case "First": //如果点首页
	  {
	  nPage=0;
	  break;
	  }
	  case "Prev": //上一页
	  {
	   nPage=(int)Math.Max(0,nPage-1);
	   break;
	   }
	  case "Next":  //下一页
	  {
	   nPage=(int)Math.Min(nPagecount-1,nPage+1);
	   break;
	   }
       case "Last": //尾页
	    {
		 nPage=nPagecount-1;
		 break;
		 }
		default:{break;};  
	 }
	 
	// Response.Write(nPagecount+" "+nPage);
	 IsFirstLastPage();
	 Page_nPage.Text=(nPage+1).ToString();
	 Page_nRecCount.Text=nPagecount.ToString();
	 CmdDataBind();
   }
   
 // 分页按妞是否可见
 private void IsFirstLastPage()
  {
   if (nPagecount>1)
    {
	 if(nPagecount!=1)
	  {
	    if(nPage==0){PagePrev.Enabled=false;PageFirst.Enabled=false;}
		else{PagePrev.Enabled=true;PageFirst.Enabled=true;}
	    if(nPage==nPagecount-1){PageNext.Enabled=false; PageLast.Enabled=false;}
		else{PageNext.Enabled=true; PageLast.Enabled=true;}
		
	   }
	   else
	   {
	    PagePrev.Enabled=false; PageNext.Enabled=false;
	   }
	 }
   else
   {
    PageFirst.Enabled=false; PagePrev.Enabled=false; PageNext.Enabled=false; PageLast.Enabled=false;
    }
   }
  
  
  
  
  // 添加 DataSet 数据
 private void Add_DataSet(Object Src, EventArgs E)
  {

	DataRow dc=ds.Tables["AdvList"].Rows[0];
	 dc.BeginEdit();
	 dc["AdvAppearance"]=1; 
	 dc.EndEdit();
	 Console.WriteLine(ds.Tables[0].Rows[1].RowState);
	 
	 //ds.Tables["AdvList"].Rows.Add(dc);
	 da.Update(ds,"AdvList");
 	 Response.Write("添加成功");
     CmdDataBind();
  }
 
  
 // 删除记录
 private void Del_DataSet(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
 {
   
   if(e.CommandName.ToString()=="del")
   {
    if(e.Item.Cells[0].Text.ToString()=="");
   SqlConnection cn = new SqlConnection(strConn);
   string DeleteSql="Delete From AdvList where id=@id and AdvUserName='" + AdvUserName + "'and AdvAppearance=0 or AdvAppearance=2";
   SqlCommand SComm= new SqlCommand(DeleteSql,cn);
   SComm.Parameters.Add(new SqlParameter("@id",SqlDbType.Int));
   SComm.Parameters["@id"].Value=Convert.ToInt32(e.Item.Cells[0].Text);
   cn.Open();
   try
   { 
    SComm.ExecuteNonQuery();
    //Response.Write("删除成功");
   }
   catch(Exception s)
   { 
    Response.Write(s.Message);
   }
   cn.Close();
   CmdDataBind(); 
   //Response.Write((int)DataGrid1.DataKeys[(int)e.Item.ItemIndex]);
    //msgbox("删除成功");
  // da.Update(ds,"AdvList");
  
  }
  }
 
 private string IsEdit(int id,int app)
 {
  if (app==3)
  {
    return "<a disabled=disabled>修改</a>";
   }else
   return "<a href=EditAdvList.aspx?id="+id+"><font color=#FF6600>修改</font></a>"; 
  }
  
 private string IsAddAdv(int id,int app)
 {
  if(app==0||app==2||app==3)
  {
    return "<a disabled=disabled>添加广告</a>";
   }else
   return "<a href=AddAdvDetailed.aspx?AdvListId="+id+"><font color=#009900>添加广告</a></a>"; 
  }
 
public void MyDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
  //Response.Write(e.Item.Cells[10].Text +"<br>");
  
　 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
	  
	 if(e.Item.Cells[10].Text=="1"||e.Item.Cells[10].Text=="3"||e.Item.Cells[10].Text=="4")
	  {
	   //e.Item.Cells[9].Page_Del.Enabled=false;
        ((LinkButton)e.Item.FindControl("Page_Del")).Enabled=false;
	  // Response.Write("少地");
	   }
	 }

}
</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" />
</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%">&nbsp;</td>
              </tr>
            </table></td>
          </tr>
          <tr>
            <td height="30">
			<form runat="server">
			
			 <asp:DataGrid 
			 AutoGenerateColumns="false"
			 AllowSorting="True"
			 BackColor="#ffffff"
			  BorderColor="#dddddd" 
			  BorderWidth="1" 
			  Height="25" 
			  ID="DataGrid1" 
			   runat="server" 
			   ShowFooter="false" 
			   ShowHeader="true" 
			   HeaderStyle-Height="25"
			   FooterStyle-ForeColor="#EAEAEA"
			   HeaderStyle-BackColor="#F3F3F3"
			   HeaderStyle-HorizontalAlign="Center"
			   OnItemCommand="Del_DataSet"
			   DataKeyField="id"
			   Width="970" 
			   gridline="Both"
			   AlternatingItemStyle-BackColor="#F7F7F7"
			   OnItemDataBound="MyDataGrid_ItemDataBound"
				>
			  <columns>
			  
			   <asp:boundcolumn datafield="id" ItemStyle-Width="30" ItemStyle-Height="25" ItemStyle-HorizontalAlign="Center" HeaderText="NO"/>
			   
			   <asp:boundcolumn datafield="AdvTitle" ItemStyle-Width="200" ItemStyle-HorizontalAlign="Center" HeaderText="广告名称"/>
			   
				
          <asp:TemplateColumn ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" HeaderText="广告类型">
                <ItemTemplate>
					<%# Adv_AdvType(DataBinder.Eval(Container.DataItem,"AdvType").ToString()) %>				</ItemTemplate>			   		 
			</asp:TemplateColumn>
				
			
          <asp:TemplateColumn ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center" HeaderText="审核">
                <ItemTemplate>
					<%# Adv_IsExamine(DataBinder.Eval(Container.DataItem,"IsExamine").ToString()) %>				</ItemTemplate>			   		 
			</asp:TemplateColumn>

          <asp:TemplateColumn ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center" HeaderText="受理">
                <ItemTemplate>
					<%# Adv_AcceptAppearance(DataBinder.Eval(Container.DataItem,"AcceptAppearance").ToString()) %>				</ItemTemplate>			   		 
			</asp:TemplateColumn>
		
          <asp:TemplateColumn ItemStyle-Width="60" ItemStyle-HorizontalAlign="Center" HeaderText="状态">
                <ItemTemplate>
					<%# Adv_AdvAppearance(DataBinder.Eval(Container.DataItem,"AdvAppearance").ToString()) %>				                </ItemTemplate>			   		 
			</asp:TemplateColumn>
			
				
	<asp:boundcolumn datafield="GotoMoney" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" HeaderText="可用金额"/>	
	<asp:boundcolumn datafield="Unitprice" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" HeaderText="广告单价"/>	
   <asp:boundcolumn datafield="Quantity" ItemStyle-Width="100" ItemStyle-HorizontalAlign="Center" HeaderText="当前数量"/>

          <asp:TemplateColumn ItemStyle-Width="250" ItemStyle-HorizontalAlign="Center" HeaderText="操作">
                <ItemTemplate>	
				   					
					
					<%# IsEdit((int)DataBinder.Eval(Container.DataItem, "id"),(int)DataBinder.Eval(Container.DataItem, "AcceptAppearance")) %>
					
					<asp:LinkButton ID="Page_Del" Text="删除" runat="server" CommandName="del"/>
					
					
					<a href="Closebook.Aspx?id=<%# (int)DataBinder.Eval(Container.DataItem, "id")%>"><font color=#0000Ff>
					<%# ((int)(DataBinder.Eval(Container.DataItem, "AdvAppearance"))==0)
					 ?"结账"
					 :"续费"
					 %></font></a>
					
				 <%# IsAddAdv((int)DataBinder.Eval(Container.DataItem, "id"),(int)DataBinder.Eval(Container.DataItem, "AdvAppearance")) %>	
				 <a href="ManageAdvDetailed.aspx?AdvListId=<%# (int)DataBinder.Eval(Container.DataItem, "id")%>"><font color=#009900>广告管理</font></a>
				</ItemTemplate>			   		 
			</asp:TemplateColumn>
			
		  <asp:boundcolumn datafield="AdvAppearance" ItemStyle-Width="50" ItemStyle-HorizontalAlign="Center" HeaderText="状态" Visible="false"/>	
		 </columns>	
			</asp:DataGrid>
			
			 <table width="100%" border="0" cellspacing="0" cellpadding="0">
               <tr>
                 <td> 
				 <div align="center">
				 <asp:LinkButton ID="PageFirst" OnClick="Page_Click" Text="首页" runat="server" CommandArgument="First"/> 
				   |  
				     <asp:LinkButton ID="PagePrev" OnClick="Page_Click" Text="上一页" runat="server" CommandArgument="Prev"/>
				 | 
				 <asp:LinkButton ID="PageNext" OnClick="Page_Click" Text="下一页" runat="server" CommandArgument="Next"/> 
				 |  
				 <asp:LinkButton ID="PageLast" OnClick="Page_Click" Text="尾页" runat="server" CommandArgument="Last"/>
				 <asp:Label ID="CountPage" runat="server" />                  
				 转到:<asp:TextBox AutoPostBack="true" Columns="2" ID="ispages" runat="server" /> 
				 页 第 <asp:Label ID="Page_nPage" runat="server" ForeColor="Red"></asp:Label> 页/共 <asp:Label ID="Page_nRecCount" ForeColor="Red" runat="server"></asp:Label> 页</div> </td>
               </tr>
             </table>
			</form>
			<p>&nbsp;</p></td>
          </tr>
          <tr>
            <td height="30">&nbsp;</td>
          </tr>
        </table></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>
