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

namespace PTMedicalInsurance.Forms
{
    public partial class AddSingleDictionary : Form
    {
        enum operatType { Add,Update };

        operatType opt;

        public DataRow dr;
        public JObject joPamam;
        public AddSingleDictionary()
        {
            InitializeComponent();
            cbxDirecType.SelectedIndex = 8;
            opt = operatType.Add;
            this.dr = null;
        }

        public AddSingleDictionary(DataRow dr)
        {
            InitializeComponent();
            cbxDirecType.SelectedIndex = 8;
            setValue(dr);
            opt = operatType.Update;
            this.dr = dr;
        }
        private void btnYes_Click(object sender, EventArgs e)
        {
            AssignToJoParam();
            this.DialogResult = DialogResult.OK;
        }

        private void setValue(DataRow dr)
        {
            try
            {
                tbInsuCode.Text = dr["insuCode"].ToString();
                tbInsuDesc.Text = dr["insuDesc"].ToString();
                tbInsuCodeStartDate.Text = dr["insuCodeStartDate"].ToString();
                tbInsuCodeStopDate.Text = dr["insuCodeStopDate"].ToString();
                tbCode.Text = dr["code"].ToString();
                tbDescripts.Text = dr["descripts"].ToString();
                tbStartDate.Text = dr["startDate"].ToString();
                tbStopDate.Text = dr["stopDate"].ToString();
                tbPinyinSearchCode.Text = dr["pinyinSearchCode"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("setValue" + ex.Message);
            }
        }
      
        private void AssignToJoParam()
        {
            joPamam = new JObject();
            JObject joDic = new JObject();
            JObject joDicDetail = new JObject();
            joDic.Add("HospitalDr", Global.inf.hospitalDr);
            joDic.Add("InterfaceDr", Global.inf.interfaceDr);
            joDic.Add("InsuCode", tbInsuCode.Text);
            joDic.Add("InsuDesc", tbInsuDesc.Text);
            joDic.Add("StartDate", tbInsuCodeStartDate.Text);
            joDic.Add("StopDate", tbInsuCodeStopDate.Text);
            if (opt == operatType.Add)
            {
                joDic.Add("CreateDate", DateTime.Now.ToString("yyyy-MM-dd"));
                joDic.Add("CreateTime", DateTime.Now.ToString("HH:mm:ss"));
            }

            joDicDetail.Add("HospitalDr", Global.inf.hospitalDr);
            joDicDetail.Add("InterfaceDr", Global.inf.interfaceDr);
            joDicDetail.Add("Code", tbCode.Text);
            joDicDetail.Add("Descripts", tbDescripts.Text);
            joDicDetail.Add("PinyinSearchCode", tbPinyinSearchCode.Text);
            joDicDetail.Add("StartDate", tbStartDate.Text);
            joDicDetail.Add("StopDate", tbStopDate.Text);
            if (opt == operatType.Add)
            {
                joDicDetail.Add("CreateDate", DateTime.Now.ToString("yyyy-MM-dd"));
                joDicDetail.Add("CreateTime", DateTime.Now.ToString("HH:mm:ss"));
                joDicDetail.Add("CreateUserDr", tbStartDate.Text);
            }

            joPamam.Add("dicObj", joDic);
            joPamam.Add("detailObj", joDicDetail);
            joPamam.Add("operateType",opt.GetHashCode());
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
