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

namespace PTMedicalInsurance.Business
{
    class STA
    {
        //设置业务实例
        CenterBusiness cBus = new CenterBusiness();
        HisMainBusiness hBus = new HisMainBusiness();
        HisIrisServices hIS = new HisIrisServices();
        MIIrisServices mIS = new MIIrisServices();
        InvokeHelper invoker = new InvokeHelper();

        public delegate void callBack(string errMsg);

        public void CallBack(string errMsg)
        {
            MessageBox.Show(errMsg);
        }
        [STAThread]
        public void print(object o)
        {
            string outParam = "", errMsg = "";
            try
            {
                //查询结算表
                string sqlStr = "SELECT * FROM  BS_MedInsuSettlement WHERE billType = 1 and Hospital_Dr=" + Global.inf.hospitalDr
                              + " and Adm_Dr=" + Global.pat.adm_Dr
                              + " and MdtrtID='" + Global.pat.mdtrtID + "'"
                              + " and SettlementID='" + Global.pat.settlID + "'";
                JObject joSql = new JObject();
                joSql.Add("sqlStr", sqlStr);
                JObject joSettlInfo = mIS.QuerySettlementInfo(joSql);
                JArray jaSettlInfo = JArray.Parse(JsonHelper.getDestValue(joSettlInfo, "result.data"));
                if (jaSettlInfo.Count == 0)
                {
                    outParam = JsonHelper.setIrisReturnValue(-1, "未查询到结算数据", null).ToString();
                }
                Global.pat.insuplc_admdvs = JsonHelper.getDestValue(joSettlInfo, "result.data[0].insuplc_admdvs");
                Global.pat.insuType = JsonHelper.getDestValue(joSettlInfo, "result.data[0].InsuranceType");
                Global.pat.medType = JsonHelper.getDestValue(joSettlInfo, "result.data[0].MedicalType");
                Global.pat.psn_type = JsonHelper.getDestValue(joSettlInfo, "result.data[0].PersonType");
                dynamic joTmp = new JObject();
                joTmp.prm_akc190 = Global.pat.mdtrtID;
                joTmp.prm_yab003 = Global.pat.insuplc_admdvs;
                joTmp.prm_aka130 = Global.pat.medType;
                joTmp.prm_akc021 = Global.pat.psn_type;
                joTmp.prm_yka103 = Global.pat.settlID;
                joTmp.prm_ykb065 = Global.pat.insuType;
                joTmp.prm_yabtch = Global.pat.insuplc_admdvs;


                if (invoker.invokeCenterService("44", joTmp) != 0)
                {
                    outParam = JsonHelper.setIrisReturnValue(-1, errMsg, null).ToString();
                }
                else
                {
                    outParam = JsonHelper.setIrisReturnValue(0, "", null).ToString();
                }
                CallBack(outParam);

            }
            catch (Exception ex)
            {
                CallBack(ex.Message);
            }

        }

        [STAThread]
        public void MutualAid(object o)
        {
            string outParam = "", errMsg = "";
            try
            {
                //MutualAid frmMA = new MutualAid();
                //frmMA.WindowState = FormWindowState.Maximized;
                //frmMA.ShowDialog();
                CallBack(outParam);

            }
            catch (Exception ex)
            {
                CallBack(ex.Message);
            }

        }

        [STAThread]
        public void ShowBasicData(object o)
        {
            try
            {
                BasicData bd = new BasicData();
                bd.ShowDialog();

            }
            catch (Exception ex)
            {
                CallBack(ex.Message);
            }

        }
    }

    public class WindowWrapper : System.Windows.Forms.IWin32Window
    {
        public WindowWrapper(IntPtr handle)
        {
            _hwnd = handle;
        }

        public IntPtr Handle
        {
            get { return _hwnd; }
        }

        private IntPtr _hwnd;
    }
}
