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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //取得处事类型
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getitemlist":
                    context.Response.Write(getitemlist(context));
                    break;
                case "getselectitemlist":
                    context.Response.Write(getselectitemlist(context));
                    break;
                case "add":
                    context.Response.Write(Add(context));
                    break;
                case "addall":
                    context.Response.Write(AddAll(context));
                    break;
                case "modify":
                    context.Response.Write(Modify(context));
                    break;
                case "move":
                    context.Response.Write(Move(context));
                    break;
                case "delete":
                    context.Response.Write(Delete(context));
                    break;
                case "deleteall":
                    context.Response.Write(DeleteAll(context));
                    break;
            }
        }

        #region 删除数据

        private string DeleteAll(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                string[] arrid = arr.Split(',');
                foreach (string itemid in arrid)
                {
                    if (new BLL.T_Call_PageField().Delete(Convert.ToInt32(itemid)))
                    {
                        res = "success";
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 添加数据

        private string AddAll(HttpContext context)
        {
            string res = "error";
            try
            {
                string arr = context.Request.Params["arrid"];
                string[] arrid = arr.Split(',');
                string sort = "0";
                int intsort = 0;
                try
                {
                    sort = context.Request.Params["sort"];
                    intsort = Convert.ToInt32(sort) + 1;
                }
                catch
                { }
                foreach (string fieldid in arrid)
                {
                    Model.T_Form_EntityAttribute fieldmodel = new BLL.T_Form_EntityAttribute().GetModel(Convert.ToInt32(fieldid));
                    if (fieldmodel != null)
                    {
                        Model.T_Call_PageField itemmodel = new Model.T_Call_PageField();
                        itemmodel.F_Name = fieldmodel.F_Name;
                        itemmodel.F_DBFieldlName = fieldmodel.F_DBFieldlName;

                        int taskid = 0;
                        try
                        {
                            taskid = Convert.ToInt32(context.Request.Params["taskid"]);
                        }
                        catch
                        { }
                        itemmodel.F_TaskId = taskid;
                        itemmodel.F_Sort = intsort;
                        intsort++;

                        try
                        {
                            itemmodel.F_FeildId = Convert.ToInt32(fieldid);
                        }
                        catch
                        { }
                        int itemid = new BLL.T_Call_PageField().Add(itemmodel);
                        if (itemid > 0)
                        {
                            res = "success";
                        }
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 添加数据

        private string Add(HttpContext context)
        {
            string res = "error";
            try
            {
                Model.T_Form_EntityAttribute fieldmodel = new BLL.T_Form_EntityAttribute().GetModel(Convert.ToInt32(context.Request.Params["itemid"]));
                if (fieldmodel != null)
                {
                    Model.T_Call_PageField itemmodel = new Model.T_Call_PageField();
                    itemmodel.F_Name = fieldmodel.F_Name;
                    itemmodel.F_DBFieldlName = fieldmodel.F_DBFieldlName;

                    int taskid = 0;
                    try
                    {
                        taskid = Convert.ToInt32(context.Request.Params["taskid"]);
                    }
                    catch
                    { }
                    itemmodel.F_TaskId = taskid;
                    string sort = "0";
                    try
                    {
                        sort = context.Request.Params["sort"];
                        if (sort.Trim() != "")
                        {
                            itemmodel.F_Sort = Convert.ToInt32(sort) + 1;
                        }
                    }
                    catch
                    { }
                    string F_FeildId = "0";
                    try
                    {
                        F_FeildId = context.Request.Params["itemid"];
                        if (F_FeildId.Trim() != "")
                        {
                            itemmodel.F_FeildId = Convert.ToInt32(F_FeildId);
                        }
                    }
                    catch
                    { }
                    int itemid = new BLL.T_Call_PageField().Add(itemmodel);
                    if (itemid > 0)
                    {
                        res = "success";
                    }
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 修改数据

        private string Modify(HttpContext context)
        {
            string res = "error";
            try
            {
                Model.T_Call_PageField model = new BLL.T_Call_PageField().GetModel(Convert.ToInt32(context.Request.Params["itemid"]));
                model.F_Name = context.Request.Params["itemvalue"];
                if (new BLL.T_Call_PageField().Update(model))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 移动排序

        private string Move(HttpContext context)
        {
            string res = "error";
            DataTable dt = new DataTable();
            try
            {
                //ajax/questionitems.ashx?action=" + optaction + "&questionid=" + id + "&itemid=" + itemid+"&sort="+sort+"&movetype="+type
                string sql = "";
                string order = "";
                int sort = Convert.ToInt32(context.Request.Params["sort"]);
                int itemid = Convert.ToInt32(context.Request.Params["itemid"]);
                int newsort = 0;
                int newitemid = 0;
                if (context.Request.Params["movetype"] == "-1")
                {
                    sql = " F_Sort<" + sort.ToString();
                    order = " order by F_Sort desc";
                }
                else
                {
                    sql = " F_Sort>" + sort.ToString();
                    order = " order by F_Sort ";
                }
                dt = new BLL.T_Call_PageField().GetList(sql + " " + order).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    newitemid = Convert.ToInt32(dt.Rows[0]["F_Id"].ToString());
                    newsort = Convert.ToInt32(dt.Rows[0]["F_Sort"].ToString());
                }
                Model.T_Call_PageField newmodel = new BLL.T_Call_PageField().GetModel(newitemid);
                newmodel.F_Sort = sort;
                if (new BLL.T_Call_PageField().Update(newmodel))
                {
                    res = "success";
                }
                Model.T_Call_PageField model = new BLL.T_Call_PageField().GetModel(itemid);
                model.F_Sort = newsort;
                if (new BLL.T_Call_PageField().Update(model))
                {
                    res = "success";
                }
            }
            catch
            { }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return res;
        }

        #endregion

        #region 删除数据

        private string Delete(HttpContext context)
        {
            string res = "error";
            try
            {
                if (new BLL.T_Call_PageField().Delete(Convert.ToInt32(context.Request.Params["itemid"])))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        #endregion

        #region 获取数据
        private string getitemlist(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 sql = " and F_DBTableName='T_Call_PageField' ";
                string taskid = "0";
                try
                {
                    taskid = context.Request.Params["taskid"];
                    if (taskid.Trim() != "")
                    {
                        sql += " and F_AttributeId not in (SELECT [F_FeildId] FROM [T_Call_PageField] where [F_TaskId]=" + taskid + ") ";
                    }
                }
                catch
                { }
                string dbtype = "";
                try
                {
                    dbtype = context.Request.Params["dbtype"];
                    if (dbtype.Trim() != "")
                    {
                        sql += " and F_DBTypeName='" + dbtype + "' ";
                    }
                }
                catch
                { }
                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_Form_EntityAttribute> pageModel = new Model.PageData<Model.T_Form_EntityAttribute>();
                dt = BLL.PagerBLL.GetListPager(
                    "T_Form_EntityAttribute",
                    "F_AttributeId",
                    "*",
                    sql,
                    "ORDER BY F_ColumnNumber ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.T_Form_EntityAttribute> modelList = new BLL.T_Form_EntityAttribute().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_Form_EntityAttribute>));
                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 getselectitemlist(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 sql = " ";
                string taskid = "0";
                try
                {
                    taskid = context.Request.Params["taskid"];
                    if (taskid.Trim() != "")
                    {
                        sql += " and [F_TaskId]=" + taskid + " ";
                    }
                    else
                    {
                        sql += " and [F_TaskId]=0 ";
                    }
                }
                catch
                { }
                if (strpageindex.Trim() != "")
                {
                    try
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }
                    catch
                    { }
                }
                if (strpagesize.Trim() != "")
                {
                    try
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    catch
                    { }
                }
                int recordCount = 0;
                Model.PageData<Model.vw_Call_PageField> pageModel = new Model.PageData<Model.vw_Call_PageField>();
                dt = BLL.PagerBLL.GetListPager(
                    "vw_Call_PageField",
                    "F_Id",
                    "*",
                    sql,
                    "ORDER BY F_Sort ",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);
                System.Collections.Generic.List<Model.vw_Call_PageField> modelList = new BLL.T_Call_PageField().DataTableToListView(dt);
                pageModel.Rows = modelList;
                pageModel.Total = recordCount;
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Model.PageData<Model.vw_Call_PageField>));
                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

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