using System;
using System.Data.SqlClient;
using System.Collections.Generic;
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.IO;
using Yahoo.Yui.Compressor;
using SurveyModel;
using DLLforSatisfyWeb;
using System.Text;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;

namespace DimHelper
{
    public class SchedulerListItem
    {

        public string TaskID;
        public string OwnerID;
        public string Title;
        public string Description;
        public string Start;
        public string End;
        public string RecurrenceRule;
        public string RecurrenceID;
        public string RecurrenceException;
        public string IsAllDay;

    }  


  
    public partial class DimHelper : System.Web.UI.Page
    {
        //System.Web.UI.Page
         protected void Page_Load(object sender, EventArgs e){

             string SearchInput = Request["SearchInput"];
             string SearchValue = Server.UrlDecode(Request["SearchValue"]);


             string Prex = "";
             if (!(Request["Prex"] == null))
             {
                 Prex = Request["Prex"];
             }

        

             string user = "";
             if (!(Request["user"] == null))
             {
                 user = Server.UrlDecode(Request["user"]);
             }

             string pwd = "";
             if (!(Request["pwd"] == null))
             {
                 pwd = Server.UrlDecode(Request["pwd"]);
             }

             string imsi = "";
             if (!(Request["imsi"] == null))
             {
                 imsi = Request["imsi"];
             }

             string version = "";
             if (!(Request["version"] == null))
             {
                 version = Request["version"];
             }

             string templet = "";
             if (!(Request["templet"] == null))
             {
                 templet = Request["templet"];
             }
             
             string userid = "";
             if (!(Request["userid"] == null))
             {
                 userid = Request["userid"];
             }
             string old_pwd = "";
             if (!(Request["old_pwd"] == null))
             {
                 old_pwd = Server.UrlDecode(Request["old_pwd"]);
             }
             string new_pwd = "";
             if (!(Request["new_pwd"] == null))
             {
                 new_pwd = Server.UrlDecode(Request["new_pwd"]);
             }

             string OtherCondition = "";
             if (!(Request["OtherCondition"] == null))
             {
                 OtherCondition = Server.UrlDecode(Request["OtherCondition"]);
             }

             string KPIStr = "";
             if (!(Request["KPIStr"] == null))
             {
                 KPIStr = Server.UrlDecode(Request["KPIStr"]);
             }

             string GroupID = "";
             if (!(Request["GroupID"] == null))
             {
                 GroupID = Request["GroupID"];
             }

             string UsePersonDataControl = "";
             if (!(Request["UsePersonDataControl"] == null))
             {
                 UsePersonDataControl = Request["UsePersonDataControl"];
             }

             string ifUseOAMainTable = "";
             if (!(Request["ifUseOAMainTable"] == null))
             {
                 ifUseOAMainTable = Request["ifUseOAMainTable"];
             }

             string MyStore = "-1";
             if (!(Request["MyStore"] == null))
             {
                 MyStore = Request["MyStore"];
             }
             string CurrentDashRole = "";
             if (!(Request["CurrentDashRole"] == null))
             {
                 CurrentDashRole = Server.UrlDecode(Request["CurrentDashRole"]);
             }

             string BackSqlColumn = "";
             if (!(Request["BackSqlColumn"] == null))
             {
                 BackSqlColumn = Request["BackSqlColumn"];
             }

             string HideCost = "";
             if (!(Request["HideCost"] == null))
             {
                 HideCost = Request["HideCost"];
             }

             string AtachTableSearch = "";
             if (!(Request["AtachTableSearch"] == null))
             {
                 AtachTableSearch = Request["AtachTableSearch"];
             }
             

             string MobileSqlColumn = "";
             if (!(Request["MobileSqlColumn"] == null))
             {
                 MobileSqlColumn = Request["MobileSqlColumn"];
             }

             string SMSCC = "";
             if (!(Request["SMSCC"] == null))
             {
                 SMSCC = Request["SMSCC"];
             }

             string SMSBODY = "";
             if (!(Request["SMSBODY"] == null))
             {
                 SMSBODY = Server.UrlDecode(Request["SMSBODY"]);
             }
             

             string OAMainOrderNum = "";
             if (!(Request["OAMainOrderNum"] == null))
             {
                 OAMainOrderNum = Request["OAMainOrderNum"];
             }

             string FromSimple = "";
             if (!(Request["FromSimple"] == null))
             {
                 FromSimple = Request["FromSimple"];
             }

             string GridList = "";
             if (!(Request["GridList"] == null))
             {
                 GridList = Request["GridList"];
             }
             

             string SalarySolutionID = "";
             if (!(Request["SalarySolutionID"] == null))
             {
                 SalarySolutionID = Request["SalarySolutionID"];
             }

             string Num = "";
             if (!(Request["Num"] == null))
             {
                 Num = Request["Num"];
             }

             string SaaSClientKey = "1";
             if (!(Request["SaaSClientKey"] == null))
             {
                 SaaSClientKey = Request["SaaSClientKey"];
             }
             

             string Pre= "";
             if (!(Request["Pre"] == null))
             {
                 Pre = Request["Pre"];
             }
             
             string FromTaskSub = "";
             if (!(Request["FromTaskSub"] == null))
             {
                 FromTaskSub = Request["FromTaskSub"];
             }

             string PrivateDepartID = "";
             if (!(Request["PrivateDepartID"] == null))
             {
                 PrivateDepartID = Request["PrivateDepartID"];
             }
             

             string UpdateCach = "";
             if (!(Request["UpdateCach"] == null))
             {
                 UpdateCach = Request["UpdateCach"];
             }

             string LastIstemplate = "0";
             if (!(Request["LastIstemplate"] == null))
             {
                 LastIstemplate = Request["LastIstemplate"];
             }

             string byRandom = "";
             if (!(Request["byRandom"] == null))
             {
                 byRandom = Request["byRandom"];
             }

             string RandomNum = "-1";
             if (!(Request["RandomNum"] == null))
             {
                 RandomNum = Request["RandomNum"];
             }

             string Emails = "";
             if (!(Request["Emails"] == null))
             {
                 Emails = Server.UrlDecode(Request["Emails"]);
             }

             string EmailCC = "";
             if (!(Request["EmailCC"] == null))
             {
                 EmailCC = Server.UrlDecode(Request["EmailCC"]);
             }

             

             string BackEmail = "";
             if (!(Request["BackEmail"] == null))
             {
                 BackEmail = Server.UrlDecode(Request["BackEmail"]);
             }

                  string EmailSubject = "";
             if (!(Request["EmailSubject"] == null))
             {
                 EmailSubject= Server.UrlDecode(Request["EmailSubject"]);
             }

                 string EmailBody = "";
             if (!(Request["EmailBody"] == null))
             {
                 EmailBody= Server.UrlDecode(Request["EmailBody"]);
             }


             string CubeID = "";
             if (!(Request["CubeID"] == null))
             {
                 CubeID = Request["CubeID"];
             }

             string NumID = "";
             if (!(Request["NumID"] == null))
             {
                 NumID = Request["NumID"];
             }
             string ContextFilterStr = "";
             if (!(Request["ContextFilterStr"] == null))
             {
                 ContextFilterStr =  Server.UrlDecode(Request["ContextFilterStr"]);
             }

             string encodedContextFilterStr = "";
             if (!(Request["encodedContextFilterStr"] == null))
             {
                 encodedContextFilterStr = Request["encodedContextFilterStr"];
             }

             string ShowTable = "SalaryMain";
             if (!(Request["ShowTable"] == null))
             {
                 ShowTable = Request["ShowTable"];
             }

             string CurrentContrastType = "";
             if (!(Request["CurrentContrastType"] == null))
             {
                 CurrentContrastType = Request["CurrentContrastType"];
             }
             

             string FilterStr = "";
             if (!(Request["FilterStr"] == null))
             {
                 FilterStr = Request["FilterStr"];
             }

             string fromAttri= "0";
             if (!(Request["fromAttri"] == null))
             {
                 fromAttri= Request["fromAttri"];
             }

             string FilterType = "0";
             if (!(Request["FilterType"] == null))
             {
                 FilterType = Request["FilterType"];
             }

             string FilterColumn= "";
             if (!(Request["FilterColumn"] == null))
             {
                 FilterColumn= Request["FilterColumn"];
             }

             string Passed = "";
             if (!(Request["Passed"] == null))
             {
                 Passed = Request["Passed"];
             }

             string isTemplate = "0";
             if (!(Request["isTemplate"] == null))
             {
                 isTemplate = Request["isTemplate"];
             }

             string OASolutionKey = "1";
             if (!(Request["OASolutionKey"] == null))
             {
                 OASolutionKey = Request["OASolutionKey"];
             }

             string OAOrderNum = "";
             if (!(Request["OAOrderNum"] == null))
             {
                 OAOrderNum = Request["OAOrderNum"];
             }

             string CurrentOAWorkPosition = "";
             if (!(Request["CurrentOAWorkPosition"] == null))
             {
                 CurrentOAWorkPosition = Request["CurrentOAWorkPosition"];
             }

             string SolutionRoleKey = "";
             if (!(Request["SolutionRoleKey"] == null))
             {
                 SolutionRoleKey = Request["SolutionRoleKey"];
             }

             string SpecialCube= "";
             if (!(Request["SpecialCube"] == null))
             {
                 SpecialCube = Request["SpecialCube"];
             }

             string SpecialDim = "";
             if (!(Request["SpecialDim"] == null))
             {
                 SpecialDim = Request["SpecialDim"];
             }
             

             string ChangeRowSearchKey = "";
             if (!(Request["ChangeRowSearchKey"] == null))
             {
                 ChangeRowSearchKey = Request["ChangeRowSearchKey"];
             }
             string ChangeRowSearchKeyValue = "";
             if (!(Request["ChangeRowSearchKeyValue"] == null))
             {
                 ChangeRowSearchKeyValue = Request["ChangeRowSearchKeyValue"];
             }
             

             string GetValueStr = "";
             if (!(Request["GetValueStr"] == null))
             {
                 GetValueStr = Request["GetValueStr"];
             }
             string SearchConditionStr = "";
             if (!(Request["SearchConditionStr"] == null))
             {
                 SearchConditionStr = Server.UrlDecode(Request["SearchConditionStr"]);
             }

             string SearchConditionStr2 = "";
             if (!(Request["SearchConditionStr2"] == null))
             {
                 SearchConditionStr2 = Server.UrlDecode(Request["SearchConditionStr2"]);
             }
             

             string GetKPIStr = "";
             if (!(Request["GetKPIStr"] == null))
             {
                 GetKPIStr = Request["GetKPIStr"];
             }
             string CubeStr = "";
             if (!(Request["CubeStr"] == null))
             {
                 CubeStr = Request["CubeStr"];
             }
             string ChoseColumnStr = "";
             if (!(Request["ChoseColumnStr"] == null))
             {
                 ChoseColumnStr = Request["ChoseColumnStr"];
             }
             
                string RoleID = "1";
             if (!(Request["NomiRole"] == null))
             {
                 RoleID = Request["NomiRole"];
             }

             string Constr = "";
             if (!(Request["Constr"] == null))
             {
                 Constr= Server.UrlDecode(Request["Constr"]);
             }

             string ConstrLeaf = "";
             if (!(Request["ConstrLeaf"] == null))
             {
                 ConstrLeaf = Server.UrlDecode(Request["ConstrLeaf"]);
             }
             
             string FatherKey = "";
             if (!(Request["FatherKey"] == null))
             {
                 FatherKey = Request["FatherKey"];
             }
            string FatherStatus = "";
             if (!(Request["FatherStatus"] == null))
             {
                 FatherStatus= Request["FatherStatus"];
             }
             string FatherKeySelectStr = "";
             if (!(Request["FatherKeySelectStr"] == null))
             {
                 FatherKeySelectStr = Request["FatherKeySelectStr"];
             }
             string OLAPKey = "";
             if (!(Request["OLAPKey"] == null))
             {
                 OLAPKey = Request["OLAPKey"];
             }
            string IfDel = "";
             if (!(Request["IfDel"] == null))
             {
                 IfDel = Request["IfDel"];
             }
             string DimType = "";
             if (!(Request["DimType"] == null))
             {
                 DimType= Request["DimType"];
             }
             string DimName = "";
             if (!(Request["DimName"] == null))
             {
                 DimName= Request["DimName"];
             }

             string getType = "Leaf";
             if (!(Request["getType"] == null))
             {
                 getType = Request["getType"];
             }
             string FromWhere = "";
             if (!(Request["FromWhere"] == null))
             {
                 FromWhere = Request["FromWhere"];
             }
             string NameKey = "";
             if (!(Request["NameKey"] == null))
             {
                 NameKey = Request["NameKey"];
             }


             string OutDataNameKey= "";
             if (!(Request["OutDataNameKey"] == null))
             {
                 OutDataNameKey = Request["OutDataNameKey"];
             }

             string SelectColumn = "";
             if (!(Request["SelectColumn"] == null))
             {
                 SelectColumn =  Server.UrlDecode(Request["SelectColumn"]);
             }
             string TargetColumn = "";
             if (!(Request["TargetColumn"] == null))
             {
                 TargetColumn = Request["TargetColumn"];
             }
             string TargetTable = "";
             if (!(Request["TargetTable"] == null))
             {
                 TargetTable = Request["TargetTable"];
             }
             string TargetName = "";
             if (!(Request["TargetName"] == null))
             {
                 TargetName = Request["TargetName"];
             }
             string TargetValue = "";
             if (!(Request["TargetValue"] == null))
             {
                 TargetValue = Server.UrlDecode(Request["TargetValue"]);
             }

             string StartTime= "";
             if (!(Request["StartTime"] == null))
             {
                 StartTime = Request["StartTime"];
             }
             string EndTime = "";
             if (!(Request["EndTime"] == null))
             {
                 EndTime = Request["EndTime"];
             }
             string Para = "";
             if (!(Request["Para"] == null))
             {
                 Para = Request["Para"];
             }

             string DataType = "";
             if (!(Request["DataType"] == null))
             {
                 DataType= Request["DataType"];
             }

             string PartName = "";
             if (!(Request["PartName"] == null))
             {
                PartName = Server.UrlDecode(Request["PartName"]);
             }

             string ParaValue = "";
             if (!(Request["ParaValue"] == null))
             {
                 ParaValue = Server.UrlDecode(Request["ParaValue"]);
             }
             string DimsolutionID = "1";
             if (!(Request["DimsolutionID"] == null))
             {
                 DimsolutionID = Request["DimsolutionID"];
             }

             string SearchTime = "";
             if (!(Request["SearchTime"] == null))
             {
                 SearchTime= Request["SearchTime"];
             }

             string SearchTimeStart = "";
             if (!(Request["SearchTimeStart"] == null))
             {
                 SearchTimeStart= Request["SearchTimeStart"];
             }

             string SearchTimeType = "";
             if (!(Request["SearchTimeType"] == null))
             {
                 SearchTimeType = Request["SearchTimeType"];
             }

             string Prefix = "";
             if (!(Request["Prefix"] == null))
             {
                 Prefix= Request["Prefix"];
             }

             string TableName= "";
             if (!(Request["TableName"] == null))
             {
                 TableName= Request["TableName"];
             }
             

             string AgentType = "";
             if (!(Request["AgentType"] == null))
             {
                 AgentType= Request["AgentType"];
             }

             string UserID = "";
             if (!(Request["UserID"] == null))
             {
                 UserID= Request["UserID"];
             }

             string FromID = "";
             if (!(Request["FromID"] == null))
             {
                 FromID = Request["FromID"];
             }


             if (SearchInput == "schedulerevent")
             {
                 Response.Clear();
                 Response.ContentType = "application/json";
                 Response.Charset = "UTF-8";
                 Response.Write(GetSchedulerEvent(SearchValue, StartTime, EndTime));
                 Response.End();
             }
                    
         }

        protected string GetSchedulerEvent(string SearchValue, string StartTime, string EndTime)
        {

            //I have tried save the data into an object, then use the new JavaScriptSerializer().Serialize(object) to create json, but the problem is the same.
            List<SchedulerListItem> Tasktings = new List<SchedulerListItem>();
            SchedulerListItem sli = null;
            string returnstr = "";

            //I have tried save the data into an stringbuilder, then use the stringbilder.tostring to create json, but the problem is the same.
            //System.Text.StringBuilder builder = new System.Text.StringBuilder();

            DataTable dt = null;
            DataRow[] drdtv = null;

            //This is nothing but a tool dll, you could replace this with the simple sqlcomand to read the database,  but the problem is the same.
            DLLforSatisfyWeb.DLLforSatisfyWeb web = new DLLforSatisfyWeb.DLLforSatisfyWeb();
            using (System.Data.SqlClient.SqlConnection ok = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["HROK"].ConnectionString))
            {
                web.cnOKcon = ok;
                web.cnOKcon.Open();

                //This is a datatable in sql server 2005.
                string sql = "select TableID,TaskName,TaskScript,AgentKey,CONVERT(varchar(100), StartTime, 20) as 'StartTime',CONVERT(varchar(100), FinishTime, 20) as 'FinishTime',EventRecurrenceID,EventRecurrenceRule,EventRecurrenceException,EventIsAllDay from OAOrderTaskFactBill where StartTime>='" + StartTime + "' and FinishTime<'" + EndTime + "' and AgentKey in (" + SearchValue + ") and isEventKey='1' order by TaskOrder";
                //This is a tool method to read the database, you could replace this with the simple sqlcomand,  but the problem is the same.
                dt = web.GetValueRunSQL(sql);

                drdtv = dt.Select();
                for (int t = 0; t < drdtv.Length; t++)
                {
                    //to make it clear I just get one record to show the problem.
                    if (t == 0)
                    {
                        returnstr = "\"" + "TaskID" + "\"" + ":" + "\"" + drdtv[t]["TableID"].ToString() + "\"";
                    }

                    //no luck, the same problem.
                    //sli = new SchedulerListItem();
                    //sli.TaskID =drdtv[t]["TableID"].ToString();
                    //sli.Title =drdtv[t]["TaskName"].ToString();
                    //sli.OwnerID =drdtv[t]["AgentKey"].ToString();
                    //sli.Description =drdtv[t]["TaskScript"].ToString();
                    //sli.Start =drdtv[t]["StartTime"].ToString();
                    //sli.End =drdtv[t]["FinishTime"].ToString();
                    //sli.RecurrenceID=drdtv[t]["EventRecurrenceID"].ToString();
                    //sli.RecurrenceRule =drdtv[t]["EventRecurrenceRule"].ToString();
                    //sli.RecurrenceException =drdtv[t]["EventRecurrenceException"].ToString();
                    //sli.IsAllDay =drdtv[t]["EventIsAllDay"].ToString();

                    //Tasktings.Add(sli);

                }
            }
            // nothing but to destroy the tool object. nothing to do with the problem.
            web.DisposeAll("1");

            //if I just hard-code the json string, it works fine!
            //string test1="[{\"TaskID\":\"885\",\"OwnerID\":\"475\",\"Title\":\"早会\",\"Description\":\"开早会。\",\"Start\":\"2014-11-17 00:00:00\",\"End\":\"2014-11-17 00:00:00\",\"RecurrenceRule\":\"\",\"RecurrenceID\":\"\",\"RecurrenceException\":\"\",\"IsAllDay\":\"0\"}]";
            //string test1 = "[{\"TaskID123\":\"885\"}]";

            // get the variable from the record.
            string tes = dt.Rows[0]["TableID"].ToString();
            //create the json from it.
            string test2 = "[{" + "\"" + "TaskID" + "\"" + ":" + "\"" + tes + "\"" + "}]";
            return test2;

            
        }

    }
}


