using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using Fm.Tags;
using System.IO;

public partial class admin_model_article_edit : Fm.Bussiness.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //获取栏目值
            Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
            //绑定栏目的方法
            ColumnBind();
            //作者
            Drop_author.DataSource = ps.Getps("Fm_author", "*", "");
            Drop_author.DataTextField = "au_name";
            Drop_author.DataValueField = "au_name";
            Drop_author.DataBind();
            //来源
            Drop_source.DataSource = ps.Getps("Fm_source", "*", "");
            Drop_source.DataTextField = "so_name";
            Drop_source.DataValueField = "so_name";
            Drop_source.DataBind();

            Bind(ps);
        }
    }
    //绑定栏目的方法
    void ColumnBind()
    {
        //绑定栏目信息
        Fm.Bussiness.Column cn = new Fm.Bussiness.Column();
        cn.GetColumns(Drop_column);

        //添加第一个栏目
        ListItem item = new ListItem();
        item.Text = "请选择栏目";
        item.Value = "0";

        Drop_column.Items.Insert(0, item);
    }
    protected void bt_save_Click(object sender, ImageClickEventArgs e)
    {
        //模型基表数据处理
        Fm.EntityClass.model_main model = new Fm.EntityClass.model_main();
        model.docid = int.Parse(Request.QueryString["docid"].ToString());
        model.title = doctitle.Text;
        if (head.Checked)
        {
            model.head = true;
        }
        else
        {
            model.head = false;
        }
        if (map.Checked)
        { model.map = true; }
        else
        { model.map = false; }
        if (flash.Checked)
        { model.flash = true; }
        else
        { model.flash = false; }
        if (commend.Checked)
        { model.commend = true; }
        else
        {
            model.commend = false;
        }
     
        model.bold = false;
        model.colid = int.Parse(Drop_column.SelectedValue);
        //model.mid = 1;
        model.thumbnail = thumbnail.Value;
        model.writertime = DateTime.Now;
      
        model.username =username.Value;
        model.click = int.Parse(click.Text);
        model.audit = true;
        if (comment.Checked)
        { model.comment = true; }
        else
        {
            model.comment = false;
        }
        model.color = getcolor.Text;
        model.keyword = keywords.Text;
        model.intro = intro.Text;
        if (html_true.Checked)
        {
            model.html = true;
        }
        if (html_false.Checked)
        {
            model.html = false;
        }
        model.topclick = int.Parse(ViewState["topclick"].ToString());
        model.tread = int.Parse(ViewState["tread"].ToString());
        //主表数据的添加
        Fm.Bussiness.model_main buss = new Fm.Bussiness.model_main();
        buss.model_main_update(model);
        //Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        //内容处理
        Fm.EntityClass.model_article article = new Fm.EntityClass.model_article();
        article.docid = model.docid;
        article.content = FCKeditor1.Value;
        article.author = author.Text;
        article.source = source.Text;
        if (buss.model_article_update(article))
        {
            //判断是否选择了静态生成
            if (html_true.Checked == true)
            {
                //实例化静态文档写入类
                Fm.Tags.HtmlWrite html = new Fm.Tags.HtmlWrite();
                html.WriteContent(model.docid);
            }
            Response.Redirect("Erro.aspx?eid=文章信息修改成功!!&url=model_document.aspx");
        }
    }
    /// <summary>
    /// 初始化修改文档内容
    /// </summary>
    /// <param name="ps"></param>
    void Bind(Fm.Bussiness.public_select ps)
    {
        //查询文章信息
        //查询主表信息
        int docid=int.Parse(Request.QueryString["docid"].ToString());
        DataView dv = ps.Getps("Fm_model_main","*","docid="+docid+"");
        foreach (DataRow row in dv.Table.Rows)
        {
            username.Value = row["username"].ToString();
            doctitle.Text = row["title"].ToString();
            //head.Checked = ((bool)(row["head"].ToString().ToLower()));
            if (row["head"].ToString().ToLower() == "true")
            {
                head.Checked = true;
            }
            if (row["map"].ToString().ToLower() == "true")
            {
                map.Checked = true;
            }
            if(row["commend"].ToString().ToLower()=="true")
            {
                commend.Checked=true;
            }
            //if(row["bold"].ToString().ToLower()=="true")
            //{
            //    bold.Checked=true;
            //}
            if(row["flash"].ToString().ToLower()=="true")
            {
                flash.Checked=true;
            }
            if (row["html"].ToString().ToLower() == "true")
            {
                html_true.Checked = true;
            }
            else
            {
                html_false.Checked = true;
            }
            foreach (ListItem item in Drop_column.Items)
            {
                if (row["colid"].ToString() == item.Value)
                {
                    item.Selected = true;
                }
            }

            thumbnail.Value = row["thumbnail"].ToString();

            //comment.Checked = (bool)(row["comment"].ToString().ToLower());
            if (row["comment"].ToString().ToLower() == "true")
            {
                comment.Checked = true;
            }
            else
            {
                comment.Checked = false;
            }
            click.Text = row["click"].ToString();
            keywords.Text = row["keyword"].ToString();
            intro.Text = row["intro"].ToString();
            getcolor.Text = row["color"].ToString();

            //视图存储被顶和被踩次数
            ViewState["topclick"] = row["topclick"].ToString();
            ViewState["tread"] = row["tread"].ToString();

        }
        //查询内容模型的数据
        DataView dw = ps.Getps("Fm_model_article","*","docid="+docid+"");
        foreach (DataRow row in dw.Table.Rows)
        {
            author.Text = row["author"].ToString();
            source.Text = row["source"].ToString();
            FCKeditor1.Value = row["content"].ToString();
        }
    }
 
}
