using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.OutPat
{
    class Register : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            JObject joReg = (JObject)input.DeepClone();
            string errMsg;
            //医保挂号
            JObject jo2201Inpar = JObject.Parse(JsonHelper.getDestProperty(joReg, "data"));
            JObject jo2201Inpar_plain = JObject.Parse(JsonHelper.getDestProperty(joReg, "data"));
            jo2201Inpar = JObject.Parse(JsonHelper.setCenterInpar("2201", jo2201Inpar));
            jo2201Inpar_plain = JObject.Parse(JsonHelper.setCenterInpar_plain("2201", jo2201Inpar_plain));
            //此处先取到就诊信息上传的入参，只要挂号成功就保存到云HIS后台（保存后台需要用到2203入参）
            JObject jo2203Inpar = new JObject();
            JObject jo2203Inpar_plain = new JObject();
            jo2203Inpar.Add("mdtrtinfo", JObject.Parse(JsonHelper.getDestValue(joReg, "mdtrtinfo")));
            jo2203Inpar.Add("diseinfo", JArray.Parse(JsonHelper.getDestValue(joReg, "diseinfo")));
            JObject jo2201Rtn = invoker.invokeCenterService("2201", jo2201Inpar);
            if (JsonHelper.parseCenterRtnValue(jo2201Rtn, out errMsg) != 0)
            {
                outParam = JsonHelper.setExceptionJson(-1, "医保挂号", errMsg).ToString();
                return Exception(-1,"",outParam);
            }
            else
            {
                Global.pat.mdtrtID = JsonHelper.getDestValue(jo2201Rtn, "output.data.mdtrt_id");
                string patInfo = JsonHelper.getDestValue(joReg,"patInfo"); //这个估计要设置一个全局变量，当分的过细的时候，如何引用前面节点的局部变量得设计下。
                //插入患者本次就诊用的参保信息
                if (hBus.insertPatCurInsuInfo(patInfo, out outParam) != 0)
                {
                    if (DialogResult.Yes != MessageBox.Show(outParam, " 是否继续？ ", MessageBoxButtons.YesNo))
                    {
                        outParam = JsonHelper.setExceptionJson(-1, "插入患者本次就诊用的参保信息，请联系管理员！是否继续？", outParam).ToString();
                        return Exception(-1, "", outParam);
                    }
                }
                jo2203Inpar["mdtrtinfo"]["mdtrt_id"] = Global.pat.mdtrtID;
                jo2203Inpar_plain = JObject.Parse(JsonHelper.setCenterInpar_plain("2203", jo2203Inpar));
                jo2203Inpar = JObject.Parse(JsonHelper.setCenterInpar("2203", jo2203Inpar));
                //门诊登记信息，插入云医保平台
                jo2201Rtn.Add("validflag", 1);
                jo2201Rtn.Add("regstate", 1);
                jo2201Rtn.Add("type", 3);
                Global.writeLog(jo2201Inpar_plain.ToString());
                if (mIS.saveOutpatRegisterInfo(jo2201Inpar_plain, jo2201Rtn, jo2203Inpar_plain, out errMsg) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "中心登记成功但医保平台保存失败，请联系管理员！", errMsg).ToString();
                    return Exception(-1, "", outParam);
                }

                outParam = input.ToString();
                return Success();
            }
        }
    }
}
