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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getlist":
                    context.Response.Write(LoadList(context));
                    break;
                case "deletelist":
                    context.Response.Write(Delete(context));
                    break;
                case "opt":
                    context.Response.Write(Opt(context));
                    break;
                case "clear":
                    break;
            }
        }
        #region 获取数据
        private string LoadList(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 strtelnum = HttpUtility.UrlDecode(CommonRequest.GetQueryString("telnum"));
                string strsettime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("settime"));
                string strremovetime = HttpUtility.UrlDecode(CommonRequest.GetQueryString("removetime"));
                string status = HttpUtility.UrlDecode(CommonRequest.GetQueryString("status"));
                string sql = " ";
                if (strtelnum.Trim() != "" && strtelnum != "undefined")
                {
                    sql += " and F_Phone= '" + strtelnum.Trim() + "' ";
                }
                if (strsettime.Trim() != "" && strsettime != "undefined")
                {
                    sql += " and F_LeaveTime > '" + Convert.ToDateTime(strsettime.Trim()) + "' ";
                }
                if (strremovetime.Trim() != "" && strremovetime != "undefined")
                {
                    sql += " and F_DealTime < '" + Convert.ToDateTime(strremovetime.Trim()) + "' ";
                }
                if (status.Trim() != "-1" && status != "undefined")
                {
                    sql += " and F_Status = " + Convert.ToInt32(status);
                }
                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_Call_LeaveRecord> pageModel = new Model.PageData<Model.T_Call_LeaveRecord>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Call_LeaveRecord",
                    "F_Id",
                    "*",
                    sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Call_LeaveRecord> modelList = new BLL.T_Call_LeaveRecord().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_Call_LeaveRecord>));
                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;
        }
        #endregion

        #region 删除数据
        private string Delete(HttpContext context)
        {
            bool res = false;
            string arr = HttpUtility.UrlDecode(CommonRequest.GetQueryString("arrid"));
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Call_LeaveRecord().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Call_LeaveRecord().DeleteList(arr);
            }
            return res ? "success" : "field";
        }
        #endregion

        #region 更新状态数据
        private string Opt(HttpContext context)
        {
            string res = "";
            string arr = HttpUtility.UrlDecode(CommonRequest.GetQueryString("arrid"));
            Model.T_Call_LeaveRecord model = new BLL.T_Call_LeaveRecord().GetModel(Convert.ToInt32(arr));
            if (model.F_Status == 0)
            {
                model.F_Status = 1;
                model.F_DealTime = DateTime.Now;
                model.F_DealContent = "1";
                model.F_UserName = CookieUtil.GetCookie("BaseCallCenter_T_User", "F_UserName");
                model.F_UserCode = CookieUtil.GetCookie("BaseCallCenter_T_User", "F_UserCode");
                model.F_UserId = Convert.ToInt32(CookieUtil.GetCookie("BaseCallCenter_T_User", "F_UserID"));
                
                if (new BLL.T_Call_LeaveRecord().Update(model))
                {
                    res = "success";
                }
                else
                {
                    res = "field";
                }
            }
            else
            {
                res = "noneed";
            }
            return res;
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}