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 sP_CertType, sP_CertNO, sP_CardSN, sP_IDType, sP_IDNO, sL_CertCodeType, sL_EcCertDecodeTradeNO;
        public string businessType, EcCertDecodeType;
        public string ID,PatName;
        public Boolean bOtherProvLocal = false;
        public int sL_CardType;

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

        public ChooseCard()
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            if (rbgOtherProv.SelectedIndex == 0)
                bOtherProvLocal = false;       //本地
            else
                bOtherProvLocal = true;        //异地

            if ((tbPZLP.Text != "") && (tbZJHM.Text == ""))
                tbZJHM.Text = tbPZLP.Text;

            sP_CertType = "0" + (rbgMdtrtCertType.SelectedIndex +1).ToString();  //就诊凭证类型            
            sP_CertNO = tbPZLP.Text;                                             //凭证令牌
            sP_IDNO = tbZJHM.Text;                                               //证件号码
            sP_CardSN = tbCardSN.Text;                                           //卡识别码
            sP_IDType = cbDocumentType.Text.Trim().Substring(0, 6);              //证件类型
            PatName = tbName.Text;                                               //姓名

            //电子凭证
            if (rbgMdtrtCertType.SelectedIndex == 0)
            {
                sL_CertCodeType= "01" + cbYMYWLX.Text.Trim().Substring(0,3);            //用码业务类型                

                if (rbgEcCertDecodeType.SelectedIndex == 0)
                {
                    sL_EcCertDecodeTradeNO = "ec.query";
                    EcCertDecodeType = "0";                    
                }
                else if (rbgEcCertDecodeType.SelectedIndex == 1)
                {
                    sL_EcCertDecodeTradeNO = "cn.nhsa.qrcode.get";
                    EcCertDecodeType = "1";
                }
                else if (rbgEcCertDecodeType.SelectedIndex == 2)
                {
                    sL_EcCertDecodeTradeNO = "cn.nhsa.auth.check";
                    EcCertDecodeType = "2";
                }
            }

            //身份证
            if (rbgMdtrtCertType.SelectedIndex == 1)
            {
                if (tbZJHM.Text == "")
                {
                    MessageBox.Show("使用身份证读卡时，证件号码不能为空!");
                    return;
                }
                else
                {
                    tbPZLP.Text = tbZJHM.Text;
                    sP_CertNO = sP_IDNO;
                }
            }

            //社保卡
            if (rbgMdtrtCertType.SelectedIndex == 2)
            {
                if (cbCardType.Text == "")
                {
                    MessageBox.Show("读社保卡时需要选择卡类型!");
                    return;
                }

                sL_CardType = int.Parse(cbCardType.Text.Trim().Substring(0, 1));     //卡类型                
            }
          
            DialogResult = DialogResult.OK;
        }

        private void ChooseCard_Load(object sender, EventArgs e)
        {
            rbgMdtrtCertType.SelectedIndex = 2;   //身份证
            cbYMYWLX.SelectedIndex = 0;           //用码业务类型
            rbgOtherProv.SelectedIndex = 0;       //本地           
            cbDocumentType.SelectedIndex = 0;     //证件类型
        }

        private void rbgMdtrtCertType_ValueChanged(object sender, int index, string text)
        {
            if (rbgMdtrtCertType.SelectedIndex == 0)
            {
                rbgEcCertDecodeType.SelectedIndex = 0;
                uiLabel4.Text = "凭证令牌";
                rbgEcCertDecodeType.Enabled = true;
                cbYMYWLX.Enabled = true;

                uiLabel3.Visible = true;
                cbYMYWLX.Visible = true;
            }
            else if (rbgMdtrtCertType.SelectedIndex == 1)
            {
                uiLabel4.Text = "身份证号";
                rbgEcCertDecodeType.Enabled = false;
                cbYMYWLX.Enabled = false;

                uiLabel3.Visible = false;
                cbYMYWLX.Visible = false;
            }
            else if (rbgMdtrtCertType.SelectedIndex == 2)
            {
                uiLabel4.Text = "社保卡号";
                cbCardType.SelectedIndex = 0;
                rbgEcCertDecodeType.Enabled = false;
                cbYMYWLX.Enabled = false;

                uiLabel3.Visible = false;
                cbYMYWLX.Visible = false;
            }
        }

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

        private void uiButton1_Click(object sender, EventArgs e)
        {          
            Global.pat.bChangePIN = true;
            CardReader_GS CardRead_GS = new CardReader_GS(Global.inf.cardURL, Global.inf.areaCode);

            #region【更改社保卡密码】
            //【1.调用初始化读卡函数】
            if (CardRead_GS.Init() != 0)
            {
                MessageBox.Show("修改密码初始化Init失败！");
                return;
            }            

            //【2.调用社保卡获取卡信息】
            if (CardRead_GS.ReadCardBas(out string basInfo, out string regInfo) != 0)
            {                
                return;
            }
            else
            {
                //【社保卡读卡成功返回】
                Global.pat.insuplc_admdvs = tools.getDestPosStrBySpliter(basInfo, 1);    //参保地
                Global.pat.card.NO = tools.getDestPosStrBySpliter(basInfo, 2);           //社会保障号码                                                     
                Global.pat.name = tools.getDestPosStrBySpliter(basInfo, 5);              //姓名
                Global.pat.certNO = Global.pat.card.NO;

                if (Global.pat.insuplc_admdvs == "")
                {
                    MessageBox.Show("ReadCardBas读卡获取：参保地为空！");
                    return;
                }
                if (Global.pat.certNO == "")
                {
                    MessageBox.Show("ReadCardBas读卡获取：身份证号为空！");
                    return;
                }
                if (Global.pat.name == "")
                {
                    MessageBox.Show("ReadCardBas读卡获取：姓名为空！");
                    return;
                }

                //【2.调用更改社保卡密码函数】
                if (CardRead_GS.ChangePIN(out string OutMsg) != 0)
                {
                    MessageBox.Show("社保卡密码更改异常ChangePIN：" + OutMsg);
                    return;
                }
                else
                {                    
                    MessageBox.Show("社保卡密码更改成功！");
                }
            }
            #endregion

            //DialogResult = DialogResult.OK;
        }

        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 cbCBD_DoEnter(object sender, EventArgs e)
        {
            SearchAdmdvs();
        }
    }
}
