using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SystemX.Product.ALIS.Interface;
using static SystemX.Product.ALIS.UI.Commons;

namespace SystemX.Product.ALIS.UI.Subs
{
    public partial class LoginForm : DevExpress.XtraEditors.XtraForm
    {
        private IDataController ctrlDB;

        private DataSet ds = new DataSet();
        private DataTable dt = new DataTable();

        public string UserID { internal set; get; }
        public string UserName { internal set; get; }
        public string UserDept { internal set; get; }
        public string UserEmail { internal set; get; }
        public string UserComment { internal set; get; }
        public string UserPassword { internal set; get; }

        public LoginForm(IDataController ctrlDB)
        {
            InitializeComponent();

            this.ctrlDB = ctrlDB;

            this.Focus();
            this.BringToFront();

            this.ActiveControl = txtUserId;

            txtUserId.Focus();

            LoginInfoINICtrl LCtrl = new LoginInfoINICtrl();
            string strGetConnectInfo = LCtrl.GetValue("LastestLogin", "Info", "");

            if (strGetConnectInfo.Length > 0)
            {
                txtUserId.Text = strGetConnectInfo;

                this.ActiveControl = txtPassword;
            }

            for (int i = 0; i < INICtrl.MAX_INFORMATION; i++)
            {
                string strGetInfo = LCtrl.GetValue("HistoryLogin" + i.ToString(), "Info", "");

                if (strGetInfo.Length > 0)
                    txtUserId.Properties.Items.Add(strGetInfo);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.None;

            SqlDataReader dr = null;

            try
            {
                ctrlDB.GetConnSqlCmd().CommandText = "SELECT * FROM STAT_User WHERE UserID = '" + txtUserId.Text + "' AND Password = '" + txtPassword.Text + "';";
                dr = ctrlDB.GetConnSqlCmd().ExecuteReader();

                if (dr != null)
                {
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();

                    int iFieldCnt = dr.FieldCount;
                    int iRecordsAffectedCnt = dr.RecordsAffected;
                    bool bHasRow = dr.HasRows;

                    dt.Load(dr);
                    ds.Tables.Add(dt);

                    if (dt.Rows.Count > 0)
                    {
                        UserID = dt.Rows[0]["UserID"].ToString();
                        UserName = dt.Rows[0]["Name"].ToString();
                        UserDept = dt.Rows[0]["Dept"].ToString();
                        UserEmail = dt.Rows[0]["Email"].ToString();
                        UserComment = dt.Rows[0]["Comment"].ToString();
                        UserPassword = dt.Rows[0]["Password"].ToString();

                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            finally
            {
                if (dr != null)
                    dr.Close();
            }

            if (this.DialogResult == DialogResult.None)
            {
                this.MaximumSize = new Size(300, 180);
                this.MinimumSize = new Size(300, 180);
                this.Size = new Size(300, 180);

                labelAlarm.Visible = true;
                labelAlarm.Text = "User information does not match or\r\n the password is incorrect.";
            }
        }
    }
}