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 Microsoft.VisualBasic.ApplicationServices;
using System.Net;
using System.Xml.Linq;
using PackingControlSystem.lib;

namespace PackingControlSystem
{
    public partial class frmCustomer : Form
    {
        private List<List<string>> Settings = new List<List<string>>();
        DataTable dtCustomer = new DataTable();
        DataTable dtCustomerSelect = new DataTable();
        private string customerID = string.Empty;
        private string userName = string.Empty;

        public frmCustomer(string systemUserName)
        {
            InitializeComponent();
            userName = systemUserName;
        }

        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 frmCustomer_Load(object sender, EventArgs e)
        {
            List<string> tmp = null;
            dgvCustomer.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            //listBox1.HorizontalScrollbar = true;
            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

            loadTable();

            SettxtReadOnly();
        }

        private void loadTable()
        {
            dtCustomer = new DataTable();
            clsUtil_MSSQL.GetDataFromTBCustomerMaster(RtnDbCon(), "TM_CUSTOMER", ref dtCustomer);
            dgvCustomer.DataSource = dtCustomer;
            //dgvPart.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
            dgvCustomer.RowHeadersWidth = 70;

            for (int i = 0; i < dgvCustomer.Rows.Count; i++)
            {
                if (i % 2 != 0)
                {

                    dgvCustomer.Rows[i].DefaultCellStyle.BackColor = SystemColors.ControlLight;

                }
                else
                {


                }

            }

        }

        private void SettxtReadOnly()
        {
            txtCustomerID.ReadOnly = true;
            txtCustomerName.ReadOnly = true;
            txtCustomerID.BackColor = SystemColors.Control;
            txtCustomerName.BackColor = SystemColors.Control;
            btnSave.Enabled = false;
            btnClear.Enabled = false;
            btnAdd.Enabled = true;
        }

        private void dgvCustomer_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Add row number
            (sender as DataGridView).Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex + 1).ToString();

            int idx = e.RowIndex;
            DataGridViewRow row = dgvCustomer.Rows[idx];
            row.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private void dgvCustomer_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SettxtWrite();

            txtCustomerID.ReadOnly = true;
            txtCustomerID.BackColor = Color.WhiteSmoke;

            dtCustomerSelect = new DataTable();
            int rowindex = dgvCustomer.CurrentRow.Index;
            clsUtil_MSSQL.GetDataFromTBCustomerSelect(RtnDbCon(), "TM_CUSTOMER", ref dtCustomerSelect, dgvCustomer[0, rowindex].Value.ToString());
            customerID = dgvCustomer[0, rowindex].Value.ToString();
            txtCustomerID.Text = dtCustomerSelect.Rows[0][1].ToString();
            txtCustomerName.Text = dtCustomerSelect.Rows[0][2].ToString();

        }

        private void SettxtWrite()
        {
            txtCustomerID.ReadOnly = false;
            txtCustomerName.ReadOnly = false;
            txtCustomerID.BackColor = SystemColors.Window;
            txtCustomerName.BackColor = SystemColors.Window;
            btnAdd.Enabled = false;
            btnSave.Enabled = true;
            btnClear.Enabled = true;            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
            SettxtWrite();
            btnDelete.Enabled = false;
            customerID = string.Empty;
            txtCustomerID.Focus();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            SettxtReadOnly();
            SettxtStart();
            customerID = string.Empty;
        }

        private void SettxtStart()
        {
            txtCustomerID.Text = string.Empty;
            txtCustomerName.Text = string.Empty;
            btnDelete.Enabled = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (txtCustomerID.Text.Trim() == string.Empty)
            {
                MessageBox.Show(this, "กรุณาใส่ข้อมูล Customer ID ก่อนบันทึก",
                                       "Customer Information", MessageBoxButtons.OK,
                                       MessageBoxIcon.Warning,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }

            if (customerID != string.Empty)
            {
                clsUtil_MSSQL.updateCustomer(RtnDbCon(), customerID, txtCustomerName.Text,
                userName, DateTime.Now.ToString("yyyyMMdd"),
                DateTime.Now.ToString("HHmmss"));

                MessageBox.Show(this, "แก้ไขข้อมูล Customer Information เรียบร้อย",
                                   "Customer Information", MessageBoxButtons.OK,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button1, 0);

                loadTable();

                SettxtReadOnly();
                SettxtStart();
                customerID = string.Empty;

            }

            else if (customerID == string.Empty)
            {

                dtCustomerSelect = new DataTable();
                clsUtil_MSSQL.GetDataFromTBCustomerSelect(RtnDbCon(), "TM_CUSTOMER", ref dtCustomerSelect, txtCustomerID.Text);
                if (dtCustomerSelect.Rows.Count > 0)
                {
                    MessageBox.Show(this, "ไม่สามารถเพิ่มข้อมูล Customer Information ได้ เนื่องจาก Customer ID นี่มีอยู่แล้วในระบบ",
                                   "Customer Information", MessageBoxButtons.OK,
                                   MessageBoxIcon.Error,
                                   MessageBoxDefaultButton.Button1, 0);
                    return;
                }
                clsUtil_MSSQL.insertCustomer(RtnDbCon(), txtCustomerID.Text, txtCustomerName.Text, "Y", userName,
                    DateTime.Now.ToString("yyyyMMdd"),
                    DateTime.Now.ToString("HHmmss"), userName, DateTime.Now.ToString("yyyyMMdd"),
                    DateTime.Now.ToString("HHmmss"));

                MessageBox.Show(this, "เพิ่มข้อมูล Customer Information เรียบร้อย",
                                   "Customer Information", MessageBoxButtons.OK,
                                   MessageBoxIcon.Information,
                                   MessageBoxDefaultButton.Button1, 0);

                loadTable();

                SettxtReadOnly();
                SettxtStart();
                customerID = string.Empty;

            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtCustomerID.Text == string.Empty)
            {
                MessageBox.Show(this, "กรุณาเลือกรายการที่จะลบก่อน",
                                       "Customer Information", MessageBoxButtons.OK,
                                       MessageBoxIcon.Warning,
                                       MessageBoxDefaultButton.Button1, 0);
                return;
            }


            DialogResult dialogResult = MessageBox.Show(this, "ท่านต้องการที่ลบรายการนี้แน่ใจหรือไม่?",
                                   "Customer Information", MessageBoxButtons.YesNo,
                                   MessageBoxIcon.Question,
                                   MessageBoxDefaultButton.Button1, 0);

            if (dialogResult == DialogResult.Yes)
            {
                if (customerID != string.Empty)
                {
                    clsUtil_MSSQL.deleteCustomer(RtnDbCon(), customerID);

                    MessageBox.Show(this, "ลบข้อมูลรายการนี้เรียบร้อย",
                                       "Customer Information", MessageBoxButtons.OK,
                                       MessageBoxIcon.Information,
                                       MessageBoxDefaultButton.Button1, 0);

                    loadTable();

                    SettxtReadOnly();
                    SettxtStart();
                    customerID = string.Empty;
                }
                else if (dialogResult == DialogResult.No)
                {
                    return;
                }



            }
        }

        private void dgvCustomer_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
