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 Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using PTMedicalInsurance.Variables;
using PTMedicalInsurance.Business;

namespace PTMedicalInsurance.Forms
{
    public partial class PatientInsuInfo : Form
    {
        public DataTable dtInsuInfo;
        public DataTable dtIDInfo;
        public JObject joSelectedInsuInfo;
        public int insuInfoIndex = 0;
        public int idInfoIndex = 0;

        //1.声明自适应类实例
        AutoResizeForm asc = new AutoResizeForm();

        public PatientInsuInfo()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterParent;
            asc.controllInitializeSize(this);

            asc.controlAutoSize(this);
        }

        public PatientInsuInfo(JObject jo)
        {
            InitializeComponent();
            dtInsuInfo = (DataTable)jo["insuinfo"].ToObject(typeof(DataTable));
            dtIDInfo   = (DataTable)jo["idetinfo"].ToObject(typeof(DataTable));
            JObject joBaseInfo = JObject.FromObject(jo["baseinfo"]);
            this.tbName.Text = joBaseInfo["psn_name"].ToString();
            this.tbPsnNO.Text = joBaseInfo["psn_no"].ToString();
            this.tbGend.Text = joBaseInfo["gend"].ToString();
            this.tbBirthDay.Text = joBaseInfo["brdy"].ToString();
            this.tbNaty.Text = joBaseInfo["naty"].ToString();
            this.tbCertType.Text = joBaseInfo["psn_cert_type"].ToString();
            this.tbCertNO.Text = joBaseInfo["certno"].ToString();
            this.tbAge.Text = joBaseInfo["age"].ToString();


            //JObject joInsuInfo = JObject.FromObject(jo["insuinfo"]);
            //Global.pat.insuType= joInsuInfo["insutypeNo"].ToString();

            setDgvIDInfo();
            setDgvInsuInfo();
            dgvInsuInfo.DataSource = dtInsuInfo;
            dgvIDInfo.DataSource = dtIDInfo;
        }

        private void AddDGVColumn(DataGridView dgv, string headerText, string dataPropertyName, int width = 120)
        {
            DataGridViewColumn newColumn = new DataGridViewTextBoxColumn();
            newColumn.HeaderText = headerText;
            newColumn.Width = width;
            newColumn.DataPropertyName = dataPropertyName;
            newColumn.Name = dataPropertyName;
            dgv.Columns.Add(newColumn);
        }

        private void setDgvInsuInfo()
        {
            AddDGVColumn(dgvInsuInfo, "余额", "balc", 80);
            AddDGVColumn(dgvInsuInfo, "险种类型", "insutype",200);
            AddDGVColumn(dgvInsuInfo, "人员类别", "psn_type",100);
            AddDGVColumn(dgvInsuInfo, "人员参保状态", "psn_insu_stas");
            AddDGVColumn(dgvInsuInfo, "个人参保日期", "psn_insu_date");
            AddDGVColumn(dgvInsuInfo, "暂停参保日期", "paus_insu_date");
            AddDGVColumn(dgvInsuInfo, "公务员标志", "cvlserv_flag",80);
            AddDGVColumn(dgvInsuInfo, "参保地医保区划", "insuplc_admdvs");
            AddDGVColumn(dgvInsuInfo, "单位名称", "emp_name",300);
            AddDGVColumn(dgvInsuInfo, "险种类型编码", "insutypeNo", 150);
            dgvInsuInfo.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
            dgvInsuInfo.ColumnHeadersHeight = 40;
        }

        private void setDgvIDInfo()
        {
            AddDGVColumn(dgvIDInfo, "人员身份类别", "psn_idet_type", 140);
            AddDGVColumn(dgvIDInfo, "人员类别等级", "psn_type_lv", 140);
            AddDGVColumn(dgvIDInfo, "备注", "memo", 150);
            AddDGVColumn(dgvIDInfo, "开始时间", "begntime", 200);
            AddDGVColumn(dgvIDInfo, "结束时间", "endtime", 200);
            dgvIDInfo.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
            dgvIDInfo.ColumnHeadersHeight = 40;
        }
        private void uiButton1_Click(object sender, EventArgs e)
        {
            
            insuInfoIndex = dgvInsuInfo.CurrentRow.Index;
            if (dtIDInfo.Rows.Count != 0 )
                idInfoIndex = dgvIDInfo.CurrentRow.Index;
            joSelectedInsuInfo = new JObject();
            for (int i = 0; i < dtInsuInfo.Columns.Count; i++)
            {
                string key = dtInsuInfo.Columns[i].ColumnName;
                string value = dgvInsuInfo.CurrentRow.Cells[key].Value.ToString();
                joSelectedInsuInfo.Add(key, value);
            }

            DialogResult = DialogResult.OK;
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnRYDY2001_Click(object sender, EventArgs e)
        {
            string sInput = "", errorMsg = "";


            int iHis = dgvInsuInfo.CurrentRow.Index;
            if (dgvInsuInfo.CurrentRow == null)
            {
                MessageBox.Show("请选择参保信息再查询！");
                return;
            }

            string insutype = dgvInsuInfo.Rows[iHis].Cells["insutypeNo"].Value.ToString();

            MessageBox.Show(insutype);

            JObject joData = new JObject();
            joData.Add("psn_no", this.tbPsnNO.Text);
            joData.Add("insutype", insutype); 
            joData.Add("fixmedins_code", Global.inf.hospitalNO);
            joData.Add("med_type", "11");
            joData.Add("begntime", "2010-01-01 00:00:00");
            joData.Add("endtime", "");

            joData.Add("dise_codg", "");
            joData.Add("dise_name", "");
            joData.Add("oprn_oprt_code", "");
            joData.Add("oprn_oprt_name", "");
            joData.Add("matn_type", "");
            joData.Add("birctrl_type", "");

            JObject joInput = new JObject();
            joInput.Add("data", joData);

            InvokeHelper invoker = new InvokeHelper();
            sInput = JsonHelper.setCenterInpar("2001", joInput);
            JObject joRtn = invoker.invokeCenterService("2001", sInput);
            if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
            {
                MessageBox.Show("人员待遇享受检查失败：" + errorMsg);
            }
            else
            {
                MessageBox.Show("人员待遇享受检查成功！"+ joRtn.ToString());
            }
        }
    }
}
