using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Business;
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 AnHuiMI.Common;
using PTMedicalInsurance.Variables;
using PTMedicalInsurance.FormSetter;
using FastReport.Utils;

namespace AnHuiMI.Forms
{
    public partial class AssistSelect : Form
    {
        public AssistSelect()
        {
            InitializeComponent();
        }
        HisMainBusiness hBus = new HisMainBusiness();
        private InvokeHelper invoker = new InvokeHelper();
        InsuServices rIS = new InsuServices();
        private void AssistSelect_Load(object sender, EventArgs e)
        {
            uiDatetimePicker1.Text = DateTime.Now.ToString("yyyy-MM-01 00:00:00");
            uiDatetimePicker2.Value = DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMonths(1).AddDays(-1);
        }
        private void uiButton1_Click(object sender, EventArgs e)
        {
            string outParam;
            //打开读卡窗口，操作员选择读卡类型后进行读卡器读卡，再进行1101获取参保信息
            if (hBus.readCard(out outParam) != 0)
            {
                MessageBox.Show(JsonHelper.setExceptionJson(-100, "读卡失败！",  outParam).ToString());
            }
            else
            {
                //展示患者信息界面
                if (hBus.showPatInfo(outParam, out outParam) != 0)
                {
                    MessageBox.Show(JsonHelper.setIrisReturnValue(0, "展示患者信息失败", JObject.Parse(outParam)).ToString());
                }
                else
                {
                    
                }
            }
            uiTextBox1.Text = Global.pat.psn_no;
        }

        private void uiMbyyButton1_Click(object sender, EventArgs e)
        {
            GridViewSetter grdSetter = new GridViewSetter();
            grdSetter.SetMbMedicationHistory(uiDataGridView1);
            JObject jo5205= new JObject();
            jo5205.Add("psn_no", uiTextBox1.Text);//必填
            jo5205.Add("begntime", uiDatetimePicker1.Text);//yyyy-MM-dd HH:mm:ss必填
            jo5205.Add("endtime", uiDatetimePicker2.Text);//yyyy-MM-dd HH:mm:ss
            JObject data5205= new JObject();
            data5205.Add("data", jo5205);
            JObject out5205= invoker.invokeCenterService("5205", JObject.Parse(JsonHelper.setCenterInpar("5205", data5205)));
            DataTable dt = (DataTable)out5205["output"]["feedetail"].ToObject(typeof(DataTable));
            uiDataGridView1.DataSource = dt;
        }

        private void uiPanel1_Click(object sender, EventArgs e)
        {

        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            GridViewSetter grdSetter = new GridViewSetter();
            grdSetter.SetPersonAccumulativeTotal(uiDataGridView2);
            JObject jo5206 = new JObject();
            jo5206.Add("psn_no", uiTextBox2.Text);//必填
            jo5206.Add("cum_ym", uiTextBox3.Text);//累计年月
            JObject data5206 = new JObject();
            JArray joDic=new JArray();
            data5206.Add("data", jo5206);
            JObject out5206 = invoker.invokeCenterService("5206", JObject.Parse(JsonHelper.setCenterInpar("5206", data5206)));
            //转换累积类别编码
            JArray joTrtinfo = JArray.Parse(JsonHelper.getDestValue(out5206, "output.cuminfo"));
            JObject OutIDInfo=new JObject();
            if (joTrtinfo.Count > 0)
            {
                for (int i = 0; i < joTrtinfo.Count; i++)
                {
                    OutIDInfo = rIS.getSpecDictionaryValue(Global.inf.interfaceDr.ToString(), "cum_type_code", out5206["output"]["cuminfo"][i]["cum_type_code"].ToString());
                    joDic = JArray.Parse(JsonHelper.getDestValue(OutIDInfo, "result.data"));
                    if (joDic.Count > 1)
                    {
                        out5206["output"]["cuminfo"][i]["cum_type_code"] = joDic[1]["name"].ToString();
                    }
                }
            }
            DataTable dt = (DataTable)out5206["output"]["cuminfo"].ToObject(typeof(DataTable));
            //相同含义行数值求和
            //绑定之后判断是否有重复数据，重复数据进行合并
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //两层循环判断表格内是否有数据重复，如果有则合并加上数量即可
                    for (int j = i + 1; j < dt.Rows.Count;)
                    {
                        if (dt.Rows[i]["cum_type_code"].ToString() == dt.Rows[j]["cum_type_code"].ToString() && dt.Rows[i]["cum_ym"].ToString() == dt.Rows[j]["cum_ym"].ToString() && dt.Rows[i]["year"].ToString() == dt.Rows[j]["year"].ToString() && dt.Rows[i]["insutype"].ToString() == dt.Rows[j]["insutype"].ToString())
                        {
                            dt.Rows[i]["cum"] = Convert.ToDouble(dt.Rows[i]["cum"]) + Convert.ToDouble(dt.Rows[j]["cum"]);
                            dt.Rows[j].Delete();
                        }
                        else
                        {//这里是若没有数据重复，则取下一行继续比较，若重复了则因为删除了一行，不需要执行j++进行下一行取值。
                            j++;
                        }
                    }
                }
            }

            uiDataGridView2.DataSource = dt;
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            string outParam;
            //打开读卡窗口，操作员选择读卡类型后进行读卡器读卡，再进行1101获取参保信息
            if (hBus.readCard(out outParam) != 0)
            {
                MessageBox.Show(JsonHelper.setExceptionJson(-100, "读卡失败！", outParam).ToString());
            }
            else
            {
                //展示患者信息界面
                if (hBus.showPatInfo(outParam, out outParam) != 0)
                {
                    MessageBox.Show(JsonHelper.setIrisReturnValue(0, "展示患者信息失败", JObject.Parse(outParam)).ToString());
                }
                else
                {

                }
            }
            uiTextBox2.Text = Global.pat.psn_no;
        }
    }
}
 