using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Helper;
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 SelfpayPercentForm : Form
    {
        public string CenterCode { get; set; }
        public string SelfpayId = "";
        public string BeginDate { get; set; }
        public SelfpayPercentForm()
        {
            InitializeComponent();
        }


        private 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 = "09010081";
            string inParam = joInparm.ToString();
            JObject joRtn = invoker.invokeInsuService(inParam, "获取下拉框消息");
            //dynamic jsonRtn = JsonConvert.DeserializeObject(strRtn);
            string rtnData = JsonHelper.getDestValue(joRtn, "result.data");
            DataTable dt = (DataTable)JArray.Parse(rtnData).ToObject(typeof(DataTable));
            //dt.Columns[0].ColumnName = "编码";
            //dt.Columns[1].ColumnName = "名称";
            //dt.Columns[2].ColumnName = "拼音查找码";
            return dt;
        }

        private void SetDBLKCombox(ref PTControl.DBLookupCombox dblcbx, string sqlStr)
        {
           
            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;
        }


        private void SelfpayPercentForm_Load(object sender, EventArgs e)
        {

            loadType();

            string strType = "A";
            GetPayPercent(strType);

        }

        private void loadType()
        {
            string sqlStr = " SELECT B.Code,B.Descripts AS Name FROM HB_Dictionary A JOIN HB_DictionaryDataDetail B ON A.ID = B.HBDictionary_Dr"
                      + " WHERE A.InsuCode = 'RQLB' AND Interface_Dr = " + Global.inf.interfaceDr;

            chkType.ValueMember = "Code";
            chkType.DisplayMember = "Name";
            DataTable dt = GetDBLKComboxTable(sqlStr);
            chkType.DataSource = dt;
            if (dt?.Rows.Count > 0)
            {
                chkType.SelectedIndex = 0;
            }

        }

        private void GetPayPercent(string type)
        {
            string sqlStr = " SELECT top 1 ID,Code,Name,Proportion,BeginDate  FROM  HB_MedInsuDirectorySelfpayProportion where Interface_Dr = " + Global.inf.interfaceDr + "  And Code = '" + CenterCode + "' and PersonnelType = '" + type + "' order by ID desc";

            DataTable dt = GetDBLKComboxTable(sqlStr);
            if (dt?.Rows.Count > 0 && dt?.Columns.Count>0)
            {
                txtPercent.Text = dt.Rows[0]["Proportion"].ToString();
                lblCode.Text = CenterCode + "已设置自付比例";
                lblCode.ForeColor = Color.Black;
                SelfpayId = dt.Rows[0]["ID"].ToString();
            }
            else {
                lblCode.Text = CenterCode + "未设置自付比例";
                lblCode.ForeColor = Color.Blue;
                SelfpayId = "";
                txtPercent.Text = "";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            decimal percent = decimal.Parse(txtPercent.Text.Trim());
            if (percent > 1) {
                percent = (percent / 100);
            }

            JArray joArray = new JArray();
            dynamic joTmp = new JObject();
            joTmp.HospitalDr = Global.inf.hospitalDr;
            joTmp.InterfaceDr = Global.inf.interfaceDr;
            joTmp.updateUserID = Global.user.ID;
            joTmp.ID = SelfpayId;
            joTmp.Code = CenterCode;
            string personType = chkType.SelectedValue.ToString(); //人群类别
            if (string.IsNullOrEmpty(personType))
            {
                personType = "A"; //职工
            }
            joTmp.PersonnelType = personType;
            string proportionType = ""; //自费类型（统筹类别）
            if (string.IsNullOrEmpty(proportionType))
            {
                proportionType = "6"; //普通门诊
            }
            joTmp.ProportionType = proportionType;
            joTmp.Proportion = percent + "";
            joArray.Add(joTmp);
            string errMsg = "";
            InvokeHelper invoker = new InvokeHelper();
            JObject joRtn = invoker.invokeInsuService(JsonHelper.setIrisInpar("09010085", joArray).ToString(), "设置自费比例");

            if (JsonHelper.parseIrisRtnValue(joRtn, out errMsg) != 0)
            {
                MessageBox.Show(errMsg);
            }
            else {
                MessageBox.Show("设置成功!");
            }
        }

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

        private void chkType_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetPayPercent(chkType.SelectedValue.ToString());
        }
    }
}
