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_dx
{
    public class hfOptController : BaseController
    {
        public ActionResult AddInfo(string id, string phone, string resid,
             string vistype, string viscontent, int type, string callcode)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                int uid = CurrentUser.UserData.F_UserId;
                string uname = CurrentUser.UserData.F_UserName;
                string resname = new BLL.T_Sys_DictionaryValue().GetModel(int.Parse(resid)).F_Name;
                bool hfstate = false;

                bool hj = false;int isvis = 0;
                if (resname.Equals("正常接通"))
                {
                    hfstate = true; isvis = 1;
                }
                bool hf = false;
                if (type == 0)
                {
                    hj= new BLL.T_Vis_DXOTelphone().UpdateHJ(int.Parse(id), uid, uname, int.Parse(resid), resname);
                    hf = new BLL.T_Vis_DXOTelphone().UpdateHF(int.Parse(id), hfstate, int.Parse(resid), resname, uid, uname,
                                                                   vistype, viscontent, isvis);
                }
                else
                {
                    hj = new BLL.T_Vis_DXNTelphone().UpdateHJ(int.Parse(id), uid, uname, int.Parse(resid), resname);
                    hf = new BLL.T_Vis_DXNTelphone().UpdateHF(int.Parse(id), hfstate, int.Parse(resid), resname, uid, uname,
                                                                  vistype, viscontent,  isvis);
                }
                if (hj & hf)
                {
                    Model.T_Vis_DXTelRecords telrModel = new Model.T_Vis_DXTelRecords();
                    telrModel.F_HFID = int.Parse(id);
                    telrModel.F_HFType = type;
                    telrModel.F_HFInfo = vistype;
                    telrModel.F_HFRemark = viscontent;
                    telrModel.F_HFState = hfstate ? 1 : 0;
                    telrModel.F_HFTime = DateTime.Now;
                    telrModel.F_HFUserId = uid;
                    telrModel.F_HFUserName = uname;
                    telrModel.F_ISCall = true;
                    telrModel.F_ISCallRes = hfstate;

                    telrModel.F_CallCode = callcode;

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

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

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

            }
            return res;
        }

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

                if (telid != null && telid.Trim() != "")
                {
                    var rmodel = new object();

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

            }
            return res;
        }
    }
}