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.Data.SqlClient;
using System.Xml;
using System.IO;


public partial class TaksCategory : System.Web.UI.Page
{
    static int SelLay;
    public static string[] NowID = new string[6];
    static string GroupID;

    public void NotShow()
    {
        AddCategoryPanel.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = false;
        Panel1.Visible = false;
    }

    public void rfDB(string id)
    {
        object RD;
        //RD = wtoDatabase.DBSelect("select * from LTGroup where pid=" + id + " order by n");
        RD = CDatabase.DBSelect("select g.id as id,g.info as info,g.minmoney as minmoney,g.Sort as Sort,g.category as category,g.pic as pic,(select Count(*) from TaksCategory a where a.pid=g.id) as snum from TaksCategory g where g.pid=" + id + "  Group by g.id,g.info,g.sort,g.minmoney,g.category,g.pic order by g.sort");
        GridView1.DataSource = RD;
        DataBind();
        if (SelLay == 0)
        {
            

        }
        else
        {
            

        }
        CDatabase.CloseConn();
      
    }
    public void reDB(int Lay)
    {
        
        rfDB(NowID[Lay]);
        if (Lay < 1) { N1.Visible = false; }
        if (Lay < 2) { N2.Visible = false; }
        if (Lay < 3) { N3.Visible = false; }
        if (Lay < 4) { N4.Visible = false; }
        if (Lay < 5) { N5.Visible = false; }


     }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["WtoRightName"] == null)
        {
            Response.Redirect("~/SYSADMIN/main/error.aspx?err=非法登陆!或系统超时，请刷新后重新登录");
        }

        if (IsPostBack)
        {

            /*
            string UserName = Request.Form["UserName"];
            if (wtodo.chkuser(UserName))
            {
                this.checkuser.IsValid = false;
            }
            else
            {
                this.checkuser.IsValid = true;
            }*/
        }
        else
        {
            
            SelLay = 0;
            NowID[0] = "0";
            rfDB("0");
            //Message.Text = NowID[0];


            
        }
        

    }


    protected void N0_Click(object sender, EventArgs e)
    {
        SelLay = 0;
        reDB(SelLay);
    }
    protected void N1_Click(object sender, EventArgs e)
    {
        SelLay = 1;
        reDB(SelLay);

        //Message.Text = Convert.ToString(SelLay);
    }
    protected void N2_Click(object sender, EventArgs e)
    {
        SelLay = 2;
        reDB(SelLay);       
    }
    protected void N3_Click(object sender, EventArgs e)
    {
        SelLay = 3;
        reDB(SelLay);
    }
    protected void N4_Click(object sender, EventArgs e)
    {
        SelLay = 4;
        reDB(SelLay);
    }
    protected void N5_Click(object sender, EventArgs e)
    {
        SelLay = 5;
        reDB(SelLay);
    }

    protected string ReadPICURL(string pic)
    {
        Task.FUNC t = new Task.FUNC();

        if (pic == "")
        {
            return t.filepath + "null.gif";
        }
        else
        {
            return t.filepath + pic;
        }

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        Panel2.Visible = true;
        string id=GridView1.Rows[e.NewEditIndex].Cells[0].Text;
        /*
        ESort.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
        ECategory.Text = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
        EInfo.Text = GridView1.Rows[e.NewEditIndex].Cells[4].Text;
        ESort.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
        ESort.Text = GridView1.Rows[e.NewEditIndex].Cells[1].Text;
         */


        string[] rd=new string[6];
        EID.Text = id;
        rd=CDatabase.DBReader("select id,sort,category,info,minMoney,pic from TaksCategory where id=" + id, 6);
        ESort.Text = rd[1];
        Ecategory.Text = rd[2];
        EInfo.Text = rd[3];
        EMinMoney.Text = rd[4];
        EPIC.Text = rd[5];




        /*
        string id;

        id =  GridView1.Rows[0].Cells[0].Text;
         
        object RD;
        RD = wtoDatabase.DBSelect("select * from LTGroup where id=" + id + "");
        DetailsView1.DataSource = RD;
        DataBind();
        */
       // Message.Text = Convert.ToString(e.NewSelectedIndex);
    }

   

    //====================移动组
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.Visible = false;
        Panel1.Visible = true;
        
        object RD;

        RD = CDatabase.DBSelect("select * from TaksCategory where pid=0 and id<>" + EID.Text + " order by sort");
        DDL1.DataSource = RD;
        DDL1.DataTextField = "Category";
        DDL1.DataValueField = "id";
        DataBind();
        DDL1.Items.Insert(0, new ListItem("主类别", "0"));
        GroupID = "0";
    }

    public void ChangeDDL(DropDownList SDDL,DropDownList EDDL)
    {

        /*    object RD;
            EDDL.Visible = true;
            //Message.Text = DDL1.Text;
            RD = CDatabase.DBSelect("select * from TaksCategory where pid=" + SDDL.Text + " and id<>" + EID.Text + " order by sort");
            EDDL.DataSource = RD;
            EDDL.DataTextField = "Category";
            EDDL.DataValueField = "id";
            DataBind();
         * */
        GroupID = SDDL.Text;


            EDDL.Items.Insert(0, new ListItem("本组", "0"));
            


    }
    protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL1.Text != "0")
        {
           /* object RD;
            DDL2.Visible = true;
            //Message.Text = DDL1.Text;
            RD = wtoDatabase.DBSelect("select * from LTGroup where pid=" + DDL1.Text + " order by n");
            DDL2.DataSource = RD;
            DDL2.DataTextField = "GroupName";
            DDL2.DataValueField = "id";
            DataBind();
            GroupID = DDL1.Text;
            DDL2.Items.Insert(0, new ListItem("本组", "0"));
            */
            ChangeDDL(DDL1, DDL2);
            DDL3.Visible = false; DDL4.Visible = false; DDL5.Visible = false;
        }
        else
        {
            GroupID = "0";
            DDL2.Visible = false; DDL3.Visible = false; DDL4.Visible = false; DDL5.Visible = false;

        }
    }
    protected void DDL2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL2.Text != "0")
        {
            ChangeDDL(DDL2, DDL3);
        }
        else
        {
            GroupID = DDL1.Text;
            DDL3.Visible = false; DDL4.Visible = false; DDL5.Visible = false;

        }
    }
    protected void DDL3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL3.Text != "0")
        {
            ChangeDDL(DDL3, DDL4);
        }
        else
        {
            GroupID = DDL2.Text;
            DDL4.Visible = false; DDL5.Visible = false;

        }
    }
    protected void DDL4_SelectedIndexChanged(object sender, EventArgs e)
    {
        /*
        if (DDL4.Text != "0")
        {
            ChangeDDL(DDL4, DDL5);
        }
        else
        {
            GroupID = DDL3.Text;
            DDL4.Visible = false; DDL5.Visible = false;

        }*/
        if (DDL4.Text != "0")
        {
            GroupID = DDL4.Text;
        }
        else
        {
            GroupID = DDL3.Text;
            DDL4.Visible = false; DDL5.Visible = false;

        }
    }
    protected void DDL5_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        NotShow();
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        string sqlstr = "update TaksCategory set pid=" + GroupID + " where id=" + EID.Text;
        CDatabase.DBUpdate(sqlstr);
        LinkButton4_Click(sender, e);
    }

    
    
    protected void AddCategoryKey_Click(object sender, EventArgs e)
    {
        Task.FUNC f = new Task.FUNC();
        string filename="";

        if (FileUpload2.HasFile)
        {

            WEB.FUNC w = new WEB.FUNC();

            filename = "T" + w.FileName(FileUpload2);
            FileUpload2.SaveAs(Server.MapPath(f.filepath + filename));
           
        }        
        
        CDatabase.DBUpdate("insert into TaksCategory (pid,sort,category,pic) values (" + NowID[SelLay] + "," + Sort.Text + ",'" + Category.Text + "','"+filename+"')");
        AddCategoryPanel.Visible = false;
        rfDB(NowID[SelLay]);
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {

            e.Row.Cells[0].Visible = false;

        }  
    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

        if (SelLay < 1)
        {
            string id, str;

            NotShow();

            id = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;

            str = "> " + GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;

            SelLay++;
            NowID[SelLay] = id;//记录父ＩＤ

            switch (SelLay)
            {
                case 0: break;
                case 1: N1.Visible = true; N1.Text = str; break;
                case 2: N2.Visible = true; N2.Text = str; break;
                case 3: N3.Visible = true; N3.Text = str; break;
                case 4: N4.Visible = true; N4.Text = str; break;
                case 5: N5.Visible = true; N5.Text = str; break;
                //default: Console.WriteLine("2"); break;
            }
            rfDB(id);
            //Message.Text = SelLay.ToString();

            //((Panel)GridView1.FindControl("Panel3")).Visible = true; 
            // LinkButton7.Visible = false;

        }
        else
        {
            //GridView1_RowEditing(sender, e);
            rfDB(NowID[SelLay]);

        }
        Message.Text = SelLay.ToString();

        
    }
    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        NotShow();
        AddCategoryPanel.Visible = true;
        
       
    }
    protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {

    }
    protected void LinkButton6_Click1(object sender, EventArgs e)
    {
        NotShow();
    }
    protected void EditCategoryKey_Click(object sender, EventArgs e)
    {
        Task.FUNC f = new Task.FUNC();
        if (DELPIC.Checked == true)
        {
            string of = Server.MapPath(f.filepath + EPIC.Text);
                if (File.Exists(of))
                {
                    File.Delete(of);
                }
                EPIC.Text = "";
                DELPIC.Checked = false;
        }
        if (FileUpload1.HasFile)
        {

            if (EPIC.Text != "")
            {
                string of = Server.MapPath(f.filepath + EPIC.Text);

                if (File.Exists(of))
                {
                    File.Delete(of);
                }
            }

            WEB.FUNC w = new WEB.FUNC();

            string filename = "T"+w.FileName(FileUpload1);
            FileUpload1.SaveAs(Server.MapPath(f.filepath+filename));
            EPIC.Text = filename;
        }



        CDatabase.DBUpdate("update TaksCategory set sort="+ESort.Text+",Category='"+Ecategory.Text+"',info='"+EInfo.Text+"',minMoney="+EMinMoney.Text+",pic='"+EPIC.Text+"' where id="+EID.Text);
        
        
        NotShow();
        rfDB(NowID[SelLay]);
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        NotShow();
        string id;
        id = GridView1.Rows[e.RowIndex].Cells[0].Text;
        
        string[] RD=new string[1];
        string[] RD2 = new string[1];

        RD = CDatabase.DBReader("select Count(*) as CN from TaksCategory where pid=" + id,1);
        RD2 = CDatabase.DBReader("select Count(*) as CN from TaksInfo where CID=" + id, 1);
        Panel3.Visible = true;
        Label1.Text = GridView1.Rows[e.RowIndex].Cells[3].Text;
        if (RD[0]=="0" && RD2[0]=="0")
        {
            
            DelID.Text = id;
            
            LinkButton1.Visible = true;
        }
        else
        {
            LinkButton1.Enabled = false;
            DELMessage.Text= "此组有子分组或数据，所以不能删除！";
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        DELMessage.Text = "";
        Panel3.Visible = false;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string fi = CDatabase.DBReadData("select pic from TaksCategory where id=" + DelID.Text);
        
        Task.FUNC f = new Task.FUNC();
        string of = Server.MapPath(f.filepath + fi);
            if (File.Exists(of))
            {
                File.Delete(of);
            }
           

        CDatabase.DBUpdate("delete from TaksCategory where id=" + DelID.Text);
        rfDB(NowID[SelLay]);
        NotShow();
    }
    protected void LinkButton8_Click(object sender, EventArgs e)
    {

    }
}
