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

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

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "savedata":
                    context.Response.Write(savedata(context));
                    break;
                case "updateHJJG":
                    context.Response.Write(updateHJJG(context));
                    break;
            }
        }

        private string updateHJJG(HttpContext context)
        {
            string res = "error";
            try
            {
                string id = context.Request.Params["id"];
                string resid = context.Request.Params["resid"];
                string name = context.Request.Params["name"];
                if (new BLL.T_Call_TaskTelNum().UpdateHJJG(Convert.ToInt32(id), Convert.ToInt32(resid), name))
                {
                    res = "success";
                }
            }
            catch
            { }
            return res;
        }

        private string savedata(HttpContext context)
        {
            string res = "";
            string type = "0";
            DataTable fielddt = new DataTable();
            DataTable questiondt = new DataTable();
            try
            {
                string id = context.Request.Params["id"];
                string taskid = context.Request.Params["taskid"];
                string pagerid = context.Request.Params["pagerid"];
                string userid = context.Request.Params["userid"];
                string updatesql = "";


                fielddt = new BLL.T_Call_PageField().GetList(" [F_TaskId]=" + taskid + " order by F_Sort ").Tables[0];
                int fieldcount=fielddt.Rows.Count;
                for (int i = 0; i < fieldcount; i++)
                {
                    if (i == (fieldcount - 1))
                    {
                        updatesql += "" + fielddt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "='" + context.Request.Form["txt_field_" + fielddt.Rows[i]["F_Id"].ToString().Trim()].ToString().Replace(",", "").Replace("'", "") + "'";
                    }
                    else
                    {
                        updatesql += "" + fielddt.Rows[i]["F_DBFieldlName"].ToString().Trim() + "='" + context.Request.Form["txt_field_" + fielddt.Rows[i]["F_Id"].ToString().Trim()].ToString().Replace(",", "").Replace("'", "") + "',";
                    }
                }

                if (updatesql.Trim() != "")
                {
                    //保存呼叫结果

                    updatesql += ", F_HJJGId=" + context.Request.Form["txtHJJGId"] + ",F_HJJGName='" + context.Request.Form["txtHJJGName"] + "' ";
                }
                else
                {
                    updatesql += " F_HJJGId=" + context.Request.Form["txtHJJGId"] + ",F_HJJGName='" + context.Request.Form["txtHJJGName"] + "' ";
                }

                int r=DBUtility.DbHelperSQL.ExecuteSql("update [T_Call_TaskTelNum] set " + updatesql + " where [F_Id]=" + id);
                if (r > 0)
                {
                    //保存问卷
                    Model.T_Call_TaskTelNum model = new Model.T_Call_TaskTelNum();
                    model.F_Id = Convert.ToInt32(id);
                    string arrquestionid = "";
                    string arrquestionres = "";

                    string itemtable = "T_Ask_PagerItems";
                    string questionidname = "F_QuestionId";
                    string sortname = "F_Sort";
                    string itemidname = "F_ItemId";
                    string key = "T_Ask_PagerItems.F_ItemId";
                    string sql = " and [T_Ask_PagerItems].[F_PagerId]=" + pagerid + " ";
                    int recordCount = 0;
                    questiondt = BLL.PagerBLL.GetListPager(
                    "vw_Ask_Question LEFT OUTER JOIN " + itemtable + " ON vw_Ask_Question.F_QuestionId = " + itemtable + "." + questionidname + " ",
                    key,
                    "vw_Ask_Question.*," + itemtable + "." + sortname + " as F_Sort," + itemtable + "." + itemidname + " as F_ItemId",
                    " and F_DeleteFlag=0 " + sql,
                    "ORDER BY " + itemtable + "." + sortname + " ",
                    100,
                    1,
                    true,
                    out recordCount);
                    int qcount = questiondt.Rows.Count;
                    for (int n = 0; n < qcount; n++)
                    {
                        string questionid = questiondt.Rows[n]["F_QuestionId"].ToString().Trim();
                        string questiontype = questiondt.Rows[n]["F_Type"].ToString().Trim();
                        if (n == (qcount - 1))
                        {
                            arrquestionid += questionid + "";
                        }
                        else
                        {
                            arrquestionid += questionid + ",";
                        }
                        if (questiontype == "1")
                        {
                            arrquestionres += context.Request.Form["txtF_Title_" + questionid].ToString().Replace(",", ";") + ",";
                        }
                        else
                        {
                            string qres = "";
                            DataTable dt = new DataTable();
                            try
                            {
                                dt = new BLL.T_Ask_QuestionItems().GetList(" F_QuestionId=" + questionid.Trim() + " order by F_Sort ").Tables[0];
                                if (dt != null)
                                {
                                    for (int i = 0; i < dt.Rows.Count; i++)
                                    {
                                        if (questiontype == "2")
                                        {
                                            qres = context.Request.Form["radio_name_" + questionid].Trim();
                                        }
                                        if (questiontype == "3")
                                        {
                                            string chk = context.Request.Form["check_name_" + questionid.Trim() + "_" + dt.Rows[i]["F_ItemId"].ToString().Trim()];
                                            if (chk != "null" && chk != null)
                                            {
                                                qres += dt.Rows[i]["F_ItemId"].ToString().Trim() + "|";
                                            }
                                        }
                                    }
                                    qres += "!@#";
                                    qres = qres.Replace("|!@#", "").Replace("!@#", "");
                                }
                            }
                            catch
                            {
                            }
                            finally
                            {
                                dt.Clear();
                                dt.Dispose();
                            }
                            arrquestionres += qres + ",";
                        }
                    }

                    arrquestionres += "!@#";
                    arrquestionres = arrquestionres.Replace(",!@#", "").Replace("!@#", "");

                    model.F_AskRes = arrquestionid;
                    model.F_AskInfo = arrquestionres;

                    if (new BLL.T_Call_TaskTelNum().UpdateAsk(model))
                    {

                        res = "保存成功";
                        type = "1";
                    }
                    else
                    {
                        res = "保存问卷信息失败";
                    }
                }
                else
                {
                    res = "更新电话数据信息失败";
                }
            }
            catch (Exception ex)
            {
                res = ex.ToString();
            }
            finally
            {
                questiondt.Clear();
                questiondt.Dispose();
                fielddt.Clear();
                fielddt.Dispose();
            }

            return "{msg:" + type + ",info:\"" + res + "\"}";
        }

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