using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderSheetSystem.lib;
using OrderSheetSystem.model;
using lib;

namespace OrderSheetSystem
{
    public partial class FrmPartNoOrder : Form
    {
        
        public FrmPartNoOrder()
        {
            InitializeComponent();
                        
        }

        private void FrmPartNoOrder_Load(object sender, EventArgs e)
        {
            List<string> tmp = null;
            List<PartNoOrderMaster> PartNoOrder = new List<PartNoOrderMaster>();
            Int32 i = 0;
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\PartNoOrder.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {


                    if (tmp[0] != "")
                    {

                        tmp.Sort();

                        for (int i1 = 0; i1 < tmp.Count(); i1++)
                        {
                            PartNoOrder.Add(new PartNoOrderMaster() { No = i + 1, PartNoOrder = tmp[i] });
                            i++;
                        }
                    }

                }

            }//using end


            dgvPartNoOrderList.DataSource = PartNoOrder;
            dgvPartNoOrderList.Columns[1].Width = 660;
            dgvPartNoOrderList.Columns[0].HeaderText = "No.";
            dgvPartNoOrderList.Columns[1].HeaderText = "Part No. Order";
            dgvPartNoOrderList.Columns[1].SortMode = DataGridViewColumnSortMode.Automatic;

            //dgvCustomerList.Sort(dgvCustomerList.Columns[1], ListSortDirection.Ascending);

            if (dgvPartNoOrderList.RowCount > 0)
            {
                string data = (string)dgvPartNoOrderList[1, 0].Value;

                txtPartNoOrder.Text = data;
            }

        }

        private void dgvPartNoOrderList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                txtPartNoOrder.Text = dgvPartNoOrderList[1, e.RowIndex].Value.ToString();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<string> tmp = null;

            List<PartNoOrderMaster> PartNoOrder = new List<PartNoOrderMaster>();
            List<string> tmp11 = new List<string>();
            Int32 i = 0;

            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\PartNoOrder.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {
                    tmp.Sort();

                    for (int i1 = 0; i1 < tmp.Count(); i1++)
                    {
                        PartNoOrder.Add(new PartNoOrderMaster() { No = i1 + 1, PartNoOrder = tmp[i1] });
                        tmp11.Add(tmp[i1]);
                        i++;
                    }



                    //}


                }

            }//using end

            if (!tmp11.Contains(txtPartNoOrder.Text))
            {
                PartNoOrder.Add(new PartNoOrderMaster() { No = i + 1, PartNoOrder = txtPartNoOrder.Text });
            }
            else
            {
                MessageBox.Show("เพิ่ม Part No. Order ซ้ำ", "คำเตือน", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            List<string> tmp1 = new List<string>();
            List<List<string>> tmpList = new List<List<string>>();

            using (var csv = new CsvWriter(System.IO.Directory.GetCurrentDirectory() + "\\PartNoOrder.csv"))
            {
                foreach (var item in PartNoOrder)
                {
                    tmp1.Add(item.PartNoOrder.ToString());
                    //csv.Write(item.Customer.ToString());



                }

                tmpList.Add(tmp1);

                csv.Write(tmpList);

            }



            List<string> tmp2 = null;
            List<PartNoOrderMaster> PartNoOrder2 = new List<PartNoOrderMaster>();
            Int32 i2 = 0;
            tmp2 = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\PartNoOrder.csv"))
            {
                while ((tmp2 = csv.ReadRow()) != null)
                {


                    if (tmp2[0] != "")
                    {

                        tmp2.Sort();

                        for (int i1 = 0; i1 < tmp2.Count(); i1++)
                        {
                            PartNoOrder2.Add(new PartNoOrderMaster() { No = i2 + 1, PartNoOrder = tmp2[i2] });
                            i2++;
                        }
                    }

                }

            }//using end




            dgvPartNoOrderList.DataSource = PartNoOrder2;

            //dgvCustomerList.Sort(dgvCustomerList.Columns["Customer"], ListSortDirection.Ascending);

            //int nRowIndex = dgvPartNoOrderList.Rows.Count - 1;
            //int nColumnIndex = 0;

            dgvPartNoOrderList.ClearSelection();

            foreach (DataGridViewRow row in dgvPartNoOrderList.Rows)
            {
                // 1 is the column index
                if (row.Cells[1].Value.ToString().Equals(txtPartNoOrder.Text.Trim()))
                {
                    row.Selected = true;
                    break;
                }
            }

            //dgvPartNoOrderList.Rows[nRowIndex].Selected = true;
            //dgvPartNoOrderList.Rows[nRowIndex].Cells[nColumnIndex].Selected = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> tmp = null;
            
            List<PartNoOrderMaster> PartNoOrder = new List<PartNoOrderMaster>();
            Int32 i = 0;
            Int32 n = 0;
            
            tmp = null;
            using (var csv = new CsvReader(System.IO.Directory.GetCurrentDirectory() + "\\PartNoOrder.csv"))
            {
                while ((tmp = csv.ReadRow()) != null)
                {

                  
                    n = 0;

                    tmp.Sort();

                    for (int i1 = 0; i1 < tmp.Count(); i1++)
                    {
                        if (tmp[i1] != txtPartNoOrder.Text)
                        {
                            PartNoOrder.Add(new PartNoOrderMaster() { No = n + 1, PartNoOrder = tmp[i1] });
                            n++;
                            i++;
                        }

                    }



                }

            }//using end


            List<string> tmp1 = new List<string>();
            List<List<string>> tmpList = new List<List<string>>();

            using (var csv = new CsvWriter(System.IO.Directory.GetCurrentDirectory() + "\\PartNoOrder.csv"))
            {
                foreach (var item in PartNoOrder)
                {
                    tmp1.Add(item.PartNoOrder.ToString());


                }

                tmpList.Add(tmp1);
                                
                if (tmp1.Count != 0)
                {
                    csv.Write(tmpList);
                }

            }

            dgvPartNoOrderList.DataSource = PartNoOrder;

            //dgvCustomerList.Sort(dgvCustomerList.Columns["Customer"], ListSortDirection.Ascending);

            int nRowIndex = dgvPartNoOrderList.Rows.Count - 1;
            int nColumnIndex = 0;

            txtPartNoOrder.Text = string.Empty;

            dgvPartNoOrderList.ClearSelection();

            //dgvCustomerList.Rows[nRowIndex].Selected = true;
            //dgvCustomerList.Rows[nRowIndex].Cells[nColumnIndex].Selected = true;
        }
    }
}
