using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Common;
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 InpatientRegistionProcess : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            JObject joReg = new JObject();
            string errMsg = "";
            //显示登记面板
            if (hBus.showInPatRegisterForm(out joReg) == 0)
            {
                //上传给中心
                JObject joRegOut = invoker.invokeCenterService(TradeEnum.InpatientReg, joReg);
                //将中心返回的结果返回HIS,调用HIS服务进行保存
                if (JsonHelper.parseCenterRtnValue(joRegOut, out errMsg) == 0)
                {
                    // 基线版扩展
                    joRegOut.Extend("validflag", 1);
                    joRegOut.Extend("regstate", 1);
                    joRegOut.Extend("type", 1);
                    joRegOut.Extend("ExpContent", "");

                    Global.pat.mdtrtID = JsonHelper.getDestValue(joRegOut, "output.result.mdtrt_id");
                    //插入患者本次就诊用的参保信息
                    if (hBus.insertPatCurInsuInfo(JsonHelper.getDestValue(input, "patientInfo"), out outParam) != 0)
                    {
                        if (DialogResult.Yes == MessageBox.Show(outParam, " 是否继续？ ", MessageBoxButtons.YesNo))
                        {

                        }
                        else
                        {
                            return Exception("插入患者本次就诊用的参保信息，请联系管理员！", outParam);
                        }
                    }

                    //插入医保平台
                    if (mIS.saveInpatRegisterInfo(joReg, joRegOut, out outParam) != 0)
                    {
                        return Exception("云平台保存登记信息", outParam);
                    }
                    //返回登记信息，插入HIS
                    if (hIS.returnRegisterInfo(joReg, joRegOut, out outParam) != 0)
                    {
                        return Exception("HIS平台保存登记信息", outParam);
                    }
                    
                    return IrisReturn("登记成功",null);
                }
                else
                {
                    return Exception("展示登记界面", errMsg);
                }
            }
            else
            {
                return Exception("展示登记界面", joReg.ToString());
            }
        }
    }
}
