using System;
using System.Windows.Forms;
using StuMgmLib.MyNameSpace;

namespace StuMgmClient
{
    public partial class LoginForm : Form
    {
        const string deaultIp = "10.10.0.44";
        const int deaultPort = 502;



        public LoginForm()
        {
            InitializeComponent();
            SystemComm.Init(deaultIp, deaultPort);
            if (SystemData.InitSystemData() != ErrCode.Success)
                throw new Exception("初始化异常，请重新启动");
        }

        ////账号密码正则校验
        //public bool RegexUser(string u, string p)
        //{
        //    var regex = new Regex("^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-z!@#$]+$)(?![A-Z!@#$]+$)(?![\\d!@#$]+$)^[a-zA-Z\\d!@#$]{5,20}$");
        //    //校验密码是否符合
        //    bool resultU = regex.IsMatch(u);
        //    bool resultP = regex.IsMatch(p);
        //    if (resultU == true && resultP == true)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}



        private void chkPassWord_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void labForgetPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            LvErr userRole;
            int token;
            short job_id;
            if (VerifLogin(out job_id, out userRole, out token) != ErrCode.Success)
                return;

            switch (userRole)
            {
                case LvErr.Student:
                    ShowStudentForm(job_id, token);
                    break;
                default:
                    MessageBox.Show(userRole.ToString());
                    break;
            }
        }

        ErrCode VerifLogin(out short job_id, out LvErr userRole, out int token)
        {
            job_id = 01943;
            //ErrCode errcode = SystemCtrl.VerifLogin(txtUserName.Text, txtPassWord.Text, out userRole, out token);

            ErrCode errcode = SystemCtrl.VerifLogin(job_id, "1", out userRole, out token);
            if (errcode != ErrCode.Success)
                MessageBox.Show(errcode.ToString());
            job_id = 01943;
            return errcode;
        }

        void ShowStudentForm(short job_id, int token)
        {
            this.Hide();
            StudentForm stu = new StudentForm(job_id, token);
            stu.ShowDialog();
            this.Show();
        }
    }
}
