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

namespace HySoft.BaseCallCenter.Web.sysmanage.ajax
{
    /// <summary>
    /// calllogs 的摘要说明
    /// </summary>
    public class calllogs : 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 "delete":
                    context.Response.Write(Delete(context));
                    break;
            }
        }

        #region 获取数据
        private string LoadList(HttpContext context)
        {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";

            try
            {
                string name = HttpUtility.UrlDecode(CommonRequest.GetQueryString("name"));
                string time = HttpUtility.UrlDecode(CommonRequest.GetQueryString("time"));
                
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 10;

                if (name.Trim() != "")
                {
                    sql += " and F_OptName like '%" + name.Trim() + "%' ";
                }
                if (time.Trim() != "")
                {
                    sql += " and CONVERT(varchar , F_OptOn, 120)>=CONVERT(varchar , '" + time.Trim() + " 00:00:01', 120) and CONVERT(varchar , F_OptOn, 120)<=CONVERT(varchar , '" + time.Trim() + " 23:59:59', 120) ";
                }

                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_CallOptLogs> pageModel = new Model.PageData<Model.T_Sys_CallOptLogs>();

                dt = BLL.PagerBLL.GetListPager(
                    "T_Sys_CallOptLogs",
                    "F_Id",
                    "*",
                    sql,
                    "ORDER BY F_Id desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                System.Collections.Generic.List<Model.T_Sys_CallOptLogs> modelList = new BLL.T_Sys_CallOptLogs().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_CallOptLogs>));

                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 = CommonRequest.GetQueryString("arrid");
            if (arr.IndexOf(",") == -1)
            {
                res = new BLL.T_Sys_CallOptLogs().Delete(Convert.ToInt32(arr));
            }
            else
            {
                res = new BLL.T_Sys_CallOptLogs().DeleteList(arr);
            }


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

        #endregion

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