using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;
using PublicLib;
using ClientLib.CommonService;
using ClientLib;
using System.Reflection;
using System.IO;

namespace HM_WORK
{
    public partial class LoginForm : XtraForm
    {
        //private bool m_Loading;

        public LoginForm()
        {
            InitializeComponent();

            labelControl_Notice.BackColor = Color.Transparent;
            labelControl_Notice.BringToFront();


            //m_Loading = true;
            if (File.Exists(ConstClass._RUN_PATH + "app.ico"))
            {
                this.Icon = Icon.ExtractAssociatedIcon(ConstClass._RUN_PATH + "app.ico");
            }
            else
            {
                this.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
            }

            pictureBox_Login.ImageLocation = ConstClass._PATH + "login_background.png";

            simpleButton_YES.Click += (sender, e) =>
            {
                LoginProc();
            };

            textEdit_PASSWD.KeyPress += (sender, e) =>
            {
                if (e.KeyChar == '\r')
                {
                    LoginProc();
                }
            };

            checkEdit_SAVE_ID.Checked = true;

            Object comp_cd = ConstClass._REG.GetValue("COMP_CD");
            if (!UtilClass.isNull(comp_cd))
            {
                lookUpEdit_COMP_CD.EditValue = comp_cd;
            }

            Object usr_id = ConstClass._REG.GetValue("USR_ID");
            if (!UtilClass.isNull(usr_id))
            {
                textEdit_LOGIN_ID.Text = UtilClass.toStr(usr_id);
            }


            //m_Loading = false;

            this.Shown += (sender, e) =>
            {
                try
                {
 
                    ResultData data = ClientClass.GetData("GetCompanyListForLogin", null);
                    if (data.isError)
                    {
                        throw new Exception(data.ResultValue);
                    }
                    UtilClass.SetLookup(lookUpEdit_COMP_CD, data.TableData, "COMP_CD", "COMP_NM", true);

                    if (ConstClass._DEBUG_MODE)
                    {
                        lookUpEdit_COMP_CD.EditValue = "0001";
                        textEdit_LOGIN_ID.Text = "admin";
                        textEdit_PASSWD.Text = "1111";
                        textEdit_PASSWD.Focus();

                        //LoginProc();

                        return;
                    }
                    if (textEdit_LOGIN_ID.Text.Trim().Equals(""))
                    {
                        textEdit_LOGIN_ID.Focus();
                    }
                    else
                    {
                        textEdit_PASSWD.Focus();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Close();
                }
            };
        }

        public string tryLogin(string comp_cd, string id, string pwd)
        {
            string aStr = "";

            try
            {
                SerializedSqlParam[] aParam = new SerializedSqlParam[] {
                    ClientClass.CreateSqlParameter("COMP_CD", SqlDbType.NVarChar, comp_cd),
                    ClientClass.CreateSqlParameter("USR_ID", SqlDbType.NVarChar, id),
                    ClientClass.CreateSqlParameter("SYS_PWD_NO", SqlDbType.NVarChar, pwd),
                    ClientClass.CreateSqlParameter("IP_NO", SqlDbType.NVarChar, ConstClass._MAC),
                    ClientClass.CreateSqlParameter("PC_NM", SqlDbType.NVarChar, ConstClass._PC_NM)
                };
                ResultData data = ClientClass.GetData("LoginProc", aParam);
                if (data.isError)
                {
                    throw new Exception(data.ResultValue);
                }

                ConstClass._COMP_CD = comp_cd;
                ConstClass._USR_COMP_CD = data.DataList.Tables[0].Rows[0]["COMP_CD"].ToString().Trim();
                ConstClass._USR_ID = data.DataList.Tables[0].Rows[0]["USR_ID"].ToString().Trim();
                ConstClass._USR_NM = data.DataList.Tables[0].Rows[0]["USR_NM"].ToString().Trim();
                ConstClass._USR_IMAGE_FILE = data.DataList.Tables[0].Rows[0]["IMG_PATH"].ToString().Trim();
                ConstClass._ADMIN_YN = UtilClass.isEqual(data.DataList.Tables[0].Rows[0]["ADMIN_YN"], "Y");

                DataTable userData = data.DataList.Tables[0];
                for (int i = 0; i < userData.Columns.Count; i++)
                {
                    ConstClass._ADD_USER_INFO(userData.Columns[i].ColumnName, userData.Rows[0][i]);
                }

                ConstClass._DATA = data.DataList.Tables[1].Copy();


                if (checkEdit_SAVE_ID.Checked)
                {
                    if (!ConstClass._DEBUG_MODE)
                    {
                        ConstClass._REG.SetValue("COMP_CD", comp_cd.Trim());
                        ConstClass._REG.SetValue("USR_ID", id.Trim());
                    }
                }
                else
                {
                    if (!ConstClass._DEBUG_MODE)
                    {
                        ConstClass._REG.SetValue("COMP_CD", "");
                        ConstClass._REG.SetValue("USR_ID", "");
                    }
                }

                ConstClass._USR_ID = id.ToLower().Trim();
            }
            catch (Exception ex)
            {
                aStr = ex.Message;
            }

            return aStr;
        }

        private void LoginProc()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                string comp_cd = UtilClass.toStr(lookUpEdit_COMP_CD.EditValue);
                if (UtilClass.isNull(comp_cd))
                {
                    lookUpEdit_COMP_CD.Focus();
                    throw new Exception("사업장을 선택하세요.");
                }

                string id = textEdit_LOGIN_ID.Text;
                if (UtilClass.isNull(id))
                {
                    textEdit_LOGIN_ID.Focus();
                    throw new Exception("아이디를 입력하세요");
                }

                string pwd = textEdit_PASSWD.Text;
                if (UtilClass.isNull(pwd))
                {
                    textEdit_PASSWD.Focus();
                    throw new Exception("패스워드를 입력하세요.");
                }

                string resultStr = tryLogin(comp_cd, textEdit_LOGIN_ID.Text, textEdit_PASSWD.Text);
                if (!UtilClass.isNull(resultStr))
                {
                    throw new Exception(resultStr);
                }

                this.Cursor = Cursors.Arrow;

                this.DialogResult = DialogResult.Yes;
                this.Close();
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Arrow;
                MessageBox.Show(ex.Message);
            }
        }
    }
}