using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PTMedicalInsurance.FormSetter
{
    class GridViewSetterBase
    {
        /// <summary>
        /// 动态增加datagridview列
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="headerText"></param>
        /// <param name="dataPropertyName"></param>
        protected 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 SetDBLKCombox(ref PTControl.DBLookupCombox dblcbx, string sqlStr)
        {
            SetDBLKCombox(ref dblcbx,GetDBLKComboxTable(sqlStr));
        }

        public void SetDBLKCombox(ref PTControl.DBLookupCombox dblcbx,DataTable dt)
        {
            if (dt == null) return;

            dblcbx.sDisplayField = "Code,Name,SearchCode";
            dblcbx.sDisplayMember = "名称";
            dblcbx.sKeyWords = "Code,SearchCode";
            dblcbx.DataSource = dt;
            dblcbx.RowFilterVisible = true;
            dblcbx.TextBox.Width = 400;
            dblcbx.DataGridView.Width = 400;
            dblcbx.DataGridView.Columns[0].Name = "编码";
            dblcbx.DataGridView.Columns[1].Name = "名称";
            dblcbx.DataGridView.Columns[2].Name = "查找码";
            dblcbx.DataGridView.Columns[0].Width = 100;
            dblcbx.DataGridView.Columns[1].Width = 200;
        }

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