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

namespace PTMedicalInsurance.FormSetter
{
    class ComboxSetter
    {
        private InvokeHelper invoker = new InvokeHelper();
        private HisIrisServices hIS = new HisIrisServices();
        private MIIrisServices mIS = new MIIrisServices();
        public void setCbxInterfaceDataSource(Sunny.UI.UIComboBox cbx)
        {

            DataTable dt = new DataTable();
            JObject joRtn = mIS.getInterface();
            JArray jaParams = new JArray();
            jaParams = JArray.Parse(JsonHelper.getDestValue(joRtn, "result.data"));
            if (jaParams.Count == 0)
            {
                DataTableSetter dts = new DataTableSetter();
                dt = dts.setDgvInterfaceDataTable();
                return;
            }
            else
            {
                dt = (DataTable)jaParams.ToObject(typeof(DataTable));
            }
            dt.AcceptChanges();
            cbx.DataSource = dt;
            cbx.DisplayMember = "InterfaceName";
            cbx.ValueMember = "ID";

        }

        public void setCbxDictionaryDataSource(string interfaceDr, string dicType, Sunny.UI.UIComboBox cbx)
        {
            InsuServices iris = new InsuServices();
            DataTable dt = new DataTable();
            JObject joRtn = iris.getSpecDictionary(interfaceDr, dicType);
            JArray jaParams = new JArray();
            jaParams = JArray.Parse(JsonHelper.getDestValue(joRtn, "result.data"));
            if (jaParams.Count == 0)
            {
                DataTableSetter dts = new DataTableSetter();
                dt = dts.setDgvInterfaceDataTable();
                return;
            }
            else
            {
                dt = (DataTable)jaParams.ToObject(typeof(DataTable));
                //Global.inf.interfaceDr_Download = int.Parse(dt.Rows[0]["ID"].ToString());
            }
            dt.AcceptChanges();
            cbx.DataSource = dt;
            cbx.DisplayMember = "name";
            cbx.ValueMember = "code";

        }

        public string getCbxSelectedValue(Sunny.UI.UIComboBox cbx, string selectedName)
        {
            try
            {
                DataTable dt = (DataTable)cbx.DataSource;
                int a = cbx.SelectedIndex;
                return dt.Rows[a][selectedName].ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}
