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)
        {
                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);

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

        //获取回访内容详情
        
        public ActionResult GetInfo(string telid)
        {
            //ActionResult return 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)
            //            return Success("回访详情加载成功", telModel);
            //        else
            //            return Error("回访内容不存在");
            //    }
            //    else
            //        return Error("参数获取失败");

            //}
            //return res;

            if (telid != null && telid.Trim() != "")
            {
                BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
                BLL.T_Vis_Telphone_Mark bllTM = new BLL.T_Vis_Telphone_Mark();
                Model.T_Vis_Telphone telModel = telBLL.GetModel(int.Parse(telid));
                if (telModel == null)
                    return Error("回访内容不存在");
                //判断电话是否标记
                var listmodel = bllTM.GetModelList(" F_CusPhone = '" + telModel.F_CusPhone + "' ").FirstOrDefault();
                var model = new
                {
                    F_TelID = telModel.F_TelID,
                    F_Sheng = telModel.F_Sheng,
                    F_Store = telModel.F_Store,
                    F_Prodate = telModel.F_Prodate,
                    F_Reciver = telModel.F_Reciver,
                    F_License = telModel.F_License,
                    F_Brand = telModel.F_Brand,
                    F_Models = telModel.F_Models,
                    F_CusName = telModel.F_CusName,
                    F_CusTelphone = telModel.F_CusTelphone,
                    F_CusPhone = telModel.F_CusPhone,
                    F_RepType = telModel.F_RepType,
                    F_SurveyDate = telModel.F_SurveyDate,
                    F_TScore = telModel.F_TScore,
                    F_RScore = telModel.F_RScore,
                    F_RingS = telModel.F_RingS,
                    F_WholeS = telModel.F_WholeS,
                    F_TotalS = telModel.F_TotalS,
                    F_Remark = telModel.F_Remark,
                    F_VisResult = telModel.F_VisResult,
                    F_VisInfo = telModel.F_VisInfo,
                    F_VisIS = telModel.F_VisIS,
                    F_RepItem = telModel.F_RepItem,
                    F_TheTeam = telModel.F_TheTeam,
                    F_Elect = telModel.F_Elect,
                    F_Sheetmetal = telModel.F_Sheetmetal,
                    F_Paint = telModel.F_Paint,
                    F_RepUser = telModel.F_RepUser,
                    F_WoNumber = telModel.F_WoNumber,
                    F_NewCus = telModel.F_NewCus,
                    F_InputDate = telModel.F_InputDate,
                    F_InputUserID = telModel.F_InputUserID,
                    F_InputUserName = telModel.F_InputUserName,
                    F_VisUserID = telModel.F_VisUserID,
                    F_VisUserName = telModel.F_VisUserName,
                    F_VisDate = telModel.F_VisDate,
                    F_ISVis = telModel.F_ISVis,
                    F_CallUserID = telModel.F_CallUserID,
                    F_CallUserName = telModel.F_CallUserName,
                    F_CallDate = telModel.F_CallDate,
                    F_ISCall = telModel.F_ISCall,
                    F_CallResID = telModel.F_CallResID,
                    F_Callres= telModel.F_CallRes,
                    F_ISCallres= telModel.F_ISCallRes,
                    F_AllotUserID = telModel.F_AllotUserID,
                    F_AllotUserName = telModel.F_AllotUserName,
                    F_AllotZXID = telModel.F_AllotZXID,
                    F_AllotZXName = telModel.F_AllotZXName,
                    F_ISAllot = telModel.F_ISAllot,
                    F_AllotDate = telModel.F_AllotDate,
                    F_FileID = telModel.F_FileID,
                    F_Extend1 = telModel.F_Extend1,
                    F_Extend2 = telModel.F_Extend2,
                    F_Extend3 = telModel.F_Extend3,
                    F_Extend4 = telModel.F_Extend4,
                    F_Extend5 = telModel.F_Extend5,
                    F_Extend6 = telModel.F_Extend6,
                    F_Extend7 = telModel.F_Extend7,
                    F_Extend8 = telModel.F_Extend8,
                    F_Extend9 = telModel.F_Extend9,
                    F_Extend10 = telModel.F_Extend10,
                    F_Extend11 = telModel.F_Extend11,
                    F_Extend12 = telModel.F_Extend12,
                    F_Extend13 = telModel.F_Extend13,
                    F_Extend14 = telModel.F_Extend14,
                    F_Extend15 = telModel.F_Extend15,
                    F_Extend16 = telModel.F_Extend16,
                    F_Extend17 = telModel.F_Extend17,
                    F_Extend18 = telModel.F_Extend18,
                    F_Extend19 = telModel.F_Extend19,
                    F_Extend20 = telModel.F_Extend20,
                    F_QuestionTypeID = telModel.F_QuestionTypeID,
                    F_QuestionType = telModel.F_QuestionType,
                    isPhoneMark = listmodel != null ? 1 : 0     //1已标记，0没有标记
                };
                return Success("回访详情加载成功", model);
            }
            else
                return Error("参数获取失败");

        }
    }
}