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 Fm.Bussiness;

public partial class Admin_art_class_add : BasePage
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
            if (!IsPostBack)
            {
                public_select ps = new public_select();
                int cid = 0;
                if (Request.QueryString["cl_id"] != null)
                {
                     cid= int.Parse(Request.QueryString["cl_id"].ToString());
                }

                
                //自动生成栏目ID
                colid.Text = GetId().ToString();

                parentpath.Text = ps.Getps("Fm_webset", "html", "")[0]["html"].ToString();

                //绑定默认模板
                Xml_work xml = new Xml_work();
                DataTable dt = xml.Xml_Reader(Server.MapPath("../")+"config/templates.xml","templates");
                txt_temp_index.Text = dt.Rows[0]["default"].ToString();
                txt_temp_list.Text = dt.Rows[0]["list"].ToString();
                txt_temp_cont.Text = dt.Rows[0]["article"].ToString();

                //绑定栏目
                ColumnBind();

                foreach (ListItem item in Drop_Column.Items)
                {
                    if (int.Parse(item.Value) == cid)
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        
      
    }
    //绑定栏目的方法
    void ColumnBind()
    {
        //绑定栏目信息
        Column cn = new Column();
        cn.GetColumns(Drop_Column);

        //添加第一个栏目
        ListItem item = new ListItem();
        item.Text = "无上级栏目";
        item.Value = "0";

        Drop_Column.Items.Insert(0, item);
    }
    //判断是否已有的方法
    void GetParent(string parent)
    {
        public_select ps = new public_select();
        //调用生成方法
        string random=GetRandom();
        string sqlstr = "";
        //查询表中已有的一级栏目区分编号，用来判断新产生的编号是否已经存在
        if (parent.Equals(""))
        {
            sqlstr = "cl_parent='" + random + "'";
        }
        else
        {
            sqlstr = "cl_parent='" + parent + random + "'";
        }
        DataView dw = ps.Getps("Fm_column", "cl_parent", sqlstr);
        if (dw.Count > 0)
        {
            GetParent(parent);
        }
        else
        {
            ViewState["code"] = parent + random;
        }
        
    }
    //生成区分编号的方法
    string GetRandom()
    {
        string str = new Random().Next(1,99).ToString();
        if (str.Length == 1)
        {
            str ="0"+str;

        }
        return str;
    }
    //类别添加
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Fm.EntityClass.Column col = new Fm.EntityClass.Column();
        col.cl_id = int.Parse(colid.Text.Trim());
        col.cl_name = cl_name.Text;
       
        col.cl_tg = bool.Parse(Radio_tg.SelectedValue);
        col.cl_xs = bool.Parse(Radio_xs.SelectedValue);
        if (Radio_location.SelectedValue == "true")
        {
            col.cl_path = parentpath.Text + cl_path.Text;
        }
        else
        {
            col.cl_path = cl_path.Text;
        }
        col.cl_location = bool.Parse(Radio_location.SelectedValue);
        col.cl_attribute = int.Parse(Radio_attribute.SelectedValue);
        col.cl_options = int.Parse(Radio_options.SelectedValue);

        col.cl_temp_index = txt_temp_index.Text;
        col.cl_temp_list = txt_temp_list.Text;
        col.cl_temp_cont = txt_temp_cont.Text;

        col.cl_list = txt_list.Text;
        col.cl_content = txt_content.Text;
        col.cl_default = def.Text;

        col.cl_url = cl_url.Text;
        col.cl_keyword = cl_keyword.Text;
        col.cl_intro = cl_intro.Text;
        //判断是否选择了上级栏目
        if (Drop_Column.SelectedValue == "0")
        {
            GetParent("");
        }
        else
        {
            public_select ps = new public_select();
            GetParent(ps.Getps("Fm_column", "cl_parent", "cl_id=" + Drop_Column.SelectedValue)[0]["cl_parent"].ToString());
        }
        col.cl_parent = ViewState["code"].ToString();
        col.cl_px = int.Parse(cl_px.Text);

        sys_column sys = new sys_column();
        if (sys.Column_add(col))
        {
            if (Radio_attribute.SelectedValue != "2")
            {
                if (Radio_options.SelectedValue != "2")
                {
                    ATL.IO.IOStream stream = new ATL.IO.IOStream();
                    stream.CreateFolder(Server.MapPath("../") + col.cl_path);
                }
            }
            Response.Redirect("Erro.aspx?eid=新栏目添加成功!&url=art_column_add.aspx", false);
        }
        else
        {
            Response.Redirect("Erro.aspx?eid=新栏目添加失败!&url=art_column_add.aspx", false);
        }
    }
    //处理自动生成ID
    protected void Radio_id_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Radio_id.SelectedValue == "0")
        {
            colid.Enabled = false;
            colid.Text = GetId().ToString();
        }
        else
        {
            colid.Enabled = true;
        }
    }
    //获取栏目ID
    int GetId()
    {
        public_select ps = new public_select();
        DataView dv = ps.Getps("Fm_column", "max(cl_id) as amount,count(cl_id) as num", "");

        DataTable dt;
        int maxid = 1;
        if (dv.Count > 0)
        {
            if (int.Parse(dv[0]["num"].ToString()) > 0)
            {
                maxid = int.Parse(dv[0]["amount"].ToString());
            }
        }
        int colid=0;
        for (int i = 1; i <= maxid+1; i++)
        {
            
            dt = ps.Getps("Fm_column", "cl_id", "cl_id="+i+"").Table;
            if (dt.Rows.Count > 0)
            {
                continue;
            }
            else
            {
                colid = i;
                break;
            }
        }

        return colid;

    }
}
