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 TaskList : System.Web.UI.UserControl
{
    private string filepath = @"~/upfile/task/";
    static string userid = "0";
    static string TaksAccAtt;
    static int TaksAccNum;
    static long TaksAccMaxSize;
    static double TaksMinMoney=100;

    # region 参数

    static string TaskViewUrl="TaskView.aspx";
    static string TaskViewPath = "~/china/";
    static string TaskAddPlanKey = "~/images/task/TaskAddPlanKey.gif";
    public string OpenTaskViewUrl  //打开任务明细页的网页文件名
    {
        set
        {
            TaskViewUrl = value.ToString().Trim();
        }
    }
    public string OpenTaskViewPath   //打开任务明细页的路径
    {
        set
        {
            TaskViewPath = value.ToString().Trim();
        }
    }

    public string OpenTaskAddPlanKey   //参加任务的按钮
    {
        set
        {
            TaskAddPlanKey = value.ToString().Trim();
        }
    }

    #endregion

    protected string CategoryLink(string id)    
    {
        return Request.ServerVariables["URL"].ToString() + "?categoryid="+id;
    }

    protected string AddPlanKeyImg()
    {
        return TaskAddPlanKey;
    }

    protected string CategoryNum(string id)
    {
        string wherestr1 = "";
        string wherestr2 = "";
        string wherestr = "1=1";
        //获取大类中有多少任务
        if (Session["TaskListFinish"].ToString() != "all")
        {
            //wherestr1 = "finish='" + Session["TaskListFinish"].ToString() + "'";
            wherestr1 = "";
        }
        if (id != "0")
        {
            SqlConnection SQLconn = CDatabase.wtoconn();
            SqlDataReader RD;
            SQLconn.Open();
            SqlCommand scd = new SqlCommand("select id from taksCategory where pid=" + id, SQLconn);
            RD = scd.ExecuteReader();
            string re;
            if (RD.Read())
            {
                wherestr2 = "cid=" + Convert.ToString(RD[0]);
                while (RD.Read())
                {
                    wherestr2 += " or cid=" + Convert.ToString(RD[0]);
                }
            }
            SQLconn.Close();
        }

        if (wherestr1 != "" && wherestr2 != "")
        {
            wherestr = wherestr1 + " and (" + wherestr2 + ")";
        }
        else
        {
            //if (wherestr1 != "") wherestr = wherestr1;
            //else 
            if (wherestr2 != "") wherestr = wherestr2;
        }
        string txt = "";
        switch (Request.QueryString["type"])
        {
            case "1":
                txt = "(" + CDatabase.DBReadData("select count(*)  from TaksInfo where " + wherestr + "  and   money<=1000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'") + ")";
                break;
            case "2":
                txt = "(" + CDatabase.DBReadData("select count(*)  from TaksInfo where " + wherestr + "  and   money>1000 and money<2000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'") + ")";
                break;
            case "3":
                txt = "(" + CDatabase.DBReadData("select count(*)  from TaksInfo where " + wherestr + "  and  money>=2000 and money<5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'") + ")";
                break;
            case "4":
                txt = "(" + CDatabase.DBReadData("select count(*)  from TaksInfo where " + wherestr + "  and   money>=5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'") + ")";
                break;
            //default:
            //    return "(" + CDatabase.DBReadData("select Count(*) from TaksInfo where " + wherestr + "  and subject like '%" + Session["TaskListKeyword"].ToString() + "%'") + ")";
            //    break;
        }

        return txt;
    }


    protected void TaskDBList()
    {
        object RD2;
        string wherestr1 = "";
        string wherestr2 = "";
        string wherestr = "1=1";
        
        if (Session["TaskListFinish"].ToString() != "all")
        {
            wherestr1 = "finish='" + Session["TaskListFinish"].ToString() + "'";
        }
        if (Session["TaskCategoryID"].ToString() != "0")
        {
            SqlConnection SQLconn = CDatabase.wtoconn();
            SqlDataReader RD;
            SQLconn.Open();
            
            SqlCommand scd = new SqlCommand("select id from taksCategory where pid=" + Session["TaskCategoryID"].ToString(), SQLconn);
            RD = scd.ExecuteReader();
            string re;
            if (RD.Read())
            {
                wherestr2 = "cid="+Convert.ToString(RD[0]);
                while (RD.Read())
                {
                    wherestr2 += " or cid=" + Convert.ToString(RD[0]);
                }
            }
            SQLconn.Close();
        }

        if (wherestr1 != "" && wherestr2 != "")
        {
            wherestr = wherestr1 + " and (" + wherestr2+")";
        }
        else
        {
            if (wherestr1 != "") wherestr = wherestr1;
            else if(wherestr2!="") wherestr = wherestr2;
        }
        switch(Request.QueryString["type"])
        {
            case "1":
            RD2 = CDatabase.DBFillSelect("select * from TaksInfo where " + wherestr + " and  money<=1000 and subject like '%"+Session["TaskListKeyword"].ToString()+"%'" + Session["TaskListOrderBy"].ToString());
            TaskGridView2.DataSource = RD2;
                break;
        case "2":
            RD2 = CDatabase.DBFillSelect("select * from TaksInfo where " + wherestr + " and  money>1000 and money<2000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'" + Session["TaskListOrderBy"].ToString());
            TaskGridView2.DataSource = RD2;
            break;
        case "3":
            RD2 = CDatabase.DBFillSelect("select * from TaksInfo where " + wherestr + " and  money>=2000 and money<5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'" + Session["TaskListOrderBy"].ToString());
            TaskGridView2.DataSource = RD2;
            break;
        case "4":
            RD2 = CDatabase.DBFillSelect("select * from TaksInfo where " + wherestr + " and  money>=5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'" + Session["TaskListOrderBy"].ToString());
            TaskGridView2.DataSource = RD2;
            break;
        }
        TaskGridView2.DataBind();
        CDatabase.CloseConn();
        TaskListTextBox1.Text = Session["TaskListKeyword"].ToString();
        //Message.Text = "select * from TaksInfo where " + wherestr + " " + Session["TaskListOrderBy"].ToString() + "  " + Session["TaskCategoryID"].ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (IsPostBack)
        {
        }
        else
        {
            //*******获取选择参数
            if (Request["finish"] != null) //finish=false
            {
                Session["TaskListFinish"] = Request["finish"].ToString();
                if (Request["finish"].ToString() == "false")
                {
                    TaskKey02.CssClass = "TaskSelectNow";
                }
                else
                {

                    TaskKey03.CssClass = "TaskSelectNow";
                }
            }
            else
            {
                if (Session["TaskListFinish"] == null)
                {
                    Session["TaskListFinish"] = "all";

                }
                TaskKey01.CssClass = "TaskSelectNow";

            }


            if (Request["CategoryID"] != null)
            {
                Session["TaskCategoryID"] = Request["CategoryID"].ToString();
            }
            else
            {
                if (Session["TaskCategoryID"] == null)
                {
                    Session["TaskCategoryID"] = "0";
                }
            }

            if (Session["TaskListOrderBy"] == null)
            {
                Session["TaskListOrderBy"] = " order by sdt desc";
            }
            if (Session["TaskListKeyword"] == null)
            {
                Session["TaskListKeyword"] = "";
            }


            //==============总数
            if(Session["TaskListFinish"].ToString()=="all")
            {

                   switch (Request.QueryString["type"])
                   {
                       case "1":
                           TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where  money<=1000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                           break;
                       case "2":
                           TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where  money>1000 and money<2000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                           break;
                       case "3":
                           TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where money>=2000 and money<5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                           break;
                       case "4":
                           TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where  money>=5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                           break;
                   }


            }
            else
            {

                switch (Request.QueryString["type"])
                {
                    case "1":
                        TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where finish='" + Session["TaskListFinish"].ToString() + "' and  money<=1000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                        break;
                    case "2":
                        TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where  finish='" + Session["TaskListFinish"].ToString() + "' and   money>1000 and money<2000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                        break;
                    case "3":
                        TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where finish='" + Session["TaskListFinish"].ToString() + "' and   money>=2000 and money<5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                        break;
                    case "4":
                        TotalLabel.Text = CDatabase.DBReadData("select count(*) as cc from TaksInfo where  finish='" + Session["TaskListFinish"].ToString() + "' and  money>=5000 and subject like '%" + Session["TaskListKeyword"].ToString() + "%'");
                        break;
                }
            }




            object RD = CDatabase.DBSelect("select * from TaksCategory where pid=0 order by sort");
            TaskListDataList1.DataSource = RD;
            TaskListDataList1.DataBind();
            CDatabase.CloseConn();
            //TaskListPanel1.Visible = true;
            //ADDTaksPanel1.Visible = false; 

            TaskDBList();

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


            }
            else
            {
            }
        }
    }


    protected string SUBJECT(string id,string subject,string sdt,string edt)
    {

        return "第" + id + "号任务" + ":" + subject + " |  " + (Convert.ToDateTime(sdt)).ToShortDateString() + " ～ " + (Convert.ToDateTime(edt)).ToShortDateString();
    }

    protected string PLANNUM(string id)
    {
        return CDatabase.DBReadData("select count(*) as c from TaksPlan where tid=" + id);
    }

    protected string STATE(string f)
    {
        if (f == "true")
        {
            return "完成";
        }
        else
        {

            return "进行中";
        }
    }


    protected string CategoryMoneyText(string CID, string money)
    {
        return CDatabase.DBReadData("select category from takscategory where id=" + CID) + "<br>" + "￥" + money; ;

    }

    protected string CategoryMoneyBK(string money)
    {
        string css;
        if (Convert.ToUInt16(money) < 1000)
        {
            css = "Money-1000";
        }
        else
        {
            if (Convert.ToUInt16(money) < 2000)
            {
                css = "Money-2000";
            }
            else
            {
                if (Convert.ToUInt16(money) < 5000)
                {
                    css = "Money2000";
                }
                else
                {
                    css = "Money5000";
                }
            }
        }
        return css;


    }

    protected string CATEGORY(string CID)
    {
        return CDatabase.DBReadData("select category from takscategory where id="+CID);
    }
    protected string MONEY(string money)
    {
        return "￥" + money;
    }
    protected string SURPLUS(string sdt, string edt)
    {
        Task.FUNC su = new Task.FUNC();
        return su.SurplusDateTime(sdt, edt);
    }
    protected string ViewTimeOut(string sdt,string edt)
    {
      Task.FUNC su = new Task.FUNC();
      string re=su.SurplusDateTime(sdt, edt);
        if (re=="-")
        {
            return "投稿截止";
        }else{
            return "剩余时间<br>"+re;
        }

    }



    protected string Link(string c, string i)
    {
        return Request.ServerVariables["URL"].ToString()+"?category=" + c + "&categoryid=" + i;

    }

    protected void TaskKey01_Click(object sender, EventArgs e)
    {
        Session["TaskListFinish"] = "all";
        Session["TaskCategoryID"] = "0";
        Session["TaskListKeyword"] = "";
        Response.Redirect("~/china/task_vvwhere.aspx?type=" + Request.QueryString["type"]);
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/china/task_vvwhere.aspx?type=" + Request.QueryString["type"] + "&finish=false");
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/china/task_vvwhere.aspx?type=" + Request.QueryString["type"] + "&finish=true");
    }


    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownContents(string knownCategoryValues, string category)
    {
        return default(AjaxControlToolkit.CascadingDropDownNameValue[]);
    }

    protected void TaskListDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (TaskListDropDownList1.Text == "发布由晚到早") Session["TaskListOrderBy"] = "order by sdt desc";
        if (TaskListDropDownList1.Text == "发布由早到晚") Session["TaskListOrderBy"] = "order by sdt";
        if (TaskListDropDownList1.Text == "截稿由晚到早") Session["TaskListOrderBy"] = "order by edt desc";
        if (TaskListDropDownList1.Text == "截稿由早到晚") Session["TaskListOrderBy"] = "order by edt";
        if (TaskListDropDownList1.Text == "由高到低↓") Session["TaskListOrderBy"] = "order by money desc";
        if (TaskListDropDownList1.Text == "由低到高↑") Session["TaskListOrderBy"] = "order by money";
        if (TaskListDropDownList1.Text == "由多到小↓") Session["TaskListOrderBy"] = "order by callnum desc";
        if (TaskListDropDownList1.Text == "由小到多↑") Session["TaskListOrderBy"] = "order by callnum";
        TaskDBList();
    }
    protected void TaskListLinkButton2_Click(object sender, EventArgs e)
    {
        Session["TaskListKeyword"] = TaskListTextBox1.Text;
        TaskDBList();
    }

    protected string TASKVIEWURL(string id)
    {
        return TaskViewPath+TaskViewUrl + "?taskid=" + id;

    }
    protected void TaskGridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        # region 行变色
        int i;
        int g = 0;
        /*
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < TaskGridView2.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#EEEEEE'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#000000'");
            }
            g++;
        }
         * */
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#EFEFEF'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion


        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //BACKGROUND-IMAGE: url(../../images/task/money-2000.gif); WIDTH: 100px; HEIGHT: 43px
            //Table CMBK=(Table)e.Row.FindControl("CategoryMoneyBK");
            //CMBK.Style = "";


            //HyperLink TVHL2 = (HyperLink)e.Row.FindControl("TaskViewHyperLink2");
            //TVHL2.CssClass = "Money-2000";

                //.BackImageUrl = @"/images/task/money+2000.gif";
                //= "BACKGROUND-IMAGE: url(/images/task/money+2000.gif); WIDTH: 100px; HEIGHT: 43px";
            //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 void TaskGridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        TaskGridView2.PageIndex = e.NewPageIndex;
        TaskGridView2.DataBind();
    }
}
