using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static PTMedicalInsurance.Helper.DropHelper;

namespace PTMedicalInsurance.Forms
{
    public partial class SlowSpecRecord : Form
    {
        JObject userMsg = (JObject)JsonConvert.DeserializeObject("");
        DropHelper drop = new DropHelper();
        IrisDataHelper irisData = new IrisDataHelper();
        InvokeHelper invoker = new InvokeHelper();
        public SlowSpecRecord()
        {
            InitializeComponent();
        }

        private void SlowSpecRecord_Load(object sender, EventArgs e)
        {
            //初始化身份选择
            Dictionary<string, string> data = drop.CredenecType();
            //ComBoxChoose(data, cbx_CardType, false);
            //cbx_CardType.SelectedIndex = 0;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            JObject joRtn = invoker.invokeInsuService(JsonHelper.setCenterInpar("2503", ToJson("data")), "人员慢特病备案");
            MessageBox.Show(joRtn.ToString());
            if (joRtn.ToString().IndexOf("errorCode") != -1)
            {
                string errorCode = joRtn["errorCode"].ToString();
                if (errorCode == "-1")
                {
                    MessageBox.Show(joRtn["errorMessage"].ToString());
                    return;
                }
            }
            string code = joRtn["code"].ToString();
            string csbCode = joRtn["csbCode"].ToString();
            string message = joRtn["message"].ToString();

            if (code == "200" && message == "SUCCESS")      //调用IRIS
            {
                string infcode = joRtn["infcode"].ToString();
                if (infcode == "-1") MessageBox.Show("慢特病备案失败,原因是:" + joRtn.ToString());
                if (infcode == "200")//成功时调用
                {
                    string dcla = joRtn["trt_dcla_detl_sn"].ToString();
                    //提交IRIS
                    JObject toJson = (JObject)JsonConvert.DeserializeObject(ToJson("params", dcla));
                    toJson.Add(new JProperty("code", "02030004"));
                    //toJson.Add(new JProperty("session", irisData.AddUserJson()));
                    toJson.Add(new JProperty("session", userMsg));
                    JObject rtn = invoker.invokeIrisService(toJson.ToString().Replace("\r\n", ""), "人员定点备案");
                    string errorCode = rtn["errorCode"].ToString().Trim();
                    if (errorCode == "0")
                    {
                        MessageBox.Show("撤销操作已完成!");
                    }
                    else
                    {
                        MessageBox.Show("写入IRIS失败!");
                    }
                }
            }
        }
        /// <summary>
        /// 组装json
        /// </summary>
        /// <param name="parmName">传入Key值</param>
        /// <param name="dclaRtn">流水号</param>
        /// <returns></returns>
        public string ToJson(string parmName, string dclaRtn = "")
        {
            System.Text.StringBuilder json = new System.Text.StringBuilder("{\"" + parmName + "\":[{", 1000);
            json.AppendFormat("\"psn_no\":\"{0}\",", tbx_No.Text.Trim());
            var cardType = (ComboBoxItem)cbx_CardType.Items[cbx_CardType.SelectedIndex == -1 ? 0 : cbx_CardType.SelectedIndex];
            json.AppendFormat("\"psn_cert_type\":\"{0}\",", cardType.Value);
            json.AppendFormat("\"certno\":\"{0}\",", tbx_IdCard.Text.Trim());
            json.AppendFormat("\"psn_name\":\"{0}\",", tbx_Name.Text.Trim());
            json.AppendFormat("\"insu_admdvs\":\"{0}\",", tbx_AddParentNo.Text.Trim());
            json.AppendFormat("\"insutype\":\"{0}\",", tbx_InsuType.Text.Trim());
            var slow = (ComboBoxItem)cbx_Slow.Items[cbx_Slow.SelectedIndex == -1 ? 0 : cbx_Slow.SelectedIndex];
            json.AppendFormat("\"opsp_dise_code\":\"{0}\",", slow.Value);
            json.AppendFormat("\"opsp_dise_name\":\"{0}\",", slow.Text.Trim());
            json.AppendFormat("\"certno\":\"{0}\",", tbx_IdCard.Text.Trim());
            json.AppendFormat("\"tel\":\"{0}\",", tbx_Phone.Text.Trim());
            json.AppendFormat("\"addr\":\"{0}\",", tbx_address.Text.Trim());
            json.AppendFormat("\"insu_optins\":\"{0}\",", tbx_AddNo.Text.Trim());
            var medInst = (ComboBoxItem)cbx_MedInst.Items[cbx_MedInst.SelectedIndex == -1 ? 0 : cbx_MedInst.SelectedIndex];
            json.AppendFormat("\"ide_fixmedins_no\":\"{0}\",", medInst.Value);
            json.AppendFormat("\"ide_fixmedins_name\":\"{0}\",", medInst.Text.Trim());
            json.AppendFormat("\"hosp_ide_date\":\"{0}\",", dpt_HospDate.Text.Trim());
            json.AppendFormat("\"diag_dr_codg\":\"{0}\",", tbx_DoctorNo.Text.Trim());
            json.AppendFormat("\"diag_dr_name\":\"{0}\",", tbx_Doctor.Text.Trim());
            json.AppendFormat("\"begndate\":\"{0}\",", dpk_StaDate.Text.Trim());
            json.AppendFormat("\"enddate\":\"{0}\",", dpk_EndDate.Text.Trim());
            json.AppendFormat("\"biz_type_flag\":\"{0}\",", tbx_BizAppy.Text.Trim());
            json.AppendFormat("\"med_trt_type\":\"{0}\",", tbx_MedType.Text.Trim());
            json.AppendFormat("\"brdy\":\"{0}\",", dpk_Birth.Text.Trim());
            json.AppendFormat("\"med_trt_type_name\":\"{0}\",", tbx_Dise.Text.Trim());
            json.AppendFormat("\"cond_abst_diag\":\"{0}\",", rtb_Condition.Text.Trim());
            json.AppendFormat("\"trt_schm\":\"{0}\",", rtb_Treatment.Text.Trim());
            json.AppendFormat("\"hosp_opnn\":\"{0}\",", tbx_HospOpin.Text.Trim());
            json.AppendFormat("\"chfdr\":\"{0}\",", tbx_ChiefDoctor.Text.Trim());
            json.AppendFormat("\"gend\":\"{0}\",", cbx_Sex.Text.Trim());
            json.AppendFormat("\"naty\":\"{0}\",", tbx_Natio.Text.Trim());

            json.AppendFormat("\"emp_name\":\"{0}\",", tbx_comp.Text.Trim());
            json.AppendFormat("\"emp_no\":\"{0}\",", tbx_CompNo.Text.Trim());
            json.AppendFormat("\"dept_code\":\"A10\",");
            json.AppendFormat("\"dept_name\":\"眼科\",");
            json.AppendFormat("\"dept_tel\":\"{0}\",", tbx_HospTel.Text.Trim());
            

            //若返回流水号非空,则表示成功,保存到IRIS
            if (dclaRtn != "")
            {
                json.AppendFormat("\"trt_dcla_detl_sn\":\"{0}\",", dclaRtn);//申报流水号
            }

            #region 拼上Iris所需

            json.AppendFormat("\"iresult\":\"\",");        //操作结果 0已上传 9已撤销 
            json.AppendFormat("\"InPut\":\"\",");          //调用入参
            json.AppendFormat("\"OutPut\":\"\",");         //调用反参

            #endregion
            json.AppendFormat("\"psn_type\":\"{0}\"", tbx_perType.Text.Trim());
            json.Append("}]}");
            return json.ToString();
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}