using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.visit
{
    public class VisithfOptController : BaseController
    {
        public ActionResult AddInfo(string id, string phone, string resid, string hfjg, string hfinfo,
            string visis, string bzscore, string jcyscore, string hjscore, string ztscore, string score, string remark, string qid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                string uid = CurrentUser.UserData.F_UserId.ToString();
                string uname = CurrentUser.UserData.F_UserName;
                string resname = "";
                if (resid != null && resid.Trim() != "")
                {
                    var config = new BLL.T_Sys_DictionaryValue().GetModel(int.Parse(resid));
                    if (config != null)
                        resname = config.F_Name;
                }
                bool hfstate = false;

                string ques = ""; int qqid = 0;
                if (qid != null && qid.Trim() != "")
                {
                    qqid = int.Parse(qid);
                    var config = new BLL.T_Sys_DictionaryValue().GetModel(int.Parse(qid));
                    if (config != null)
                        ques = config.F_Name;
                }
                bool hj = new BLL.T_Vis_Telphone().UpdateHJ(int.Parse(id), Convert.ToInt32(uid), uname, int.Parse(resid), resname);
                if (resname.Equals("正常接通"))
                    hfstate = true;
                bool hf = new BLL.T_Vis_Telphone().UpdateHF(int.Parse(id), hfstate, hfstate, int.Parse(resid), resname, int.Parse(uid), uname,
                                                             bzscore, jcyscore, hjscore, ztscore, score, remark, hfjg, hfinfo, visis, qqid, ques);
                bool bz = new BLL.T_Vis_Telphone().UpdateBZ(int.Parse(id), bzscore);

                if (hj & hf)
                {
                    Model.T_Vis_TelRecords telrModel = new Model.T_Vis_TelRecords();
                    telrModel.F_HFID = int.Parse(id);
                    telrModel.F_HFInfo = "回访结果：" + hfjg + ";回访内容：" + hfinfo + ";是否成功：" + visis + "。";
                    telrModel.F_HFRemark = "班组得分：" + bzscore + ";接车员得分：" + jcyscore + ";环检：" + hjscore
                                            + ";整体：" + ztscore + ";合计：" + score + ";问题客户分类：" + ques + ";备注：" + remark + "。";
                    telrModel.F_HFState = hfstate ? 1 : 0;
                    telrModel.F_HFTime = DateTime.Now;
                    telrModel.F_HFUserId = int.Parse(uid);
                    telrModel.F_HFUserName = uname;
                    telrModel.F_ISCall = true;
                    telrModel.F_ISCallRes = hfstate;

                    telrModel.F_HFTelphone = phone;
                    telrModel.F_CallUserName = uname;
                    telrModel.F_CallUserID = int.Parse(uid);
                    telrModel.F_CallResID = int.Parse(resid);
                    telrModel.F_CallRes = resname;
                    telrModel.F_CallDate = DateTime.Now;

                    new BLL.T_Vis_TelRecords().Add(telrModel);

                    res = Success("保存成功");
                }
                else
                {
                    res = Error("保存信息失败");
                }

            }
            return res;
        }

        //获取回访内容详情
        public ActionResult GetInfo(string telid)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                if (telid != null && telid.Trim() != "")
                {
                    BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
                    Model.T_Vis_Telphone telModel = telBLL.GetModel(int.Parse(telid));
                    if (telModel != null)
                        res = Success("回访详情加载成功", telModel);
                    else
                        res = Error("回访内容不存在");
                }
                else
                    res = Error("参数获取失败");

            }
            return res;
        }
    }
}