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

namespace PTMedicalInsurance.Business.Core.SelfServiceMachine.Process.InPat
{
    class ShowDischargeAuditForm : AbstractProcess
    {
        AuditForm auditFrm = new AuditForm();
        private PTMedicalInsurance.Forms.PatientInsuInfo frmPatientInfo;
        private PTMedicalInsurance.Business.Core.Forms.DischargeAudit.AdmissionRegistration admRegForm;
        private PTMedicalInsurance.Business.Core.Forms.DischargeAudit.DischargeRegistration disRegForm;
        private PTMedicalInsurance.Business.Core.Forms.DischargeAudit.OutpatRegistration outpatRegForm;
        private PTMedicalInsurance.Business.Core.Forms.DischargeAudit.GridViewSetter grdSetter = new GridViewSetter();
        private PTMedicalInsurance.Business.Core.Forms.DischargeAudit.FeeDetail feeDetailForm = new FeeDetail();

        private DataTable dtDisease;
        private DataTable dtDiseaseType;
        private DataTable dtInsuranceType;
        private DataTable dtMedicalType;
        private DataTable dtOperation;
        private DataTable dtSettelmentWay;

        JObject joAuditInfo;
        private JArray jaFinalDiagnoses;
        private JArray jaAdmRegAuditDetail; 
        private JArray jaDisRegAuditDetail;
        private JArray jaOutpatRegAuditDetail;
        private JArray jaFeeDetail;
        int admRegAuditDetailCount = 0;
        int disRegAuditDetailCount = 0;
        int outpatRegAuditDetailCount = 0;
        private JObject joHisDiagnoseInfo;

        private int auditMethod = 1;
        public override CallResult Process(JObject input)
        {
            auditFrm.tcAudit.TabPages.Remove(auditFrm.tpOutpatRegistration);
            if (InitPatienInfoForm(out errMsg) != 0)
            {
                return Exception(errMsg);
            }

            GetDBLKComBoxDatasource();
            if (GetDiagnoses(out errMsg) !=0)
            {
                return Exception(errMsg);
            }
            if (GetDischargeAuditInfo(out errMsg) != 0)
            {
                return Exception(errMsg);
            }
            if (InitAdmissionRegistrationForm(out errMsg) !=0)
            {
                return Exception(errMsg);
            }
            if (InitDischargeRegistrationForm(out errMsg) !=0)
            {
                return Exception(errMsg);
            }
            InitFeeDetailForm();
            
            auditFrm.ConfirmClicked += (sender, e) => { ConfirmClicked(sender, e); };
            auditFrm.ShowDialog();
            var jo = new { auditStatus = auditFrm.auditStatus ,memo= auditFrm.memo};
            outParam = JObject.FromObject(jo).ToString();

            if (auditFrm.auditStatus == 1)
            {
                return Success("审核通过");
            }
            else if (auditFrm.auditStatus == 0)
            {
                return Success("暂未审核");
            }
            else
            {
                return Success("审核被拒");
            }
            
        }


        #region 获取数据源
        //获取登记界面Combox的数据源
        private void GetDBLKComBoxDatasource()
        {
            //获取通用目录
            string sTYML = mIS.GetCommonInterface_Dr(Global.inf.interfaceDr.ToString(), "");
            if (sTYML == "")
                Global.inf.interfaceDr_TY = Global.inf.interfaceDr;
            else
                Global.inf.interfaceDr_TY = int.Parse(sTYML);

            string sqlCondition = " and A.Interface_Dr = '" + Global.inf.interfaceDr_TY.ToString() + "'";


            string sqlStr = "SELECT B.Code,B.Descripts AS Name FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B ON A.ID = B.HBDictionary_Dr  WHERE A.InsuCode = 'insutype'" + sqlCondition;
            dtInsuranceType = grdSetter.GetDBLKComboxTable(sqlStr);

            sqlStr = "select A.Code, A.Name FROM HB_MedInsuDirectory A where A.UseFlag='1' And A.HisType=7" + sqlCondition;
            dtDisease = grdSetter.GetDBLKComboxTable(sqlStr);

            sqlStr = "select A.Code, A.Name FROM HB_MedInsuDirectory A where A.UseFlag='1' And A.HisType=8" + sqlCondition;
            dtOperation = grdSetter.GetDBLKComboxTable(sqlStr);

            sqlStr = "SELECT B.Code,B.Descripts AS Name FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B ON A.ID = B.HBDictionary_Dr  WHERE A.InsuCode = 'psn_setlway'" + sqlCondition;
            dtSettelmentWay = grdSetter.GetDBLKComboxTable(sqlStr);

            sqlStr = "SELECT B.Code,B.Descripts AS Name FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B ON A.ID = B.HBDictionary_Dr  WHERE A.InsuCode = 'med_type'" + sqlCondition;
            dtMedicalType = grdSetter.GetDBLKComboxTable(sqlStr);
        }

        /// <summary>
        /// 获取审核信息，并赋值给全局变量，供其它方法公用
        /// </summary>
        /// <param name="err"></param>
        /// <returns></returns>
        private int GetDischargeAuditInfo(out string err)
        {
            //查询费用核查信息 regType -1表示查询全部明细
            if (mIS.queryDischargeAuditInfo(out err) != 0)
            {
                return -1;
            }
            else
            {
                JObject joRtn = JObject.Parse(err);
                joAuditInfo = JObject.Parse(JsonHelper.getDestValue(joRtn, "result.AuditInfo"));
                jaAdmRegAuditDetail = JArray.Parse(JsonHelper.getDestValue(joRtn, "result.AdmReg"));
                jaDisRegAuditDetail = JArray.Parse(JsonHelper.getDestValue(joRtn, "result.DisReg"));
                jaOutpatRegAuditDetail = JArray.Parse(JsonHelper.getDestValue(joRtn, "result.OutpatReg"));
                admRegAuditDetailCount = int.Parse(JsonHelper.getDestValue(joRtn, "result.AdmRegCount"));
                disRegAuditDetailCount = int.Parse(JsonHelper.getDestValue(joRtn, "result.DisRegCount"));
                outpatRegAuditDetailCount = int.Parse(JsonHelper.getDestValue(joRtn, "result.OutpatRegCount"));
                return 0;
            }
        }

        /// <summary>
        /// 获取His的诊断数据，并进行过滤筛选
        /// </summary>
        /// <param name="err"></param>
        /// <returns></returns>
        private int GetDiagnoses(out string err)
        {
            err = "";
            //调用服务获取门诊诊断信息
            if (hIS.getPatDiagnoses(Global.pat, out err) != 0)
            {
                return -1;
            }
            JObject joHisRtnInfo = JObject.Parse(err);

            //组织登记入参
            JObject joReg = new JObject();
            JArray jaDiagnoses = JArray.Parse(JsonHelper.getDestValue(joHisRtnInfo, "diseinfo"));
            //  对诊断数组进行转换 转换下主要诊断
            JArray jaConvertedDiagnoses = new JArray();
            JArray jaInPatDiagnoses = new JArray();
            JArray jaOutPatDiagnoses = new JArray();
            for (int i = 0; i < jaDiagnoses.Count; i++)
            {

                string diag_type = JsonHelper.getDestValue((JObject)jaDiagnoses[i], "diag_type");
                switch (diag_type)
                {
                    case "1":
                        {
                            jaDiagnoses[i]["diag_type_name"] = new JObject();
                            jaDiagnoses[i]["diag_type_name"] = "西医主要诊断";
                            break;
                        }
                    case "2":
                        {
                            jaDiagnoses[i]["diag_type_name"] = new JObject();
                            jaDiagnoses[i]["diag_type_name"] = "西医其他诊断";
                            break;
                        }
                    case "3":
                        {
                            jaDiagnoses[i]["diag_type_name"] = new JObject();
                            jaDiagnoses[i]["diag_type_name"] = "中医主病诊断";
                            break;
                        }
                    case "4":
                        {
                            jaDiagnoses[i]["diag_type_name"] = new JObject();
                            jaDiagnoses[i]["diag_type_name"] = "中医主证诊断";
                            break;
                        }
                }


                if (jaDiagnoses[i]["diaTypeCode"].ToString() == "ADD") //ADD 入院 OPD 门诊
                {
                    jaInPatDiagnoses.Add(jaDiagnoses[i]);
                }
                if (jaDiagnoses[i]["diaTypeCode"].ToString() == "OPD") //ADD 入院 OPD 门诊
                {
                    jaOutPatDiagnoses.Add(jaDiagnoses[i]);
                }
            }

            if (jaInPatDiagnoses.Count > 0)
            {
                jaConvertedDiagnoses = (JArray)jaInPatDiagnoses.DeepClone();
            }
            else
            {
                jaConvertedDiagnoses = (JArray)jaOutPatDiagnoses.DeepClone();
            }

            for (int i = 0; i < jaConvertedDiagnoses.Count; i++)
            {
                jaConvertedDiagnoses[i]["diag_srt_no"] = i + 1;
            }
            jaFinalDiagnoses = (JArray)jaConvertedDiagnoses.DeepClone();
            joHisDiagnoseInfo = (JObject)joHisRtnInfo.DeepClone();
            return 0;
        }

        /// <summary>
        /// 获取当前界面的审核明细
        /// </summary>
        /// <param name="regType"></param>
        /// <returns></returns>
        private JArray GetAuditDetail(dynamic frm, int regType)
            {
                var details = new List<object>();

                // 添加所有需要的节点
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "insutype",
                    DetailName = "险种类型",
                    Value = frm.insuType,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "med_type",
                    DetailName = "医疗类别",
                    Value = frm.med_type,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "dise_codg",
                    DetailName = "病种编码",
                    Value = frm.diseCodg,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "dise_name",
                    DetailName = "病种名称",
                    Value = frm.diseName,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "oprn_oprt_code",
                    DetailName = "手术操作代码",
                    Value = frm.oprn_oprt_code,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "oprn_oprt_name",
                    DetailName = "手术操作名称",
                    Value = frm.oprn_oprt_name,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "dise_type_code",
                    DetailName = "病种类型",
                    Value = frm.dise_type_code,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "exp_content",
                    DetailName = "字段扩展",
                    Value = frm.dise_type_name,
                    ValueDesc = "",
                    RegType = regType
                });
                details.Add(new
                {
                    AdmID = Global.pat.adm_Dr,
                    DetailCode = "psn_setlway",
                    DetailName = "个人结算方式",
                    Value = frm.psn_setlway,
                    ValueDesc = "",
                    RegType = regType
                });

                // 根据regType决定是否添加最后两个节点
                if (regType == 3)
                {
                    details.Add(new
                    {
                        AdmID = Global.pat.adm_Dr,
                        DetailCode = "acct_used_flag",
                        DetailName = "个人账户使用标志",
                        Value = frm.personAccountUsedFlag ?? "",
                        ValueDesc = "",
                        RegType = regType
                    });
                    details.Add(new
                    {
                        AdmID = Global.pat.adm_Dr,
                        DetailCode = "mid_setl_flag",
                        DetailName = "中途结算标志",
                        Value = frm.halfwaySettlementFlag,
                        ValueDesc = "",
                        RegType = regType
                    });
                }

                // 将列表转换为JArray并返回
                return JArray.FromObject(details);
            }

        private int GetFeeDetail(out string err)
        {
            //调用HIS费用查询信息
            if (hIS.getHisFee(Global.pat, out err) != 0)
            {
                return -1;
            }
            //调用医保平台转换
            JObject joHisFee = JObject.Parse(err);
            if (mIS.convertHisFeeWithInsuCode(joHisFee, out err) != 0)
            {
                return -1;
            }
            else
            {
                jaFeeDetail = JArray.Parse(JsonHelper.getDestValue(JObject.Parse(err), "data"));
                return 0;
            }
        }
        #endregion

        #region 初始化界面
        /// <summary>
        /// 初始化患者信息界面
        /// </summary>
        /// <param name="err"></param>
        /// <returns></returns>
        private int InitPatienInfoForm(out string err)
        {
            //获取全量表数据
            string sqlStr = $" SELECT * FROM BS_MIPatFullInsuInfo WHERE AdmID = {Global.pat.adm_Dr}";
            JObject joRtn = mIS.DynamicQuery(sqlStr, "查询全量信息表");
            if (JsonHelper.parseIrisRtnValue(joRtn, out err) != 0)
            {
                return -1;
            }

            if (JsonHelper.getDestValue(joRtn, "result.total") == "0")
            {
                err = "全量信息表为空";
                return -1;
            }
            //转换患者信息
            JObject joOutparam = JObject.Parse(JsonHelper.getDestValue(joRtn, "result.data[0].FullInfo"));
            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(), "患者信息编码转换");

            frmPatientInfo = new PTMedicalInsurance.Forms.PatientInsuInfo(joConvertCodeRtn, auditFrm);
            frmPatientInfo.pnlBottom.Visible = false;
            auditFrm.AddForm(auditFrm.tpPatientInfo, frmPatientInfo);
            return 0;
        }
        
        /// <summary>
        /// 初始化入院登记
        /// </summary>
        /// <param name="err"></param>
        /// <returns></returns>
        private int InitAdmissionRegistrationForm(out string err)
        {
            admRegForm = new Forms.DischargeAudit.AdmissionRegistration(auditFrm);
            return InitRegistrationForm(2, out err);
        }
        
        /// <summary>
        /// 初始化出院登记界面
        /// </summary>
        /// <param name="err"></param>
        /// <returns></returns>
        private int InitDischargeRegistrationForm(out string err)
        {
            disRegForm = new Forms.DischargeAudit.DischargeRegistration(auditFrm);
            return InitRegistrationForm(3, out err);
        }

        private int InitFeeDetailForm()
        {
            GetFeeDetail(out errMsg);
            feeDetailForm = new Forms.DischargeAudit.FeeDetail(auditFrm);
            feeDetailForm.GetFeeClicked += (sender, e) => GetFeeClicked();
            feeDetailForm.UploadFeeClicked += (sender, e) => UploadFeeClicked();
            auditFrm.AddForm(auditFrm.tpFeeDetail, feeDetailForm);
            return 0;
        }
        #endregion

        #region 初始化界面的细节方法封装
        private void DynamicInit(int regType,out int count, out dynamic frm, out dynamic ja)
        {
            int countTmp;
            dynamic jaTmp;
            switch (regType)
            {
                case 1: //门诊登记 
                    frm = outpatRegForm;
                    ja = jaOutpatRegAuditDetail;
                    count = outpatRegAuditDetailCount;
                    break;
                case 0://通用
                case 2://入院登记 
                default:
                    frm = admRegForm;
                    jaTmp =ja = jaAdmRegAuditDetail;
                    count = countTmp = admRegAuditDetailCount;

                    admRegForm.SaveClicked += (sender, e) => HandleSaveClicked(countTmp, jaTmp, admRegForm, regType);
                    auditFrm.AddForm(auditFrm.tpAdmissionRegistration, admRegForm);
                    break;
                case 3://出院登记 
                    frm = disRegForm;
                    jaTmp =ja = jaDisRegAuditDetail;
                    countTmp =count = disRegAuditDetailCount;
                    disRegForm.SaveClicked += (sender, e) => HandleSaveClicked(countTmp, jaTmp, disRegForm, regType);
                    auditFrm.AddForm(auditFrm.tpDischargeRegistration, disRegForm);
                    break;
            }
        }

        /// <summary>
        /// ///初始化登记界面公共方法
        /// 事件 场景类型 0.窗口门诊 1.窗口住院 2.自助机门诊 3.自助机住院 4.线上支付门诊 5.线上支付住院
        /// 登记类型  0.通用 部分地区门诊出入院都一样 1.门诊登记 2.入院登记 3.出院登记
        /// </summary>
        /// <param name="regType"></param>
        /// <param name="err"></param>
        /// <returns></returns>
        private int InitRegistrationForm(int regType, out string err)
        {

            err = "";
            dynamic frm;
            dynamic ja;
            int count = 0;
            DynamicInit(regType,out count,out frm,out ja);
            frm.DtDisease = dtDisease?.Copy();
            frm.DtDiseaseType = dtDiseaseType?.Copy();
            frm.DtInsuranceType = dtInsuranceType?.Copy();
            frm.DtMedicalType = dtMedicalType?.Copy();
            frm.DtOperation = dtOperation?.Copy();
            frm.DtSettlementWay = dtSettelmentWay?.Copy();

            frm.InitComboxDatasource();
            frm.DtDiagnose = (DataTable)jaFinalDiagnoses.ToObject(typeof(DataTable));
            

            return MatchCombox(frm, count, ja, out err);
        }

        /// <summary>
        /// 根据费用核查表返回的数据，加载并匹配到登记界面
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="count"></param>
        /// <param name="ja"></param>
        /// <param name="err"></param>
        /// <returns></returns>
        private int MatchCombox(dynamic frm, int count, dynamic ja, out string err)
        {
            string insuType = "";
            string medType = "";
            string diseaseCode = "";
            string diseaseType = "";
            string settlementWay = "";
            string operationCode = "";
            string acct_used_flag = "-1";
            string mid_setl_flag = "-1";

            err = "";


            //不存在信息则提示
            frm.IsHintVisible = count == 0;

            //frm.dblkcbxSettelmentWay.Enabled = false;
            foreach (JObject jo in ja)
            {
                if (jo["DetailCode"].ToString() == "insutype")
                {
                    insuType = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "med_type")
                {
                    medType = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "dise_codg")
                {
                    diseaseCode = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "oprn_oprt_code")
                {
                    operationCode = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "dise_type_code")
                {
                    diseaseType = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "mid_setl_flag")
                {
                    mid_setl_flag = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "acct_used_flag")
                {
                    acct_used_flag = jo["Value"].ToString();
                }
                if (jo["DetailCode"].ToString() == "psn_setlway")
                {
                    settlementWay = jo["Value"].ToString();
                }
            }
            //DataTable dt = (DataTable)frm.dblkcbxInsuranceType.DataSource;
            //int selectedIndex = 0;
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    if (dt.Rows[i]["Code"].ToString() == insuType)
            //    {
            //        selectedIndex = i;
            //        frm.dblkcbxInsuranceType.Text = dt.Rows[i]["Name"].ToString();
            //        frm.insuType = dt.Rows[i]["Code"].ToString();
            //    }
            //}

            //DataTable dtMedType = (DataTable)frm.dblkcbxMedicalType.DataSource;
            //for (int i = 0; i < dtMedType.Rows.Count; i++)
            //{
            //    if (dtMedType.Rows[i]["Code"].ToString() == medType)
            //    {
            //        frm.dblkcbxMedicalType.Text = dtMedType.Rows[i]["Name"].ToString();
            //        frm.med_type = dtMedType.Rows[i]["Code"].ToString();
            //    }
            //}
            MatchCombox(frm, "dblkcbxInsuranceType", "insuType", insuType);
            MatchCombox(frm, "dblkcbxMedicalType", "med_type", medType);
            MatchCombox(frm, "dblkcbxSettelmentWay", "psn_setlway", settlementWay);


            //if (frm is DischargeRegistration)
            //{
            //    DataTable dtPsnSettlWay = (DataTable)frm.dblkcbxSettelmentWay.DataSource;
            //    for (int i = 0; i < dtPsnSettlWay.Rows.Count; i++)
            //    {
            //        if (dtPsnSettlWay.Rows[i]["Code"].ToString() == settlementWay)
            //        {
            //            frm.dblkcbxSettelmentWay.Text = dtPsnSettlWay.Rows[i]["Name"].ToString();
            //            frm.psn_setlway = dtPsnSettlWay.Rows[i]["Code"].ToString();
            //        }
            //    }
            //}

            frm.personAccountUsedFlag = acct_used_flag;
            frm.halfwaySettlementFlag = mid_setl_flag;

            //审核状态匹配
            auditFrm.auditStatus = int.Parse(JsonHelper.getDestValue(joAuditInfo, "AuditStatus"));
            auditFrm.memo = JsonHelper.getDestValue(joAuditInfo, "Memo");
            return 0;
        }

        private void MatchCombox(dynamic frm, string comboxName,string propertyName, string value)
        {
            if (!frm.Controls["panel1"].Controls.ContainsKey(comboxName))
            {
                return;
            }
            
            var cbx = frm.Controls["panel1"].Controls[comboxName];
            DataTable dt = (DataTable)cbx.DataSource;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["Code"].ToString() == value)
                {
                    cbx.Text = dt.Rows[i]["Name"].ToString();
                    //frm.insuType = dt.Rows[i]["Code"].ToString();
                    var propertyInfo = frm.GetType().GetProperty(propertyName);
                    if (propertyInfo != null && propertyInfo.CanWrite)
                    {
                        propertyInfo.SetValue(frm, Convert.ChangeType(value, propertyInfo.PropertyType));
                    }
                    else
                    {
                        throw new ArgumentException($"无法设置属性 '{propertyName}' 或该属性不可写。");
                    }
                }
            }

        }

        #endregion

        #region Button事件封装
        /// <summary>
        /// 登记界面的保存事件委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="frm"></param>
        /// <param name="regType"></param>
        private void HandleSaveClicked(int count, dynamic ja, dynamic frm, int regType)
        {
            string errMsg;
            if (SaveAuditInfo(frm, regType, out errMsg) != 0)
            {
                MessageBox.Show(errMsg);
            }
            else 
            {
                GetDischargeAuditInfo(out errMsg);
                DynamicInit(regType, out count, out frm, out ja);
                MatchCombox(frm, count, ja, out errMsg);
                MessageBox.Show("保存成功");
            }
        }

        /// <summary>
        /// 保存审核信息
        /// </summary>
        /// <param name="regType"></param>
        /// <param name="err"></param>
        /// <returns></returns>
        private int SaveAuditInfo(dynamic frm, int regType, out string err)
        {
            err = "";
            //组织入参
            JObject joAudit = new JObject();
            joAudit.Add("HospitalDr", Global.inf.hospitalDr);
            joAudit.Add("InterfaceDr", Global.inf.interfaceDr);
            joAudit.Add("AdmID", Global.pat.adm_Dr);
            joAudit.Add("BillID", Global.pat.billID);
            joAudit.Add("OccurTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            //joAudit.Add("Scene", scene);
            joAudit.Add("AuditStatus", auditFrm.auditStatus);
            joAudit.Add("Memo", auditFrm.rtbMemo.Text);
            joAudit.Add("ValidFlag", 1);
            joAudit.Add("AuditMethod", auditMethod); //审核方式  0.Manual (人工) 1.Automated (自动审核)
            joAudit.Add("AuditorNO", Global.user.ID);
            joAudit.Add("Auditor", Global.user.name);
            joAudit.Add("RegType", regType);
            JArray jaAuditDetail = GetAuditDetail(frm, regType);

            joAudit.Add("AuditDetail", jaAuditDetail);

            return mIS.saveDischargeAudit(joAudit, out err);
            //保存
        }

        private void ConfirmClicked(object sender, EventArgs e)
        {
            if ((auditFrm.auditStatus == -1) && (string.IsNullOrEmpty(auditFrm.memo)))
            {
                MessageBox.Show("审核被拒时审核意见不能为空！");
                return;
            }

            //再次查询审核信息，如果出院意见未有则提示
            if (auditFrm.auditStatus == 1)
            {
                GetDischargeAuditInfo(out errMsg);
                if (jaDisRegAuditDetail.Count == 0)
                {
                    MessageBox.Show("检测到出院登记信息未正确保存，请点击保存或联系关联员！");
                    return;
                }
            }

            //更新费用明细审核表主表
            JObject joAudit = new JObject();
            joAudit.Add("HospitalDr", Global.inf.hospitalDr);
            joAudit.Add("InterfaceDr", Global.inf.interfaceDr);
            joAudit.Add("AdmID", Global.pat.adm_Dr);
            joAudit.Add("BillID", Global.pat.billID);
            joAudit.Add("OccurTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            joAudit.Add("AuditStatus", auditFrm.auditStatus);
            joAudit.Add("AuditMethod", auditMethod); //审核方式  0.Manual (人工) 1.Automated (自动审核)
            joAudit.Add("AuditorNO", Global.user.ID);
            joAudit.Add("Auditor", Global.user.name);
            joAudit.Add("Memo", auditFrm.memo);
            joAudit.Add("ValidFlag", 1);
            if (mIS.saveDischargeAuditMaintable(joAudit, out outParam) != 0)
            {
                MessageBox.Show("出院审核表更新失败！");
                return;
            }
            auditFrm.DialogResult = DialogResult.OK;
        }

        private void GetFeeClicked()
        {
            if (jaFeeDetail == null)
            {
                if(GetFeeDetail(out errMsg) !=0)
                {
                    MessageBox.Show(errMsg);
                }
            }
            else
            {
                feeDetailForm.jaFeeDetail = jaFeeDetail ;
            }
        }

        private void UploadFeeClicked()
        {
            //按指定条数分割后上传，保存，更新
            if (jaFeeDetail == null)
            {
                MessageBox.Show("费用明细为空，请重新获取费用明细或联系管理员！");
                return;
            }
            if (hBus.uploadFeeToCenter("2301", 10, jaFeeDetail, out errMsg) != 0)
            {
                MessageBox.Show(errMsg);
            }
            else
            {
                MessageBox.Show("上传成功");
            }
        }
        #endregion

    }
}
