using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DTable_App.DTablePhoto
{
	/// <summary>
	/// Imglist 的摘要说明。
	/// </summary>
	public class Imglist : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label L_bbsname;
		protected System.Web.UI.WebControls.Label L_top5;
		protected System.Web.UI.WebControls.Label L_zs;
		protected System.Web.UI.WebControls.Label L_gks;
		protected System.Web.UI.WebControls.Label L_today;
		protected System.Web.UI.WebControls.Label L_wgks;
		protected System.Web.UI.WebControls.Label L_user;
		protected System.Web.UI.WebControls.Label L_xc;
		protected System.Web.UI.WebControls.Label L_yhm;
		protected System.Web.UI.WebControls.Label L_addnew;
		protected System.Web.UI.WebControls.Label L_mg;
		protected System.Web.UI.WebControls.Label L_hideinput;
		protected System.Web.UI.WebControls.Label L_sm;
		protected System.Web.UI.WebControls.Label L_yxyh;
		protected dtable.dTable DTable1;
	
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			// 在此处放置用户代码以初始化页面
			DTable1.ConnStr=sharecode.ConnStr;
			DTable1.TableName ="DTable_Photo_Kind";
			string struser=sharecode.GetQueryStringValue("user");
			string stryhm=sharecode.GetSessionValue(sharecode.strUserSName);
			string strpid=sharecode.GetQueryStringValue("Pid");
			string strpName=sharecode.GetQueryStringValue("PName");
			bool meisok=false;
			L_yhm.Text=stryhm;

		
			if(struser=="")
			{
				sharecode.OutMsgBox("传入的参数错误",true);
			}



			L_user.Text="<a href=list.aspx?user=" + Page.Server.UrlEncode(struser) + ">" + struser + "</a>";
			L_xc.Text="<a href=\"Imglist.aspx?user=" + struser + "&Pid=" + strpid + "&PName=" + Page.Server.UrlEncode (strpName) + "\">" + Page.Server.UrlDecode(strpName) + "</a>";
Page.Response.Cookies[sharecode.strFromUrlSName].Value=Page.Request.Url.AbsoluteUri.ToString();
			Page.Response.Cookies[sharecode.strFromUrlSName].Expires=System.DateTime.Now.AddMinutes(20); 

			//开始查询所需的隐含控件
L_hideinput.Text="<input type=\"hidden\" name=\"user\" value=\"" + struser + "\"><input type=\"hidden\" name=\"pid\" value=\"" + strpid + "\"><input type=\"hidden\" name=\"pname\" value=\"" + strpName + "\">";

			//开始添加图片的链接

           L_addnew.Text="<a href=add.aspx?addnew=true&type=tp&user=" + struser + "&lb=" + strpid + "&PID=" + strpid + "&PName=" + Page.Server.UrlEncode(strpName) + ">" + L_addnew.Text + "</a>";


			//开始显示本相册信息
			string strtoday=System.DateTime.Now.ToString("yyyy-MM-dd");

			System.Data.SqlClient.SqlConnection oconn=new System.Data.SqlClient.SqlConnection(sharecode.ConnStr);
			System.Data.SqlClient.SqlCommand mycmd=new System.Data.SqlClient.SqlCommand("select top 1 (select count(*) from DTable_Photo b where 类别=" + strpid + ") as 照片总数,(select count(*) from DTable_Photo b where 类别=" + strpid + " and 公开=1) as 公开数,(select count(*) from DTable_Photo b where 类别=" + strpid + " and 发布时间 between '" + strtoday + " 0:00:00' and '" + strtoday + " 23:59:59') as 今日上传数,a.本类说明,a.允许用户 from DTable_Photo_kind a where a.id=" + strpid,oconn);
			oconn.Open();
			System.Data.SqlClient.SqlDataReader myreader=mycmd.ExecuteReader();
			string stryxyh="";

			if(myreader.Read())
			{
				L_zs.Text=myreader.GetValue(0).ToString();
				L_gks.Text=myreader.GetValue(1).ToString();
				L_today.Text=myreader.GetValue(2).ToString();
				L_wgks.Text=System.Convert.ToString(myreader.GetInt32(0)-myreader.GetInt32(1)); 
				L_sm.Text=myreader.GetValue(3).ToString();
				if(!myreader.IsDBNull(4))
				{
					stryxyh=myreader.GetValue(4).ToString();

				}

			}
			myreader.Close();
			//开始top5图片信息
			L_yxyh.Text=stryxyh;
			

			
			if(stryhm!="")
			{
				
				if(System.Convert.ToString("," + stryxyh.Trim() + ",").IndexOf("," + stryhm.Trim() + ",")>-1)
				{
					meisok=true;
					mycmd.CommandText="select top 5 id,照片主题,说明,照片地址,点击率,发布时间 from DTable_Photo where 类别=" + strpid + " order by 点击率 desc";
				}
				else
				{
					mycmd.CommandText="select top 5 id,照片主题,说明,照片地址,点击率,发布时间 from DTable_Photo where 类别=" + strpid + " and (公开=1 or 发布人='" + stryhm + "' or ',' + 允许用户 + ',' like '%," + stryhm + ",%') order by 点击率 desc";
				}
			}
			
			else
			{
				mycmd.CommandText="select top 5 id,照片主题,说明,照片地址,点击率,发布时间 from DTable_Photo where 类别=" + strpid + " and 公开=1 order by 点击率 desc";
				//	Page.Response.Cookies[sharecode.strFromUrlSName].Value=Page.Request.Url.AbsoluteUri.ToString();Page.Response.Cookies[sharecode.strFromUrlSName].Expires=System.DateTime.Now.AddMinutes(20); 
			}
			myreader=mycmd.ExecuteReader();
			System.Text.StringBuilder strtext=new System.Text.StringBuilder();
			while(myreader.Read())
			{
				string strtitle=myreader.GetValue(2).ToString().Replace("<br>","").Replace("'","") + "\n发布：" + myreader.GetValue(5).ToString() + "\n点击：" + myreader.GetValue(4).ToString();
strtext.Append("<td align=center><a href=\"show.aspx?user=" + struser + "&pid=" + myreader.GetValue(0).ToString() + "\" target=\"showimg\" title=\"" + strtitle + "\"><img src=\"img_user/" + struser + "/mini/" + myreader.GetValue(3).ToString() + "\" border=\"0\"><br>" + myreader.GetValue(1).ToString() + "</a></td>");

			}
			myreader.Close();
			mycmd.Dispose();
			oconn.Close();
			oconn.Dispose();
			if(strtext.Length>0)
			{
L_top5.Text="<table border=0><tr>" + strtext.ToString() + "</tr></table>";

			}

			string strsql="";//开始传入的查询
			string strkw=sharecode.GetQueryStringValue("DTable_BBS_kw");
			string strse=sharecode.GetQueryStringValue("DTable_BBS_Se");
			if(strkw!="")
			{
				if(strse=="主题")
				{
					strsql=" and 照片主题 like '%" + strkw + "%' ";
				}
				else if(strse=="说明")
				{
					strsql=" and 说明 like '%" + strkw + "%' ";
				}
				else if(strse=="模糊")
				{
					strsql=" and (照片主题 like '%" + strkw + "%' or 说明 like '%" + strkw + "%') ";
				}
			}

			if(stryhm!="" && meisok==true)//如果是相册允许用户,就全部开放
			{
	DTable1.List_SQL="select id,照片主题,'说明:' + 说明 + '\\n发布时间:' + convert(nvarchar(30),发布时间,120) + '\\n点击:' + convert(nvarchar(10),点击率) as 说明,照片地址,id from DTable_Photo where 类别=" + strpid + " " + strsql + " order by id desc";
				
			}

			else if(stryhm!="" && meisok==false)//如果用户登录,允许显示公开的和该用户被允许察看的
			{
				DTable1.List_SQL="select id,照片主题,'说明:' + 说明 + '\\n发布时间:' + convert(nvarchar(30),发布时间,120) + '\\n点击:' + convert(nvarchar(10),点击率) as 说明,照片地址,id from DTable_Photo where 类别=" + strpid + " and (发布人 ='" + stryhm + "' or 公开=1 or (',' + 允许用户 + ',') like '%," + stryhm + ",%') " + strsql + " order by id desc";
				if(stryhm==struser)
				{
					//开始添加管理链接
					
					L_mg.Text="<a href=\"add.aspx?meditid=" + strpid + "&user=" + struser + "&type=upxc&lb=" + strpid + "&pname=" + Page.Server.UrlEncode(strpName) + "\">更新相册</a> | <a href=\"add.aspx?delxc=" + strpid + "&user=" + struser + "&type=delete&lb=" + strpid + "&pname=" + strpName + "\" onclick=\"{if(confirm('即将删除本相册，并且所有图片都将无法恢复,是否继续(按取消放弃删除)?')){return true;}return false;}\">删除相册</a> | <a href=\"add.aspx?addnew=true&type=xc&user=" + struser + "\">添加相册</a>";
				}
			}
			else//只能察看公开的
			{
				DTable1.List_SQL="select id,照片主题,'说明:' + 说明 + '\n发布时间:' + convert(nvarchar(30),发布时间,120) + '\n点击:' + convert(nvarchar(10),点击率) as 说明,照片地址,id from DTable_Photo where 公开=1 and 类别=" + strpid + " " + strsql + "  order by id desc";
			}

			DTable1.Show_Enable =false;
			DTable1.List_Templ_Tr1="<tr><td width=\"149\" height=\"141\" align=\"center\" valign=\"top\" background=\"images/bg002.gif\"><table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\"> <tr> <td align=\"center\">&nbsp;</td> </tr> <tr> <td align=\"center\"><a href=\"show.aspx?user=" + struser + "&PID=@@fieldvalue4@@\" title=\"@@fieldvalue2@@\" target=\"showimg\">@@fieldvalue3@@</a></td> </tr> <tr> <td align=\"center\">[@@fieldvalue1@@]</td></tr></table></td></tr>";
			DTable1.List_Templ_Tr2=DTable1.List_Templ_Tr1; //设置行模板2的样式
			

			DTable1.List_Templ_Col=1; //设置了每行包含2条记录 
			DTable1.List_Sty_Table="height=\"145\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\""; 
			DTable1.Page_Pos=1; 
			DTable1.List_ShowTableTitle=false;
			DTable1.List_NumCol="none|||||||"; 
			DTable1.Page_Size=5;
			DTable1.List_Sty_Mouse="#FFFFFF|#FFFFFF||";
			DTable1.Page_ViewMode=2;
			DTable1.Page_Html_F="[首页]";
			DTable1.Page_Html_E="[末页]";
			DTable1.Page_Html_P="[前页]";
			DTable1.Page_Html_N="[后页]";
			DTable1.Page_Align ="center";
			DTable1.List_FieldFormat="3||urlimage|img_user/" + struser + "/mini/|||width=100|";
			//写入TItle
			Page.Response.Write("<script language=javascript>document.title='" + struser + "的相册：" + strpName + "';</script>"); 

		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
