using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
namespace Fm.Bussiness
{
    public class Column:Page
    {
        public bool Doc(int cid)
        {
            public_select ps = new public_select();
            DataView dv = ps.Getps("Fm_column", "cl_id,cl_attribute,cl_tg", "cl_id=" + cid);

            //bool bo=true;
            int att = int.Parse(dv[0]["cl_attribute"].ToString());
            Boolean tg = Boolean.Parse(dv[0]["cl_tg"].ToString().ToLower());

            if (att == 2 || att == 1 || tg==false)
            {
                return false;
            }
            else
            {
                return true;
            }

        }
        /// <summary>
        /// 栏目删除操作
        /// </summary>
        /// <param name="cid"></param>
        public void Delete(int cid)
        {
            sys_column cl = new sys_column();//实例化栏目操作类
            public_select ps = new public_select();//实例化公共查询类
            //获取删除链接按钮类
            //LinkButton link = sender as LinkButton;
            //查询出相应的子类
            DataView da = ps.Getps("Fm_column", "cl_parent,cl_path,cl_attribute", "cl_id=" + cid + "");
            DataView da1 = ps.Getps("Fm_column", "cl_id,cl_path,cl_attribute", "cl_parent like '" + da[0]["cl_parent"].ToString() + "%'");

            //删除栏目的同时删除该栏目保存目录下的所有文件
            string path = Server.MapPath("../");
            if (int.Parse(da[0]["cl_attribute"].ToString()) != 2)
            {
                DirectoryInfo df = new DirectoryInfo(path + da[0]["cl_path"].ToString());
                if (df.Exists)
                {
                    df.Delete(true);
                }
            }
            Fm.EntityClass.Column col = new Fm.EntityClass.Column();

            //循环删除该栏目以及子栏目
            for (int i = 0; i < da1.Count; i++)
            {
                col.cl_id = int.Parse(da1[i]["cl_id"].ToString());
                if (!cl.Column_del(col))
                {
                    if (int.Parse(da1[i]["cl_attribute"].ToString()) != 2)
                    {
                        //删除栏目的同时删除该栏目保存目录下的所有文件
                        DirectoryInfo df1 = new DirectoryInfo(path + da1[i]["cl_path"].ToString());
                        if (df1.Exists)
                        {
                            df1.Delete(true);
                        }
                    }
                }

            }
        }

      
/**********************************返回下拉列表******************************************/
        //返回下拉列表框类
        public ListControl GetColumns(ListControl Controls)
        {
            //ListItem[] item = null;
            //item.SetValue();
           // ListControl items = null;
            ColumnBind(Controls);
            return Controls;
            

        }
        //一级栏目绑定
        void ColumnBind(ListControl items)
        {
            //DropDownList drop = new DropDownList();
            //获取栏目信息
            public_select ps = new public_select();
            DataView dv = ps.Getps("Fm_column", "cl_id,cl_name,cl_parent,cl_attribute,cl_options", "");
            //循环一级栏目
            foreach (DataRow dr in dv.Table.Rows)
            {
                if (dr["cl_parent"].ToString().Length == 2)
                {
                    //把数据存入新行中
                    //DataRow row = cn.NewRow();
                    //row["cid"] = 
                   
                    //row["cname"] = 
                    //cn.Rows.Add(row);
                    ListItem item = new ListItem();
                    item.Text=dr["cl_name"].ToString();
                    item.Value = dr["cl_id"].ToString();
                    //判断是否为外部链接,封面栏目
                    int att = int.Parse(dr["cl_attribute"].ToString());
                    if (att == 2)
                    {
                        item.Attributes.Add("style", "color:red");
                    }
                    else if (att == 1)
                    {
                        item.Attributes.Add("style", "color:green");
                    }
                    items.Items.Add(item);
                    //调用子栏目绑定方法
                    SubColumnBind(dv, dr["cl_parent"].ToString(), items);
                }

            }
        }
        //子栏目绑定
        void SubColumnBind(DataView dv, string parent, ListControl items)
        {
            foreach (DataRow dr in dv.Table.Rows)
            {
                if (dr["cl_parent"].ToString().Length == parent.Length + 2 && dr["cl_parent"].ToString().Substring(0, parent.Length) == parent)
                {

                    //栏目区分符
                    string nbsp = "";
                    for (int i = 1; i <= dr["cl_parent"].ToString().Length / 2; i++)
                    {
                        nbsp += Server.HtmlDecode("&nbsp;").ToString();
                    }
                    //把子栏目数据存入新行中
                    ListItem item = new ListItem();
                    item.Text = nbsp + "├" + dr["cl_name"].ToString(); ;
                    item.Value = dr["cl_id"].ToString();
                    //判断是否为外部链接,封面栏目
                    int att=int.Parse(dr["cl_attribute"].ToString());
                    if ( att== 2)
                    {
                        item.Attributes.Add("style","color:red");
                    }
                    else if(att==1)
                    {
                        item.Attributes.Add("style", "color:green");
                    }
                    items.Items.Add(item);
                    //递归调用
                    SubColumnBind(dv, dr["cl_parent"].ToString(), items);

                }

            }
        }
    }
}
