using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PTMedicalInsurance.Common;
using PTMedicalInsurance.Helper;
using Newtonsoft.Json.Linq;
using PTMedicalInsurance.Forms;
using PTMedicalInsurance.Variables;

namespace PTMedicalInsurance.Forms
{
    public partial class ChooseCard : Form
    {
        public string EcCodeType; //电子凭证类型
        public string cardType;
        public string businessType;
        public string ID,PatName;

        //设置业务实例
        InvokeHelper invoker = new InvokeHelper();

        public ChooseCard()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            cardType = "0" + (rbgCardType.SelectedIndex +1).ToString();
            businessType = "01" + cbBusinessType.Text.Substring(0,3);
            EcCodeType = rbgEcType.SelectedIndex.ToString();

            Global.pat.OtherProv = rbgOtherProv.SelectedIndex;

            if ((rbgOtherProv.SelectedIndex == 1)&&(cbCBD.Text==""))
            {
                MessageBox.Show("异地结算,请选择统筹区！");
                return;
            }

            if (rbgCardType.SelectedIndex == 1)
            {
                if (tbID.Text == "")
                {
                    ID = "";
                }
                else
                {
                    ID = tbID.Text;
                }                

                PatName = tbName.Text;
            }
            else
            {
                PatName = "";
            }
            DialogResult = DialogResult.OK;
        }

        private void ChooseCard_Load(object sender, EventArgs e)
        {
            rbgCardType.SelectedIndex = 2;      //身份证
            cbBusinessType.SelectedIndex = 0;   //社保卡

            rbgOtherProv.SelectedIndex = 0;     //本地
            rbgEcType.SelectedIndex = -1; //电子二维码                                                
        }

        private void rbgCardType_ValueChanged(object sender, int index, string text)
        {
            if (rbgCardType.SelectedIndex == 0)
            {
                cbBusinessType.Enabled = true;
                rbgEcType.SelectedIndex = 0;
            }
            else
            {
                cbBusinessType.Enabled = false;
                rbgEcType.SelectedIndex = -1;
            }

            if (rbgCardType.SelectedIndex == 2)
            {
                cbCardType.SelectedIndex = 0;
                cbCardLevel.SelectedIndex = 2;
                cbCardType.Enabled = true;
                tbPassword.Enabled = true;
                //tbPassword.Text = "";      
            }
            else
            {
                cbCardType.Enabled = false;
                tbPassword.Enabled = false;
            }

            if (rbgCardType.SelectedIndex != 1)
                tbID.Text = "";
        }

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

        private void uiButton1_Click(object sender, EventArgs e)
        {
            string sInput = "", errorMsg="";

            JObject joData = new JObject();
            joData.Add("", "");

            JObject joInput = new JObject();
            joInput.Add("data", joData);

            InvokeHelper invoker = new InvokeHelper();
            sInput = JsonHelper.setCenterInpar("1193", "");
            JObject joRtn =invoker.invokeCenterService("1193", sInput);
            if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
            {
                MessageBox.Show("修改卡密码失败：" + errorMsg);
            }
            else
            {
                MessageBox.Show("修改卡密码成功！");
            }
        }

        private void SearchAdmdvs()
        {
            JObject joAdmdvsInfo = new JObject();
            SearchAdmdvs Adm = new SearchAdmdvs();

            try
            {
                Global.pat.card.SearchAdmKey = cbCBD.Text;
                Adm.StartPosition = FormStartPosition.CenterParent;

                if (Adm.ShowDialog() == DialogResult.OK)
                {
                    cbCBD.Text = Global.pat.card.SearchAdmName;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("异常：" + ex.Message);
                return;
            }
        }

        private void btnScanCode_Click(object sender, EventArgs e)
        {
            cardType = "01";
            businessType = "01" + cbBusinessType.Text.Substring(0, 3);
            EcCodeType = "0";

            Global.pat.OtherProv = rbgOtherProv.SelectedIndex;

            if ((rbgOtherProv.SelectedIndex == 1) && (cbCBD.Text == ""))
            {
                MessageBox.Show("异地结算,请选择统筹区！");
                return;
            }

            PatName = "";
            DialogResult = DialogResult.OK;
        }

        private void btnFaceIdentify_Click(object sender, EventArgs e)
        {
            cardType = "01"; 
            businessType = "01" + cbBusinessType.Text.Substring(0, 3);
            EcCodeType = "1";

            Global.pat.OtherProv = rbgOtherProv.SelectedIndex;

            if ((rbgOtherProv.SelectedIndex == 1) && (cbCBD.Text == ""))
            {
                MessageBox.Show("异地结算,请选择统筹区！");
                return;
            }

            PatName = "";
            DialogResult = DialogResult.OK;
        }

        private void rbgEcType_ValueChanged(object sender, int index, string text)
        {
            rbgCardType.SelectedIndex = 0;
        }

        private void cbCBD_DoEnter(object sender, EventArgs e)
        {
            SearchAdmdvs();
        }
    }
}
