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 InProcessHandler : AbstractProcess
    {
        public override CallResult Process(JObject input)
        {
            #region【门诊预结算2206前调用3102医保明细审核事中服务】     

            if (Global.curEvt.ext.isOpenAnalysis)
            {
                //1.获取3102入参报文
                if (hIS.GetInsuPatInfo("2", Global.pat, out outParam) != 0)
                {
                    outParam = JsonHelper.setExceptionJson(-1, "获取3102医保明细审核事中分析服务入参失败：", outParam).ToString();
                    return Exception(-1, "", outParam);
                }
                else
                {
                    //2.调用医保3102明细审核事前分析服务
                    JObject joInput = new JObject();
                    //joInput.Add("data", JObject.Parse(outParam));
                    joInput = JObject.Parse(outParam);

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

            return Success();
            #endregion
        }
    }
}
