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;
using System.Text.RegularExpressions;

public partial class admin_model_article_list : Fm.Bussiness.BasePage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
       
        StringBuilder where = new StringBuilder();
        where.Append("");
        //按栏目
        int cid = 1;
        if (Request.QueryString["cid"] != null)
        {
            cid = int.Parse(Request.QueryString["cid"].ToString());
            where.Append(" colid=");
            where.Append(cid); 
        }
        //按属性
        if (Request.QueryString["where"] != null)
        {
            where.Append(Request.QueryString["where"].ToString());
        }
        //按关键字搜索
        if (Request.QueryString["kw"] != null)
        {
            where.Append(" title like '%");
            where.Append(Request.QueryString["kw"].ToString());
            where.Append("%'");
        }
        //按时间搜索
        if (Request.QueryString["btime"] != null && Request.QueryString["etime"] != null)
        {
            DateTime btime = DateTime.Parse(Request.QueryString["btime"].ToString());
            DateTime etime = DateTime.Parse(Request.QueryString["etime"].ToString());
            if (etime >= btime)
            {
                where.Append(" writertime between '");
                where.Append(btime);
                where.Append("' and '");
                where.Append(etime);
                where.Append("'");
            }
        }
        Fm.Bussiness.Column cn=new Fm.Bussiness.Column();
        if (!IsPostBack)
        {
            cn.GetColumns(Drop_Col);
            cn.GetColumns(Drop_Cn1);
        }
        //分页绑定
        GetPage(where.ToString());
    }
    void GetPage(string where)
    {
        FormPaging_Rep1.TableName = "Fm_model_main";
        FormPaging_Rep1.ColName = "docid";
        FormPaging_Rep1.Where = where;
        FormPaging_Rep1.Rep = Repeater1;
        FormPaging_Rep1.Order = 1;
    }
   
    protected void Drop_attribute_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect("model_document.aspx?where=" + Drop_attribute.SelectedValue);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     
        Response.Redirect("model_document.aspx?kw=" +keyword.Text);
    }
    //删除所选文档
    protected void delete_Click(object sender, EventArgs e)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        //获取文档ID
        Fm.EntityClass.model_main model = new Fm.EntityClass.model_main();
       // int rows = Repeater1.Items.Count;
        //实例化文档操作类

        Fm.Bussiness.model_main buss = new Fm.Bussiness.model_main();
        //删除内容模型表数据

        foreach (RepeaterItem item in Repeater1.Items)
        {
            //获取选择框
            CheckBox check = item.FindControl("Check_Select") as CheckBox;
            if (check.Checked)
            {
                HiddenField field = item.FindControl("Fielddocid") as HiddenField;
                model.docid = int.Parse(field.Value);
                //删除文档的同时删除静态文档
                Fm.Bussiness.model_htm htm = new Fm.Bussiness.model_htm();
                htm.Document_del(model.docid);
                if (buss.model_main_del(model))
                {
                    if (ps.Queryps("delete from Fm_model_article where docid=" + model.docid + ""))
                    {
                        Response.Redirect("Erro.aspx?eid=该文档信息删除成功!&url=model_document.aspx", false);
                    }
                }
               //Response.Write(model.docid);
            }
        }
    }
    //单个删除
    protected void del_Click(object sender, EventArgs e)
    {
        //获取文档ID
        Fm.EntityClass.model_main model = new Fm.EntityClass.model_main();
        model.docid = int.Parse(((LinkButton)sender).ToolTip);
        //实例化文档操作类

        Fm.Bussiness.model_main buss = new Fm.Bussiness.model_main();
        //删除文档的同时删除静态文档
        Fm.Bussiness.model_htm htm = new Fm.Bussiness.model_htm();
        htm.Document_del(model.docid);
        if (buss.model_main_del(model))
        {
            //删除内容模型表数据
            Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
            if (ps.Queryps("delete from Fm_model_article where docid=" + model.docid + ""))
            {

                Response.Redirect("Erro.aspx?eid=该文档信息删除成功!&url=model_document.aspx", false);
            }
        }
    }
    //编辑文档
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int docid = int.Parse(((LinkButton)sender).ToolTip);
        Response.Redirect("model_article_edit.aspx?docid=" + docid);
        
    }
    //审核
    protected void bt_audit_Click(object sender, EventArgs e)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        int docid = 0;
        foreach (RepeaterItem item in Repeater1.Items)
        {
            //获取选择框
            CheckBox check = item.FindControl("Check_Select") as CheckBox;
            if (check.Checked)
            {
                HiddenField field = item.FindControl("Fielddocid") as HiddenField;
                docid = int.Parse(field.Value);
                ps.Queryps("update Fm_model_main set audit=1 where docid="+docid+"");
            }
        }
        Response.Redirect("Erro.aspx?eid=所选文档审核成功!&url=model_document.aspx", false);
    }
    //项绑定时启用
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        //获取绑定数据的栏目ID
        int cid = int.Parse((e.Item.DataItem as DataRowView)["colid"].ToString());
        Label lab = e.Item.FindControl("Lab_cname") as Label;
        lab.Text = ps.Getps("Fm_column","cl_name as cname","cl_id="+cid)[0]["cname"].ToString();
        //绑定预览地址
        int docid = int.Parse((e.Item.DataItem as DataRowView)["docid"].ToString());
        //查找控件
        HyperLink link = e.Item.FindControl("proview") as HyperLink; ;
        DataView view = ps.Getps("Fm_model_article_view","path,html","docid="+docid);
        if (bool.Parse(view[0]["html"].ToString()) == true)
        {
            link.NavigateUrl = view[0]["path"].ToString();
        }
        else {
            link.NavigateUrl = "/article.aspx?docid="+docid;
        }

    }
    //按日期查询
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("model_document.aspx?btime="+txt_time1.Value+"&etime="+txt_time2.Value);
    }
 
    protected void Button3_Click1(object sender, EventArgs e)
    {
        //查找DropDownList控件
        Response.Redirect("model_document.aspx?cid=" + Drop_Cn1.SelectedValue);
    }
    //文档移动
    protected void Bt_ColUp_Click(object sender, EventArgs e)
    {
        //查找DropDownList控件
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        DataView dv = ps.Getps("Fm_column","cl_id,cl_attribute","cl_id="+Drop_Col.SelectedValue);
        int att=int.Parse(dv[0]["cl_attribute"].ToString());
        if (att == 1 || att == 2)
        {
            Response.Write("<script>alert('封面栏目或者外部链接栏目不允许移动!');</script>");
        }
        else
        {
            int docid = 0;
            foreach (RepeaterItem item in Repeater1.Items)
            {
                //获取选择框
                CheckBox check = item.FindControl("Check_Select") as CheckBox;
                if (check.Checked)
                {
                    HiddenField field = item.FindControl("Fielddocid") as HiddenField;
                    docid = int.Parse(field.Value);
                    ps.Queryps("update Fm_model_main set colid=" + Drop_Col.SelectedValue + " where docid=" + docid + "");
                }
            }
            Response.Redirect("Erro.aspx?eid=所选文档已成功移动到指定的栏目!&url=model_document.aspx", false);
        }
    }
    //静态生成
    protected void Button4_Click(object sender, EventArgs e)
    {
        Fm.Bussiness.public_select ps = new Fm.Bussiness.public_select();
        Fm.Tags.HtmlWrite html = new Fm.Tags.HtmlWrite();
        int docid = 0;
        foreach (RepeaterItem item in Repeater1.Items)
        {
            //获取选择框
            CheckBox check = item.FindControl("Check_Select") as CheckBox;
            if (check.Checked)
            {
                HiddenField field = item.FindControl("Fielddocid") as HiddenField;
                docid = int.Parse(field.Value);
                //更改文档HTML状态
                ps.Queryps("update Fm_model_main set html=1 where docid=" + docid + "");
                //生成静态
                html.WriteContent(docid);
            }
        }
        Response.Redirect("Erro.aspx?eid=所选文章静态生成成功!&url=model_document.aspx", false);
    }
    //修改属性
    protected void Button5_Click(object sender, EventArgs e)
    {
        StringBuilder str = new StringBuilder();
        foreach (RepeaterItem item in Repeater1.Items)
        {
            //获取选择框
            CheckBox check = item.FindControl("Check_Select") as CheckBox;
            if (check.Checked)
            {
                HiddenField field = item.FindControl("Fielddocid") as HiddenField;
                //docid = int.Parse();
                //把文档添加到
                str.Append(field.Value);
                str.Append(",");
            }
        }
        Response.Redirect("model_document_attribute.aspx?items="+str.ToString(), false);
    }
}
