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;

    static string TaksAccAtt;
    static int TaksAccNum;
    static long TaksAccMaxSize;
    static double TaksMinMoney = 100;

    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");
        string str1 = "select c.category as category from TaksCategory c where c.id=g.cid";
        string str2 = "select Count(*) from TaksPlan a where a.tid=g.id";
        string str = "select g.id as id,g.subject as subject,g.money as money,g.cid as cid,g.sdt as sdt,g.edt as edt,g.isopen as isopen,g.finish as finish,("+str2+") as pnum,("+str1+") as category from TaksInfo g  Group by g.id,g.subject,g.money,g.cid,g.sdt,g.edt,g.finish,g.isopen order by g.sdt desc";
        
        
        RD = CDatabase.DBFillSelect(str);
        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; }


     }


    public string ISOPEN(string isopen,string ty)
    {

        string io = "";

        if (ty == "Text")
        {
            if (isopen == "true")
            {
                io = "禁止此任务";
            }
            else
            {
                io = "开通此任务";
            }
        }
        if (ty == "Key")
        {
            if (isopen == "true")
            {
                io = "IsOpenFalse";
            }
            else
            {
                io = "IsOpenTrue";
            }

        }
        if (ty == "Value")
        {
            io = isopen;
        }
        return io;
    }


    public string FINISH(string finish, string ty)
    {

        string io = "";

        if (ty == "Text")
        {
            if (finish == "true")
            {
                io = "设为进行中";
            }
            else
            {
                io = "设为完成";
            }
        }
        if (ty == "Key")
        {
            if (finish == "true")
            {
                io = "FinishFalse";
            }
            else
            {
                io = "FinishTrue";
            }

        }
        if (ty == "Value")
        {
            io = finish;
        }
        return io;
    }

    public string STA(string isopen, string finish)
    {
        string ur = @"../image/task/";
        string sta = "";
        if (isopen == "false")
        {
            sta = "stop.gif";
        }
        else
        {
            if (finish == "true")
            {
                sta = "finish.gif";

            }
            else
            {
                sta = "ing.gif";
            }
        }
        return ur + sta;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

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

        if (IsPostBack)
        {
            string[] bi = new string[5];
            bi = CDatabase.DBReader("select taksaccatt,taksaccnum,taksaccmaxsize from systembasicinformation", 3);
            TaksAccAtt = bi[0];
            TaksAccNum = Convert.ToInt16(bi[1]);
            TaksAccMaxSize = Convert.ToInt64(bi[2]);

            //TaksMinMoney = Convert.ToDouble(CDatabase.DBReadData("select minmoney from takscategory where id=" + AddTaksCategoryID.Text));
            //TaksMinMoneyText.Text = "最低金额为" + TaksMinMoney.ToString() + "元";
            UploadMaxFileText.Text = "最多可上传" + TaksAccNum.ToString() + "个文件";

            /*
            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;
        }

    }

    public string FilePath(string file)
    {
        Task.FUNC f = new Task.FUNC();
        return f.filepath + file;
    }
    protected void readfilelist(string tid)
    {
        object RD = CDatabase.DBSelect("select * from TaksAcc where linkID=" + tid + " and type='Taks' order by sort");
        EGridView1.DataSource = RD;
        EGridView1.DataBind();
        CDatabase.CloseConn();

    }

    //编辑资料
    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;
        
        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;
         */
        ECategory.Text = GridView1.Rows[e.NewEditIndex].Cells[3].Text;

        string[] rd=new string[6];
        EID.Text = id;

        rd=CDatabase.DBReader("select cid,money,info,edt,userid from TaksInfo where id=" + id,5);


        ESubject.Text = GridView1.Rows[e.NewEditIndex].Cells[2].Text;
        Content.Text = rd[2];
        EMoney.Text = rd[1];
        //EEDT.Text=rd[3];
        EUID.Text = rd[4];
        //EPIC.Text = rd[5];
        //EEDT.SelectedDate = Convert.ToDateTime(rd[3]);
        EEDT.Text = rd[3];
        readfilelist(id);


        if (Convert.ToUInt16(CDatabase.DBReadData("select count(*) from taksacc where linkid=" + id + " and type='Taks'")) >= TaksAccNum)
        {
            UploadFilePanel.Visible = false;
        }
        /*
        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)
    {

       GroupID = SDDL.Text;
       EDDL.Items.Insert(0, new ListItem("本组", "0"));
    }
    protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL1.Text != "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")
        {
            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 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 FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {

    }

    protected void EditCategoryKey_Click(object sender, EventArgs e)
    {
        Task.FUNC f = new Task.FUNC();

        /*
        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 TaksPlan where TID=" + id, 1);
        Panel3.Visible = true;
        Label1.Text = GridView1.Rows[e.RowIndex].Cells[2].Text;
        if (RD2[0]=="0")
        {
            
            DelID.Text = id;

            LinkButton1.Enabled = 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 file;
        string sqlstr;
        sqlstr = "select filename from taksacc where linkid=" + DelID.Text + " and type='Taks'";

        SqlConnection SQLconn = CDatabase.wtoconn();
        SqlDataReader RD;
        SQLconn.Open();
        SqlCommand scd = new SqlCommand(sqlstr, SQLconn);
        RD = scd.ExecuteReader();
        while(RD.Read())
        {
            file = RD[0].ToString();
            
                string of = Server.MapPath(f.filepath + file);
                try
                {
                    if (File.Exists(of))
                    {
                        File.Delete(of);
                    }
                }
                catch { }
            
        }
        RD.Close();
        SQLconn.Close();


        CDatabase.DBUpdate("delete from TaksAcc where linkid=" + DelID.Text + " and type='Taks'");
        CDatabase.DBUpdate("delete from TaksInfo where id=" + DelID.Text);
        
        NotShow();
        rfDB("0");

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

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //Message.Text = e.CommandName;



        if (e.CommandName == "IsOpenTrue")
        {
            //Message.Text = "开启";
           CDatabase.DBUpdate("update taksinfo set isopen='true' where id="+e.CommandArgument);
        }
        if (e.CommandName == "IsOpenFalse")
        {
            //Message.Text = "关闭";
            CDatabase.DBUpdate("update taksinfo set isopen='false' where id=" + e.CommandArgument);
        }


        //================
        if (e.CommandName == "FinishTrue")
        {
            //Message.Text = "开启";
            CDatabase.DBUpdate("update taksinfo set finish='true' where id=" + e.CommandArgument);
        }
        if (e.CommandName == "FinishFalse")
        {
            //Message.Text = "关闭";
            CDatabase.DBUpdate("update taksinfo set finish='false' where id=" + e.CommandArgument);
        }


        rfDB(GroupID);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#EFEFEF'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }
    protected void LinkButton6_Click1(object sender, EventArgs e)
    {
        NotShow();
    }




    protected void UploadFileKey_Click(object sender, EventArgs e)
    {
        string size = "0";
        UploadTaksAccMessage.Visible = true;

        if (!TaksFileUpload1.HasFile)
        {
            UploadTaksAccMessage.Text = "请选择上传[" + TaksAccAtt + "]类型的文件．dv" + TaksFileUpload1.FileName;
        }
        else
        {
            try
            {
                string ext = (System.IO.Path.GetExtension(TaksFileUpload1.FileName).ToLower()).Replace(".", "");
                if (TaksAccAtt.IndexOf(ext) < 0 && TaksFileUpload1.HasFile)
                {
                    UploadTaksAccMessage.Text = "文类型不许可，请上传[" + TaksAccAtt + "]类型的文件．";

                }
                else
                {
                    size = TaksFileUpload1.PostedFile.ContentLength.ToString();

                    if (Convert.ToInt64(size) > TaksAccMaxSize)
                    {

                        UploadTaksAccMessage.Text = "文件超过" + TaksAccMaxSize.ToString() + "b字节,请进行文件压缩．";
                    }
                    else
                    {
                        UploadTaksAccMessage.Visible = false;


                        DateTime dt = DateTime.Now;
                        string filename = dt.Ticks.ToString();//用公元以来的100毫秒为记录.

                        Task.FUNC f = new Task.FUNC();
                        string savepath = Server.MapPath(f.filepath + filename + "." + ext);
                        TaksFileUpload1.SaveAs(savepath);
                        CDatabase.DBUpdate("insert into TaksAcc (linkid,UserID,sort,type,filename,fileAtt,filesize)values(" + EID.Text + "," + EUID.Text + ",0,'Taks','" + filename + "." + ext + "','" + ext + "'," + size + ")");
                        string nownum;
                        nownum = CDatabase.DBReadData("select Count(*) as c from taksacc where linkid=" + EID.Text + " and type='Taks'");
                        if (Convert.ToInt16(nownum) >= TaksAccNum)
                        {
                            UploadFilePanel.Visible = false;
                        }
                        readfilelist(EID.Text);
                    }
                }
            }
            catch
            {
                UploadTaksAccMessage.Text = "文类上传失败，请上传[" + TaksAccAtt + "]类型的文件,并且大小不超过" + TaksAccMaxSize + "b．";
            }
        }
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
    protected void EGridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Task.FUNC f = new Task.FUNC();
        string id = EGridView1.Rows[e.RowIndex].Cells[0].Text;
        string file = Server.MapPath(f.filepath + EGridView1.Rows[e.RowIndex].Cells[1].Text);

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


        CDatabase.DBUpdate("delete from taksacc where id=" + id);

        //UploadTaksAccMessage.Visible = true;
        //UploadTaksAccMessage.Text = file;
        UploadTaksAccMessage.Visible = false;
        readfilelist(EID.Text);
        UploadFilePanel.Visible = true;
    }
    protected void EGridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {

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

        } 
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        rfDB("0");
    }
}
