using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Business;
using PTMedicalInsurance.Forms;
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.ReadCard
{
    class ShowPatInfo : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            //展示信息界面
            JObject joOutparam = (JObject)input.DeepClone();
            JObject joOutput = JObject.Parse(JsonHelper.getDestValue(joOutparam, "output"));
            //编码转换
            JArray jaConvertCode = new JArray();
            JObject joConvertCodeParam = new JObject();
            joConvertCodeParam.Add("hospitalDr", Global.inf.hospitalDr);
            joConvertCodeParam.Add("interfaceDr", Global.inf.interfaceDr);
            joConvertCodeParam.Add("output", joOutput);
            jaConvertCode.Add(joConvertCodeParam);
            JObject joConvertCode = new JObject();
            joConvertCode.Add("code", "09010054");
            joConvertCode.Add("params", jaConvertCode);
            InvokeHelper invoker = new InvokeHelper();
            JObject joConvertCodeRtn = invoker.invokeInsuService(joConvertCode.ToString(), "患者信息编码转换");

            PatientInsuInfo frmPatientInfo = new PatientInsuInfo(joConvertCodeRtn);
            //相关信息转换到登记面板
            try
            {
                if (frmPatientInfo.ShowDialog() == DialogResult.OK)
                {
                    //原来用的是转换后的
                    //frmPatientInfo.joSelectedInsuInfo.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    //frmPatientInfo.joSelectedInsuInfo.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    //joOutparam.Property("output").AddBeforeSelf(new JProperty("selectedInsuInfo", frmPatientInfo.joSelectedInsuInfo));

                    //现在用转换之前的
                    JArray jaInsuInfo = JArray.Parse(JsonHelper.getDestValue(joOutput, "insuinfo"));
                    JArray jaIdInfo = JArray.Parse(JsonHelper.getDestValue(joOutput, "idetinfo"));
                    JObject joSelectedInsuInfo = (JObject)jaInsuInfo[frmPatientInfo.insuInfoIndex];
                    JObject joSelectedIdInfo;
                    if (jaIdInfo.Count != 0)
                    {
                        joSelectedIdInfo = (JObject)jaIdInfo[frmPatientInfo.idInfoIndex];
                    }
                    else
                    {
                        joSelectedIdInfo = new JObject();
                    }
                    joSelectedInsuInfo.Add("mdtrt_cert_type", Global.pat.mdtrtcertType);
                    joSelectedInsuInfo.Add("mdtrt_cert_no", Global.pat.mdtrtcertNO);
                    joOutparam.Property("output").AddBeforeSelf(new JProperty("selectedInsuInfo", joSelectedInsuInfo));
                    joOutparam.Property("output").AddBeforeSelf(new JProperty("selectedIdInfo", joSelectedInsuInfo));

                    Global.pat.balc = JsonHelper.getDestValue(joSelectedInsuInfo, "balc");
                    Global.pat.insuType = JsonHelper.getDestValue(joSelectedInsuInfo, "insutype");
                    Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joSelectedInsuInfo, "insuplc_admdvs");

                    outParam = joOutparam.ToString();

                    return Success();
                }
                else
                {
                    outParam = JsonHelper.setExceptionJson(-1, "门诊读卡", "收款员取消读卡").ToString();
                    return Exception(-1,"",outParam);
                }
            }
            catch (Exception e)
            {
                outParam = JsonHelper.setExceptionJson(-1, "患者参保信息展示", e.Message).ToString();
                return Exception(-1, "", outParam);
            }
        }
    }
}
