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.IO;

public partial class uc_case_Pall : System.Web.UI.UserControl
{
    static string filepath = @"~/upfile/task/";
    static string userid = "0";
    static string TaksAccAtt;
    static int TaksAccNum;
    static long TaksAccMaxSize;
    static double TaksMinMoney=100;

    private string id;
    private string lx;
    public string pId
    {
        set
        {
             id = value.ToString().Trim();
        }
    }
    public string LX
    {
        set
        {
            lx = value.ToString().Trim();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        else
        {

            if (Request["categoryID"] == null)
            {

                object RD = CDatabase.DBSelect("select * from TaksCategory where pid=0 order by sort");
                AddTaksDataList1.DataSource = RD;
                AddTaksDataList1.DataBind();
                CDatabase.CloseConn();
                AddTaksSelectCategoryPanel.Visible = true;
                ADDTaksPanel1.Visible = false;
            }
            else
            {
                AddTaksCategory.Text = Request["category"].ToString();

                AddTaksCategoryID.Text = Request["categoryid"].ToString();

                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() + "个文件";
                readfilelist();
                AddTaksSelectCategoryPanel.Visible = false;
                ADDTaksPanel1.Visible = true;
            }
        }
    }

    public string FilePath(string file)
    {

        return filepath + file;
    }

    protected void readfilelist()
    {
        object RD = CDatabase.DBSelect("select * from TaksAcc where UserID=" + userid + " and linkID=0 order by sort");
        GridView1.DataSource = RD;
        GridView1.DataBind();
        CDatabase.CloseConn();

    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.Rows[e.RowIndex].Cells[0].Text;
        string file = Server.MapPath(filepath) + GridView1.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();
        UploadFilePanel.Visible = true;

    }
    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;
            e.Row.Cells[1].Visible = false;

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

    }


    protected void UploadFileKey_Click(object sender, EventArgs e)
    {


        string size = "0";
        UploadTaksAccMessage.Visible = true;

        if (!TaksFileUpload1.HasFile)
        {
            UploadTaksAccMessage.Text = "请选择上传[" + TaksAccAtt + "]类型的文件．";
        }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毫秒为记录.
                        string savepath = Server.MapPath(filepath + filename + "." + ext);
                        TaksFileUpload1.SaveAs(savepath);
                        CDatabase.DBUpdate("insert into TaksAcc (UserID,sort,type,filename,fileAtt,filesize)values(" + userid + ",0,'Taks','" + filename + "." + ext + "','" + ext + "'," + size + ")");
                        string nownum;
                        nownum = CDatabase.DBReadData("select Count(*) as c from taksacc where userid=" + userid+" and linkid=0");
                        if (Convert.ToInt16(nownum) >= TaksAccNum)
                        {
                            UploadFilePanel.Visible = false;
                        }
                        readfilelist();
                    }
                }
            }
            catch
            {
                UploadTaksAccMessage.Text = "文类上传失败，请上传[" + TaksAccAtt + "]类型的文件,并且大小不超过"+TaksAccMaxSize+"b．";
            }
        }

        
    }
    protected void LinkButton1_Click1(object sender, EventArgs e)
    {
        if (TAKSEDT.Text == "选择日期")
        {
            TaksEDTMessage.Text = "请选择日期!";
            return;
        }
        else
        {
            DateTime dt = DateTime.Now;
            CDatabase.DBUpdate("insert into taksinfo (subject,userid,cid,info,money,sdt,edt)values('" + AddTaksSubject.Text + "'," + userid + "," + AddTaksCategoryID.Text + ",'" + Content.Text + "'," + TaksMoney.Text + ",'" + dt.ToString() + "','" + TAKSEDT.Text + "')");
            string id = CDatabase.DBReadData("select id from taksinfo where userid='" + userid + "' order by id desc");

            CDatabase.DBUpdate("update taksacc set linkID=" + id + " where userid='" + userid + "' and linkID=0");

            AddTaksSubject.Text = "";


            ADDTaksPanel1.Visible = false;
            AddTaksPanel2.Visible = true;
        }
    }

    protected void TAKSEDTCalendar1_SelectionChanged(object sender, EventArgs e)
    {
        TaksEDTMessage.Text = "";
        TAKSEDT.Text = TAKSEDTCalendar1.SelectedDate.ToString();
        TAKSEDTCalendar1.Visible = false;
    }
    protected void TAKSEDT_Click(object sender, EventArgs e)
    {
        TAKSEDTCalendar1.Visible = true;
    }


    protected void AddTaksDataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {



            //DataRowView dr = (DataRowView)e.Row.DataItem;
           DataList AddTaksDataList2 = (DataList)e.Item.FindControl("AddTaksDataList2");
           Label id = (Label)e.Item.FindControl("Label1");
           
            object RD = CDatabase.DBSelect("select * from TaksCategory where pid=" + id.Text);
            AddTaksDataList2.DataSource = RD;
            AddTaksDataList2.DataBind();
            CDatabase.CloseConn();
        }
    }
    protected string Link(string c, string i)
    {
        return Request.ServerVariables["URL"].ToString()+"?category=" + c + "&categoryid=" + i;

    }

    /*
     *     protected void SelCategory2_Click(object sender, EventArgs e)
    {
        DataList DL2 = (DataList)AddTaksDataList1.FindControl("AddTaksDataList2");
        //LinkButton SelCategory = (LinkButton)DL2.FindControl("SelCategory2");
        string SelID = ((Label)DL2.FindControl("Label2")).Text.Trim();
        //AddTaksCategory.Text = SelCategory.Text;
        
        //AddTaksCategoryID.Text = SelID.Text;
        AddTaksSelectCategoryPanel.Visible = false;
        ADDTaksPanel1.Visible = true;

    }
     */
}
