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

namespace PTMedicalInsurance.Forms
{     
    public partial class SearchAdmdvs : Form
    {
        public SearchAdmdvs()
        {
            InitializeComponent();      
                       
        }

        private void FrmInsuplc_Admdvs_Load(object sender, EventArgs e)
        {
            edt_SearchKey.Text = Global.pat.card.SearchAdmKey;            

            GridViewSetter gvdSetter = new GridViewSetter();
            gvdSetter.SetAdmdvsData(dbAdmdvs);

            Search(Global.pat.card.SearchAdmKey);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SelectAdmIndex();
        }

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

        private void edt_SearchKey_DoEnter(object sender, EventArgs e)
        {
            Search(edt_SearchKey.Text);
        }

        private DataTable GetAdmdvsTable(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, "获取下拉框消息");
            DataTable dt = (DataTable)joRtn["result"].ToObject(typeof(DataTable));

            dbAdmdvs.DataSource = dt;
            dbAdmdvs.Columns[0].ReadOnly = true;
            //dt.Columns[0].ColumnName = "编码";
            //dt.Columns[1].ColumnName = "名称";
            //dt.Columns[2].ColumnName = "拼音查找码";
            return dt;
        }

        private void Search(string sKey)
        {
            string Filter = " And DesCripts like '%" + edt_SearchKey.Text + "%' ";
            string sqlStr = " SELECT Top 8 B.Code,B.Descripts AS Name FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B "
                          + " ON A.ID = B.HBDictionary_Dr WHERE A.InsuCode = 'admdvs'" + Filter;

            dbAdmdvs.DataSource = GetAdmdvsTable(sqlStr);
        }

        private void SelectAdmIndex()
        {
            int iHis = dbAdmdvs.CurrentRow.Index;

            Global.pat.card.SearchAdmCode = dbAdmdvs.Rows[iHis].Cells["Code"].Value.ToString();
            Global.pat.card.SearchAdmName = dbAdmdvs.Rows[iHis].Cells["Name"].Value.ToString();

            DialogResult = DialogResult.OK;
        }

        private void dbAdmdvs_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SelectAdmIndex();
        }
    }
}
