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 DataTable dtWorkInjury;
        public JObject joSelectedInsuInfo;
        public int insuInfoIndex = 0;
        public int idInfoIndex = 0;
        InvokeHelper invoker = new InvokeHelper();
        private MIIrisServices mIS = new MIIrisServices();
        private DataTable dtFundPayType;
        private Form mainForm;

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

        public PatientInsuInfo()
        {
            InitializeComponent();

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

            asc.controlAutoSize(this);
        }

        public PatientInsuInfo(JObject jo, Form frm)
        {
            InitializeComponent();
            InitForm(jo, frm);
        }

        private DataTable GetDBLKComboxTable(string sqlStr)
        {
            InvokeHelper invoker = new InvokeHelper();
            dynamic joInparm = new JObject();

            dynamic joTmp = new JObject();
            joTmp.sqlStr = sqlStr;
            JArray jaParams = new JArray();
            jaParams.Add(joTmp);
            joInparm.Add("params", JArray.FromObject(jaParams));
            joInparm.code = "09010014";
            string inParam = joInparm.ToString();
            JObject joRtn = invoker.invokeInsuService(inParam, "获取下拉框消息");
            //dynamic jsonRtn = JsonConvert.DeserializeObject(strRtn);
            DataTable dt = (DataTable)joRtn["result"].ToObject(typeof(DataTable));
            //dt.Columns[0].ColumnName = "编码";
            //dt.Columns[1].ColumnName = "名称";
            //dt.Columns[2].ColumnName = "拼音查找码";
            return dt;
        }

        private void InitForm(JObject jo, Form frm)
        {
            InitForm(jo);
            mainForm = frm;
            CheckForIllegalCrossThreadCalls = false;
            // 禁用关闭按钮
            this.FormBorderStyle = FormBorderStyle.None;

            // 隐藏标题栏
            this.ControlBox = false;

            // 其他可能需要的配置
            if (mainForm != this)
            {
                this.TopLevel = false;
                this.Dock = DockStyle.Fill; // 根据需要设置 Dock 属性
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }

            //
            //grdSetter.setHisRegInfo(dgvHisRegInfo);
            //grdSetter.setCenterApplyInfo(dgvCenterApplyInfo);
        }

        private void InitForm(JObject jo)
        {
            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();

            string expContent = joBaseInfo["exp_content"].ToString();  //字段扩展 只有居民返回

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

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

            //获取通用目录
            string sTYML = mIS.GetCommonInterface_Dr(Global.inf.interfaceDr.ToString(), "");
            if (sTYML == "")
                Global.inf.interfaceDr_TY = Global.inf.interfaceDr;
            else
                Global.inf.interfaceDr_TY = int.Parse(sTYML);

            string sqlCondition = " and A.Interface_Dr = '" + Global.inf.interfaceDr_TY.ToString() + "'";
            string sqlStr = "SELECT B.Code,B.Descripts AS Name FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B ON A.ID = B.HBDictionary_Dr  WHERE A.InsuCode = 'fund_pay_type'" + sqlCondition;

            dtFundPayType = GetDBLKComboxTable(sqlStr);
        }

        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();

            if (Global.pat.isWorkInjury)
            { 
                dtWorkInjury = (DataTable)jo["workinjury"].ToObject(typeof(DataTable));
            }

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

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

        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);
        }

        public void setDgvTreatement(DataGridView dgv)
        {
            dgv.AutoGenerateColumns = false;
            dgv.Columns.Clear();
            AddDGVColumn(dgv, "人员编号", "psn_no", 300);
            AddDGVColumn(dgv, "待遇检查类型", "trt_chk_type", 140);
            AddDGVColumn(dgv, "基金支付类型", "fund_pay_type", 300);
            AddDGVColumn(dgv, "基金款项待遇享受标志", "trt_enjymnt_flag", 140);
            AddDGVColumn(dgv, "开始日期", "begndate", 200);
            AddDGVColumn(dgv, "结束日期", "enddate", 200);
            AddDGVColumn(dgv, "待遇检查结果", "trt_chk_rslt", 140);
            AddDGVColumn(dgv, "字段扩展", "exp_content", 140);
            dgv.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
            dgv.ColumnHeadersHeight = 30;
        }

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

        private void setDgvWorkInjury()
        {
            dgvWorkInjury.AutoGenerateColumns = false;
            AddDGVColumn(dgvWorkInjury, "单位编号", "emp_no", 120);
            AddDGVColumn(dgvWorkInjury, "单位名称", "emp_name", 300);
            AddDGVColumn(dgvWorkInjury, "工伤认定日期", "alc031", 150);
            AddDGVColumn(dgvWorkInjury, "工伤认定标志", "alc029");
            AddDGVColumn(dgvWorkInjury, "工伤认定单位", "alc030",300);
            AddDGVColumn(dgvWorkInjury, "工伤人员类别", "ylc035");
            AddDGVColumn(dgvWorkInjury, "伤害部位", "alc022", 120);
            dgvWorkInjury.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
            dgvWorkInjury.ColumnHeadersHeight = 40;
        }

        private void setDgvIDInfo()
        {
            dgvIDInfo.AutoGenerateColumns = false;
            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());
            }
        }
    }
}
