<%@ Page Language="C#" ContentType="text/html" Inherits="Ycbw.Data.OleDbClient" %>

<%@ Register TagPrefix="Rajah" TagName="Head" src="../ctrls/head.ascx" %>
<%@ Register TagPrefix="Rajah" TagName="Foot" src="../ctrls/foot.ascx" %>

<script language="c#" runat="server">
private void Page_Load(object sender, EventArgs e)
{
	if( !Page.IsPostBack )
	{
		Initialize();
		
		this.p_width.Text = "600";
		this.p_height.Text = "400";
	}
}

private void Initialize()
{
	this.BindDropDownList("select name,id,depth from [b_article_categories] order by lft", "categories", cid);
	
	this.source.Text  = ConfigurationSettings.AppSettings["Title"];
	this.addtime.Text = DateTime.Now.ToString();
	
	if( Request.QueryString["id"] != null && Request.QueryString["id"] != "" )
	{
		this.Fill("select id from [b_article_categories] where parent = "+ Request.QueryString["id"] +" order by lft", "childs");
		
		if( Tables["childs"].Rows.Count > 0 )
		{
			//只有存在子栏目时，才存在自动选择子栏目的情况
			if( Session["cid"] != null && Session["cid"].ToString() != "" )
			{
				bool selected = false;
				
				foreach(System.Data.DataRow dr in Tables["childs"].Rows)
				{
					if( Convert.ToInt32(dr["id"]) == Convert.ToInt32(Session["cid"]) )
					{
						this.cid.Text = Session["cid"].ToString();				
						
						selected = true;
						
						break;
					}
				}
				
				if( !selected )
					this.cid.Text = Tables["childs"].Rows[0]["id"].ToString();
			}
			else
				this.cid.Text = Tables["childs"].Rows[0]["id"].ToString();
		}
		else
			this.cid.Text = Request.QueryString["id"];
	}
}

private void button_submit_click(object sender, EventArgs e)
{
	this.Fill("Select top 1 * from [b_articles]", "data_articles" );
	
	System.Data.DataRow dr = this.Data.Tables[0].NewRow();

	dr["cid"]       = this.cid.Text;
	dr["title"]     = this.title.Text.Trim();
	dr["forecolor"] = this.forecolor.Text;
	dr["tags"]      = this.tags.Text;
	dr["link"]      = this.link.Text;
	dr["source"]    = this.source.Text;
	dr["addtime"]   = this.addtime.Text;
	dr["state"]     = this.state.Text;
	dr["content"]   = new Common().GetText(this.content.Text);
	dr["hits"]      = 0;

	//上传图片
	HttpPostedFile inputFile = Request.Files[0]; 
	
	if( inputFile.FileName != "" )
	{
		string fileExt  = inputFile.FileName.Substring( inputFile.FileName.LastIndexOf(".") );
		string filename = DateTime.Now.ToString("yyMMddhhmmss") + this.CreateRandomCode(2).ToString() + fileExt;
		
		if( compress.Checked )
			this.Upload( inputFile, "/uploadfiles", filename, Convert.ToInt32(p_width.Text), Convert.ToInt32(p_height.Text) );
		else
			this.Upload( inputFile, "/uploadfiles", filename );

		dr["picture"] = "/uploadfiles/" + filename;
	}
	else
	{
		if( Regex.IsMatch(dr["content"].ToString(), "src=\"[/]?uploadfiles/\\S*\"", RegexOptions.IgnoreCase) )
		{
			dr["picture"] = Regex.Match(dr["content"].ToString(), "src=\"([/]?uploadfiles/\\S*)\"", RegexOptions.IgnoreCase).Groups[1].ToString();
		}
	}
	
	this.Data.Tables[0].Rows.Add( dr );
	
	string articleid = this.Update("data_articles");
	
	Session["cid"] = this.cid.Text;
	
	this.JWrite( "alert('添加成功');window.location.href = './list.aspx?id="+ this.cid.Text +"';" );
}
</script>

<Rajah:Head ID="Head" Level="0" Runat="Server" />
<form runat="server">
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="title3"><div style="float:left">添加内容</div>
      <div style="float:right"></div></td>
  </tr>
  <tr>
    <td style="padding:8px"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td class="table3"><asp:Button Text=" 提交 " OnClick="button_submit_click" runat="server" /></td>
        </tr>
        <tr>
          <td bgcolor="#EFF5FB">
		  <table width="100%" border="0" cellspacing="8" cellpadding="0">
              <tr>
                <td width="60" align="right">分类</td>
                <td><asp:DropDownList ID="cid" runat="server" /></td>
                <td align="right">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td align="right">标题</td>
                <td><asp:TextBox ID="title" CssClass="text2" runat="server" style="width:400px" />
                  <asp:DropDownList ID="forecolor" runat="server">
                    <asp:ListItem Value="" Text="颜色" />
                    <asp:ListItem Value="#000000" style="background-color:#000000" />
                    <asp:ListItem Value="#FFFFFF" style="background-color:#FFFFFF" />
                    <asp:ListItem Value="#008000" style="background-color:#008000" />
                    <asp:ListItem Value="#800000" style="background-color:#800000" />
                    <asp:ListItem Value="#808000" style="background-color:#808000" />
                    <asp:ListItem Value="#000080" style="background-color:#000080" />
                    <asp:ListItem Value="#800080" style="background-color:#800080" />
                    <asp:ListItem Value="#808080" style="background-color:#808080" />
                    <asp:ListItem Value="#FFFF00" style="background-color:#FFFF00" />
                    <asp:ListItem Value="#00FF00" style="background-color:#00FF00" />
                    <asp:ListItem Value="#00FFFF" style="background-color:#00FFFF" />
                    <asp:ListItem Value="#FF00FF" style="background-color:#FF00FF" />
                    <asp:ListItem Value="#FF0000" style="background-color:#FF0000" />
                    <asp:ListItem Value="#0000FF" style="background-color:#0000FF" />
                  <asp:ListItem Value="#008080" style="background-color:#008080" />                  </asp:DropDownList></td>
                <td width="10%" align="right">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td align="right">关键字</td>
                <td colspan="3"><asp:TextBox ID="tags" CssClass="text2" runat="server"  style="width:400px" /></td>
              </tr>
              <tr>
                <td align="right">图片</td>
                <td colspan="3"><asp:TextBox ID="picture" CssClass="text2"  style="width:150px" runat="server" />
                  <input name="file" type="file" class="text2" runat="server" style="width:200px; height:23px;" />&nbsp;<asp:CheckBox ID="compress" Text="自动压缩" Checked="false" runat="server" /> <asp:TextBox ID="p_width" Text="160" CssClass="text2" runat="server" style="width:50px" /> <asp:TextBox ID="p_height" Text="120" CssClass="text2" style="width:50px" runat="server" /></td>
              </tr>
              <tr>
                <td align="right">来源</td>
                <td><asp:TextBox ID="source" CssClass="text2" runat="server"  style="width:120px" /></td>
                <td align="right">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td align="right">时间</td>
                <td><asp:TextBox ID="addtime" CssClass="text2" runat="server"  style="width:120px" />
                  <asp:DropDownList ID="state" runat="server">
                    <asp:ListItem Value="0" Text="状态" />                  
                    <asp:ListItem Value="1" Text="热" />                  
                    <asp:ListItem Value="2" Text="推荐" />                  
                  <asp:ListItem Value="3" Text="置顶" /></asp:DropDownList></td>
                <td align="right">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td align="right">外部连接</td>
                <td colspan="3"><asp:TextBox ID="link" CssClass="text2" runat="server" style="width:400px" />
                  <font color="gray">有外部连接时,点击文章将直接跳转到该地址</font></td>
              </tr>
              <tr>
                <td align="right" valign="top" style="padding-top:29px;">正文</td>
                <td colspan="3">
					<asp:TextBox ID="content" runat="server" style="display:none;"></asp:TextBox>
					<iframe src='../editbox/ewebeditor.htm?id=content&style=coolblue' frameborder='0' scrolling='no' width='100%' height='310'></iframe>				</td>
              </tr>
            </table></td>
        </tr>
        <tr>
          <td class="table3"><asp:Button Text=" 提交 " OnClick="button_submit_click" runat="server" /></td>
        </tr>
      </table></td>
  </tr>
</table>
</form>
<Rajah:Foot ID="Foot" Runat="Server" />