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
{
    class OPRegProcess : AbstractProcess
    {

        /// <summary>
        /// 门诊挂号
        /// </summary>
        /// <param name="joPatient"></param>
        /// <returns></returns>
        public JObject OutpatientReg(JObject joPatient)
        {

            //医保挂号
            JObject jo2201Inpar = JObject.Parse(JsonHelper.getDestProperty(joPatient, "data"));

            JObject jo2201Rtn = invoker.invokeCenterService(TradeEnum.OutpatientRegistration, jo2201Inpar);
            string errMsg = "";
            if (JsonHelper.parseCenterRtnValue(jo2201Rtn, out errMsg) != 0)
            {
                return JsonHelper.setExceptionJson(-1, "医保挂号", errMsg);
            }
            else
            {
                Global.pat.mdtrtID = JsonHelper.getDestValue(jo2201Rtn, "output.data.mdtrt_id");
                string outParam = "";
                //插入患者本次就诊用的参保信息
                if (hBus.insertPatCurInsuInfo(joPatient.ToString(), out outParam) != 0)
                {
                    if (DialogResult.Yes != MessageBox.Show(outParam, " 是否继续？ ", MessageBoxButtons.YesNo))
                    {
                        return JsonHelper.setExceptionJson(-1, "插入患者本次就诊用的参保信息，请联系管理员！是否继续？", outParam);
                    }
                }
            }
            return jo2201Rtn;
        }


        /// <summary>
        /// 挂号成功后返回医保的就诊Id(mdtrt_id)
        /// </summary>
        /// <param name="joPatient"></param>
        /// <returns></returns>
        public override CallResult Process(JObject joPatient)
        {
            JObject jo2201Inpar_plain = JObject.Parse(JsonHelper.getDestProperty(joPatient, "data"));
            jo2201Inpar_plain = JObject.Parse(JsonHelper.setCenterInpar("2201", jo2201Inpar_plain));
            JObject jo2201Rtn = OutpatientReg(joPatient);

            //此处先取到就诊信息上传的入参，只要挂号成功就保存到云HIS后台（保存后台需要用到2203入参）
            JObject jo2203Inpar = new JObject();
            JObject jo2203Inpar_plain = new JObject();
            jo2203Inpar.Add("mdtrtinfo", JObject.Parse(JsonHelper.getDestValue(joPatient, "mdtrtinfo")));
            jo2203Inpar.Add("diseinfo", JArray.Parse(JsonHelper.getDestValue(joPatient, "diseinfo")));

            jo2203Inpar["mdtrtinfo"]["mdtrt_id"] = Global.pat.mdtrtID;
            jo2203Inpar_plain = JObject.Parse(JsonHelper.setCenterInpar_plain("2203A", jo2203Inpar));
            jo2203Inpar = JObject.Parse(JsonHelper.setCenterInpar("2203A", jo2203Inpar));

            //门诊登记信息，插入云医保平台
            jo2201Rtn.Add("validflag", 1);
            jo2201Rtn.Add("regstate", 1);
            jo2201Rtn.Add("type", 3);

            if (mIS.saveOutpatRegisterInfo(jo2201Inpar_plain, jo2201Rtn, jo2203Inpar_plain, out outParam) != 0)
            {
                return Error(-1, "中心登记成功但医保平台保存失败，请联系管理员！");
            }
            // 返回病人信息
            outParam = joPatient.ToString();
            return Success();
        }
    }
}
