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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            string ArrId = CommonRequest.GetQueryString("arrid");
            string TypeId = CommonRequest.GetQueryString("TypeId");
            switch (action)
            {
                case "getTypeList":
                    context.Response.Write(LoadTypeList(context));
                    break;
                case "delete":
                    context.Response.Write(Delete(context,ArrId));
                    break;
                case "EditValue":
                    context.Response.Write(EditSingType(context, TypeId));
                    break;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="SingtypeId"></param>
        /// <returns></returns>
        public string EditSingType(HttpContext context,string SingtypeId) {
            Model.T_Sms_SmsModel smsModelList = new Model.T_Sms_SmsModel();
            string str = "";
            if (!string.IsNullOrEmpty(SingtypeId))
            {
                smsModelList = new BLL.T_Sms_SmsModel().GetModel(Convert.ToInt32(SingtypeId));
                str = smsModelList.ModelId.ToString() + "," + smsModelList.Name + "," + smsModelList.Contents + "," + smsModelList.Remark;
            }
            return str.Trim();
        }



        /// <summary>
        /// 获取当前的类型列表
        /// </summary>
        /// <returns></returns>
        public string LoadTypeList(HttpContext context) {
            string res = "";
            DataTable dt = new DataTable();
            string sql = " ";
            try
            {
             
                string strpageindex = context.Request.Params["page"];
                int pageindex = 1;
                string strpagesize = context.Request.Params["pagesize"];
                int pagesize = 5;
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }

                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }

                sql += " and State=0";
                int recordCount = 0;

                Model.PageData<Model.T_Sms_SmsModel> pageModel = new Model.PageData<Model.T_Sms_SmsModel>();

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

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

                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;
        }


        /// <summary>
        /// 删除当前的短信类型列表
        /// </summary>
        /// <returns></returns>
        public string Delete(HttpContext context,string arrId) {

            int res = 0;
            string trs = "";
            if (!string.IsNullOrEmpty(arrId))
            {
                res = DBUtility.DbHelperSQL.ExecuteSql("  update T_Sms_SmsModel set State=1 where ModelId in(" + arrId.Trim() + ") ");

            }
            if (res > 0)
            {
                trs = "success";
            }
            else {
                trs = "field";
            }
            return trs;
        }

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