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

namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.OutPat
{
    class PreWarning : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            #region【门诊就诊信息上传成功后调用3101事前服务】    
            //1.获取3101入参报文
            if (Global.curEvt.ext.isOpenAnalysis)
            {
                if (hIS.GetInsuPatInfo("1", Global.pat, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "获取3101医保明细审核事前分析服务入参失败：", outParam).ToString();
                    return Exception(-1, "", outParam);
                }
                else
                {
                    //2.调用医保3101明细审核事前分析服务
                    JObject joInput = new JObject();
                    //joInput.Add("data", JObject.Parse(outParam));
                    joInput = JObject.Parse(outParam);

                    JObject jo3101Rtn = invoker.invokeCenterService("3101", JsonHelper.setCenterInpar("3101", joInput.ToString().Replace("\r\n", ""))); //.Replace(" ", "")
                    if (JsonHelper.parseCenterRtnValue(jo3101Rtn, out errMsg) != 0)
                    {
                        outParam = JsonHelper.setExceptionJson(-1, "【3101】医保明细审核事前分析服务调用失败：", errMsg).ToString();
                        return Exception(-1, "", outParam);
                    }
                    else
                    {
                        JArray jaResult = JArray.Parse(JsonHelper.getDestValue(jo3101Rtn, "output.result"));
                        if (jaResult.HasValues)
                        {
                            PreAndInProcessAnalysisForm paipaForm = new PreAndInProcessAnalysisForm(jo3101Rtn);
                            if (paipaForm.ShowDialog() != DialogResult.OK)
                            {
                                errMsg = "事前事中审核不通过，操作员取消!";
                                outParam = JsonHelper.setExceptionJson(-1, "【3101】医保明细审核事前分析服务调用失败：", errMsg).ToString();
                                return Exception(-1, "", outParam);
                            }
                        }

                        

                    }
                }
            }
            else
            {
                outParam = input.ToString();
            }
            return Success();
            #endregion
        }
    }
}
