using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Formats.Asn1;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using CsvReadWrite;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Drawing.Imaging;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using PackingControlSystem.lib;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace PackingControlSystem
{
    public partial class frmInputPassword : Form
    {
        private List<List<string>> Settings = new List<List<string>>();
        DataTable dtUserSelect = new DataTable();
        private string serial = string.Empty;
        bool isClose = false;
        public frmInputPassword(string serial)
        {
            InitializeComponent();
            this.serial = serial;
            lblWarning.Text = "";
            frmPacking.strMSG = "";
        }

        public string RtnDbCon()
        {
            return "Data Source=" + this.Settings[0][0] + ";Initial Catalog=" + this.Settings[0][1] + ";User Id=sa;Password=" + this.Settings[0][2] + ";";

        }



        private void SettxtStart()
        {
            isClose = false;
            //txtUserID.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtPassword.Focus();
        }



        private void frmInputPassword_Load(object sender, EventArgs e)
        {
            frmSettingPLC.blConfirm = false;

            List<string> tmp = null;
            Settings = new List<List<string>>();
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\Settings.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    Settings.Add(tmp);

                }

            }//using end
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if ((txtPassword.Text.Trim() == string.Empty) || (txtPassword.Text.Trim() != this.Settings[1][1]))
            {
                MessageBox.Show(this, "Password ไม่ถูกต้อง กรุณาใส่ Password อีกครั้ง",
                                      "ERROR", MessageBoxButtons.OK,
                                      MessageBoxIcon.Error,
                                      MessageBoxDefaultButton.Button1, 0);

                SettxtStart();

                return;
            }
            else
            {
                frmSettingPLC.blConfirm = true;
                isClose = true;
                this.Close();
            }

        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Return)
            {
                if ((txtPassword.Text.Trim() == string.Empty) || (txtPassword.Text.Trim() != this.Settings[1][1]))
                {
                    MessageBox.Show(this, "Password ไม่ถูกต้อง กรุณาใส่ Password อีกครั้ง",
                                          "ERROR", MessageBoxButtons.OK,
                                          MessageBoxIcon.Error,
                                          MessageBoxDefaultButton.Button1, 0);

                    SettxtStart();

                    return;
                }
                else
                {
                    frmSettingPLC.blConfirm = true;
                    isClose = true;
                    this.Close();
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            frmSettingPLC.blConfirm = false;
            isClose = true;
            this.Close();
        }

        private void frmInputPassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isClose == false)
            {
                e.Cancel = true;
            }
        }
    }
}
