using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HySoft.Common;
using System.Data;

namespace HySoft.BaseCallCenter.Web.telmanage.ajax
{
    /// <summary>
    /// workcalendarset 的摘要说明
    /// </summary>
    public class workcalendarset : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getcalendarlist":
                    context.Response.Write(getcalendarlist(context));
                    break;
                case "changworkingday":
                    context.Response.Write(setworkingday(context));
                    break;
                case "getTimeList":
                    context.Response.Write(getTimeList(context));
                    break;
                case "deletelist":
                    context.Response.Write(Delete(context));
                    break;
            }
        }

        public int changeWeek(DayOfWeek week)
        {
            switch (week.ToString())
            {
                case "Monday": return 1;
                case "Tuesday": return 2;
                case "Wednesday": return 3;
                case "Thursday": return 4;
                case "Friday": return 5;
                case "Saturday": return 6;
                case "Sunday": return 0;
                default: return 0;
            }

        }

        #region 获取数据
        private string getcalendarlist(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string table = @"<table width='100%' style='border:0px;text-align:center;font-size:15px;font-weight:bold;'><tr height='50px'><td width='100px'>日</td><td width='100px'>一</td><td width='100px'>二</td><td width='100px'>三</td><td width='100px'>四</td><td width='100px'>五</td><td width='100px'>六</td></tr><tr height='50px'>";
                BLL.T_Sys_WorkCalendar WorkCalendar = new BLL.T_Sys_WorkCalendar();
                dt = WorkCalendar.GetList(" DATEDIFF (m,'" + CommonRequest.GetQueryString("strdate") + "',F_DateId )=0").Tables[0];
                int day = changeWeek((Convert.ToDateTime(dt.Rows[0]["F_DateId"]).DayOfWeek));

                #region
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        for (int j = 0; j < day; j++)
                        {
                            table += "<td>&nbsp;</td>";
                        }


                    }
                    if ((day + i) % 7 == 0)//周六
                    {
                        if (dt.Rows[i]["F_HolidayFlag"].ToString() == "2")
                        {
                            table += "</tr><tr height='50px'><td><font color=\"red\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' >" + dt.Rows[i]["F_HolidayName"] + "</font></td>";
                        }
                        else if (dt.Rows[i]["F_WorkingFlag"].ToString() == "False" && dt.Rows[i]["F_HolidayFlag"].ToString() != "2")
                        {
                            table += "</tr><tr height='50px'><td><font color=\"red\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' onclick=\"changeState(this)\">" + Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("dd") + "</font></td>";
                        }
                        else
                        {
                            table += "</tr><tr height='50px'><td><font color=\"black\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' onclick=\"changeState(this)\">" + Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("dd") + "</font></td>";
                        }
                    }
                    else if ((day + i) % 7 == 1)//周日
                    {
                        if (dt.Rows[i]["F_HolidayFlag"].ToString() == "2")
                        {
                            table += "<td><font color=\"red\" id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' >" + dt.Rows[i]["F_HolidayName"] + "</font></td>";
                        }
                        else if (dt.Rows[i]["F_WorkingFlag"].ToString() == "False" && dt.Rows[i]["F_HolidayFlag"].ToString() != "2")
                        {
                            table += "<td><font color=\"red\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' onclick=\"changeState(this)\">" + Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("dd") + "</font></td>";
                        }
                        else
                        {
                            table += "<td><font color=\"black\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' onclick=\"changeState(this)\">" + Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("dd") + "</font></td>";
                        }
                    }
                    else
                    {
                        if (dt.Rows[i]["F_HolidayFlag"].ToString() == "2")
                        {
                            table += "<td><font color=\"red\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' >" + dt.Rows[i]["F_HolidayName"] + "</font></td>";
                        }
                        else if (dt.Rows[i]["F_WorkingFlag"].ToString() == "False" && dt.Rows[i]["F_HolidayFlag"].ToString() != "2")
                        {
                            table += "<td><font color=\"red\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' onclick=\"changeState(this)\">" + Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("dd") + "</font></td>";
                        }
                        else
                        {
                            table += "<td><font color=\"black\" style='cursor:pointer;' id='" +  Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("yyyy-MM-dd") + "' onclick=\"changeState(this)\">" + Convert.ToDateTime(dt.Rows[i]["F_DateId"]).ToString("dd") + "</font></td>";
                        }
                    }

                }
                #endregion
                table += "</tr></table>";
                res = table;
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }
        #endregion
        private string getTimeList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            try
            {
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;
                string keyword = HttpUtility.UrlDecode(CommonRequest.GetQueryString("keyword"));
                string sql = " ";
                if (keyword.Trim() != "" && keyword != "undefined")
                {
                    sql += " and (convert(varchar,F_WorkEndTimes,120) like '%" + keyword.Trim() + "%' or convert(varchar,F_WorkEndTimes,120) like '%" + keyword.Trim()
                        + "%' or F_TimeName like '%" + keyword.Trim() + "%'or F_Remark like '%" + keyword.Trim() + "%')";
                }
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }
                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }
                int recordCount = 0;
                Model.PageData<Model.T_Sys_WorkTimes> pageModel = new Model.PageData<Model.T_Sys_WorkTimes>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_WorkTimes",
                    "F_WorkStartTimes",
                    "*",
                    sql,
                    "ORDER BY F_WorkStartTimes desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Sys_WorkTimes> modelList = new BLL.T_Sys_WorkTimes().DataTableToList(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.T_Sys_WorkTimes>));
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }
        #region 删除数据
        private string Delete(HttpContext context)
        {
            bool res = false;
            string arr = HttpUtility.UrlDecode(CommonRequest.GetQueryString("arrid"));
            if (arr.IndexOf(",") == -1)
            {
                DateTime dt =new DateTime();
                if (DateTime.TryParse(arr, out dt))
                    res = new BLL.T_Sys_WorkTimes().Delete(dt);
            }
            else
            {
                res = new BLL.T_Sys_WorkTimes().DeleteList(arr);
            }


            return res ? "success" : "field";
        }

        #endregion
        private bool setworkingday(HttpContext context)
        {
            string date = CommonRequest.GetQueryString("dateid");
            DateTime dt = new DateTime();
            if (DateTime.TryParse(date, out dt))
            {
                Model.T_Sys_WorkCalendar wc = new BLL.T_Sys_WorkCalendar().GetModel(dt);
                if (wc != null)
                {
                    if (wc.F_EditFlag == false) //没有修改过
                    {
                        if (wc.F_WorkingFlag)
                        {
                            wc.F_WorkingFlag = false;
                        }
                        else
                        {
                            wc.F_WorkingFlag = true;
                        }
                        wc.F_EditFlag = true;
                    }
                    else//修改过 
                    {
                        if (wc.F_WorkingFlag)
                        {
                            wc.F_WorkingFlag = false;
                        }
                        else
                        {
                            wc.F_WorkingFlag = true;
                        }
                        wc.F_EditFlag = false;
                    }
                    bool rvalue = new BLL.T_Sys_WorkCalendar().Update(wc);
                    if (rvalue)
                    {
                        return true;
                    }
                    else { return false; }
                }
                else { return false; }
            }
            else { return false; }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}