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


    public partial class AddSingleDirectory : Form
    {
        private int direcType ;
        public JObject joPamam = new JObject();
        public AddSingleDirectory()
        {
            InitializeComponent();
        }

        public AddSingleDirectory(int direcType)
        {
            InitializeComponent();
            this.direcType = direcType;
            cbxDirecType.SelectedIndex = this.direcType;
            initForm();
        }

        public AddSingleDirectory(int direcType,DataRow dr)
        {
            InitializeComponent();
            this.direcType = direcType;
            cbxDirecType.SelectedIndex = this.direcType;
            initForm();
            setValue(dr);

        }

        private void pnlClient_Paint(object sender, PaintEventArgs e)
        {

        }

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

        private void setTextBoxEnabled(bool b)
        {
            Control.ControlCollection sonControls = pnlClient.Controls;
            //遍历所有控件
            foreach (Control control in sonControls)
            {
                if (control is TextBox) control.Enabled = b;
            }
        }
        private bool getFlag(string value)
        {
            if (value == "")
            {
                return false;
            }

            if (value == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        private void initForm()
        {
            setTextBoxEnabled(true);
            switch (direcType)
            {
                case 0:
                    {
                        initDrugForm();
                        break;
                    }
                case 2:
                    {
                        initDrugForm();
                        break;
                    }
            }
        }
        //private void setValue(DataRow dr)
        //{
        //    switch (direcType)
        //    {
        //        case 0:
        //            {
        //                setDrugInfo(dr);
        //                break;
        //            }
        //    }
        //}
        private void setValue(DataRow dr)
        {
            try
            {
                tbCode.Text = dr["Code"].ToString();
                tbName.Text = dr["Name"].ToString();
                tbLocateCode.Text = dr["LocateCode"].ToString();
                tbSafetyCode.Text = dr["DrugSafetyStandardCode"].ToString();
                tbDosageName.Text = dr["DosageFormName"].ToString();
                tbDosageCode.Text = dr["DosageFormCode"].ToString();
                tbCategory.Text = dr["CategoryName"].ToString();
                tbSpecification.Text = dr["Specification"].ToString();
                tbUnitOfPackag.Text = dr["UnitOfPackag"].ToString();
                tbUnitOfValuation.Text = dr["UnitOfValuation"].ToString();
                tbStartDate.Text = dr["StartDate"].ToString();
                tbEndDate.Text = dr["EndDate"].ToString();
                tbPinyinSearchCode.Text = dr["PinyinSearchCode"].ToString();
                rtbInstructions.Text = dr["Instructions"].ToString();
                rtbExceptContent.Text = dr["ExceptContent"].ToString();
                rtbConnotation.Text = dr["Connotation"].ToString();
                rtbLimitRange.Text = dr["LimitRange"].ToString();
                tbNote.Text = dr["Note"].ToString();
                tbApprovalNO.Text = dr["ApprovalNO"].ToString();
                tbVersionNO.Text = dr["VersionNO"].ToString();
                tbVersionName.Text = dr["VersionName"].ToString();
                cbUseFlag.Checked = getFlag(dr["UseFlag"].ToString());
                cbSpecialFlag.Checked = getFlag(dr["SpecialFlag"].ToString());
                cbLimitFlag.Checked = getFlag(dr["LimitFlag"].ToString());
                cbValidFlag.Checked = getFlag(dr["ValidFlag"].ToString());
                tbUniqueRecordNO.Text = dr["UniqueRecordNO"].ToString();
                tbNationalCode.Text = dr["NationalCode"].ToString();
                tbNationalName.Text = dr["NationalName"].ToString();
                tbManufacturers.Text = dr["Manufacturers"].ToString();
                if (dr["ChargeItemLevel"].ToString() != "")
                {
                    cbxChargeItemLevel.SelectedIndex = int.Parse(dr["ChargeItemLevel"].ToString()) - 1;
                }
                else
                {
                    cbxChargeItemLevel.Text = dr["ChargeItemLevel"].ToString();
                }
                
                tbCompany.Text = dr["Company"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("setDrugInfo" + ex.Message);
            }
        }
        private void AssignToJoParam()
        {
            string hisTypeName = cbxDirecType.Text;


            string pym = tbPinyinSearchCode.Text;
            if (tbPinyinSearchCode.Text == "")
            {
                pym = "";
            }

            string ChargeItemLevel = cbxChargeItemLevel.Text;
            if (ChargeItemLevel != "")
            {
                ChargeItemLevel = "0" + (cbxChargeItemLevel.SelectedIndex + 1).ToString();
            }



            joPamam.Add("Code",tbCode.Text);
            joPamam.Add("Name", tbName.Text);
            joPamam.Add("HisType",direcType + 1);
            joPamam.Add("HisTypeName", hisTypeName);
            joPamam.Add("LocateCode", tbLocateCode.Text);
            joPamam.Add("LocateName", tbLocateName.Text);
            joPamam.Add("DrugSafetyStandardCode", tbSafetyCode.Text);
            joPamam.Add("DosageFormCode", tbDosageCode.Text);
            joPamam.Add("DosageFormName", tbDosageName.Text);
            joPamam.Add("CategoryCode", tbCategory.Text);
            joPamam.Add("CategoryName", "");
            joPamam.Add("Specification", tbSpecification.Text);
            joPamam.Add("SpecificationCode", "");
            joPamam.Add("UnitOfPackag", tbUnitOfPackag.Text);
            joPamam.Add("UnitOfValuation", tbUnitOfValuation.Text);
            joPamam.Add("StartDate", tbStartDate.Text);
            joPamam.Add("EndDate", tbEndDate.Text);
            joPamam.Add("PinyinSearchCode", pym);
            joPamam.Add("Instructions", rtbInstructions.Text);
            joPamam.Add("ExceptContent", rtbExceptContent.Text);
            joPamam.Add("Connotation", rtbConnotation.Text);
            joPamam.Add("Note", tbNote.Text);
            joPamam.Add("ValidFlag", Convert.ToInt32(cbValidFlag.Checked));
            joPamam.Add("ApprovalNO", tbApprovalNO.Text);
            joPamam.Add("VersionNO", tbVersionNO.Text);
            joPamam.Add("VersionName", tbVersionName.Text);
            joPamam.Add("UseFlag", Convert.ToInt32(cbUseFlag.Checked));
            joPamam.Add("SpecialFlag", Convert.ToInt32(cbSpecialFlag.Checked));
            joPamam.Add("LimitFlag", Convert.ToInt32(cbLimitFlag.Checked));
            joPamam.Add("LimitRange", rtbLimitRange.Text);
            joPamam.Add("UniqueRecordNO", tbUniqueRecordNO.Text);
            joPamam.Add("NationalCode", tbNationalCode.Text);
            joPamam.Add("NationalName", tbNationalName.Text);
            joPamam.Add("Manufacturers", tbManufacturers.Text);
            joPamam.Add("UpdateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            joPamam.Add("ChargeItemLevel", ChargeItemLevel);
            joPamam.Add("Company", tbCompany.Text);
        }
        private void initDrugForm()
        {
            lbApprovalNO.ForeColor = Color.Red;
            lbSpecification.ForeColor = Color.Red;
            lbManufacturers.ForeColor = Color.Red;
            lbUnitOfPackag.ForeColor = Color.Red;
            lbCompany.ForeColor = Color.Red;
        }

        private void initMedicalServiceForm()
        {

           
        }
        private void initMaterialsForm()
        {

        }

        private void initDiagnoseForm()
        {

        }
        private void initDictionaryForm()
        {

        }

        private void textBox23_TextChanged(object sender, EventArgs e)
        {

        }

        private void lbUniqueRecordNO_Click(object sender, EventArgs e)
        {

        }

        private void tbUniqueRecordNO_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            AssignToJoParam();
            DialogResult = DialogResult.OK;
        }
    }
}
