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;

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;
        public PatientInsuInfo()
        {
            InitializeComponent();
        }

        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();
            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");
            AddDGVColumn(dgvInsuInfo, "人员参保状态", "psn_insu_stas");
            AddDGVColumn(dgvInsuInfo, "个人参保日期", "psn_insu_date");
            AddDGVColumn(dgvInsuInfo, "暂停参保日期", "paus_insu_date");
            AddDGVColumn(dgvInsuInfo, "公务员标志", "cvlserv_flag",60);
            AddDGVColumn(dgvInsuInfo, "参保地医保区划", "insuplc_admdvs");
            AddDGVColumn(dgvInsuInfo, "单位名称", "emp_name",300);
            dgvInsuInfo.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
            dgvInsuInfo.ColumnHeadersHeight = 40;
        }

        private void setDgvIDInfo()
        {
            AddDGVColumn(dgvIDInfo, "人员身份类别", "psn_idet_type", 150);
            AddDGVColumn(dgvIDInfo, "人员类别等级", "psn_type_lv", 150);
            AddDGVColumn(dgvIDInfo, "备注", "memo", 150);
            AddDGVColumn(dgvIDInfo, "开始时间", "begntime", 150);
            AddDGVColumn(dgvIDInfo, "结束时间", "endtime", 150);
            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);
            }
            //MessageBox.Show(joSelectedInsuInfo.ToString());
            DialogResult = DialogResult.OK;
        }

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