using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StekloMaster
{
    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }
        string putin = "";

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == "Form5")
                {
                    MessageBox.Show("Информация о клиенте открыта!");
                    return;
                }
            }

            Form5 f1 = new Form5();
            f1.Owner = this;
            f1.zag("Создать клиента и выбрать");
            f1.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) return;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == "Form5")
                {
                    MessageBox.Show("Информация о клиенте открыта!");
                    return;
                }
            }

            Form5 f1 = new Form5();
            f1.Owner = this;
            f1.zag2("Редактировать клиента", dataGridView1.Rows[dataGridView1.CurrentRow.Index]);
            f1.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) return;
            if (MessageBox.Show("Вы уверены что хотите удалить клиента?",Application.ProductName , MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                int c = 0;
                string str33 = "";
                string s = "";
                try
                {
                    string nom = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value.ToString();
                    using (StreamReader sr = new StreamReader(putin + "\\users.akv"))
                    {
                        while ((str33 = sr.ReadLine()) != null)
                        {
                            if (c.ToString() != nom)
                            {
                                s += str33 + "\r\n";
                            }
                            c++;
                        }
                        sr.Close();
                        File.WriteAllText(putin + "\\users.akv", s, Encoding.UTF8);
                    }
                    string oldklient = "";
                    for (int ii = 0; ii < 8; ii++)
                    {
                        oldklient += dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[ii].Value + "|";
                    }
                    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                }
                catch { }
                c = 0;
                str33 = null;
                s = null;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) return;
            Form1 fs = this.Owner as Form1;
            fs.loadklient(dataGridView1.Rows[dataGridView1.CurrentRow.Index]);
            this.Close();
        }

        public void load(string paramert = "")
        {
            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
            int c = 0;
            string str33 = "";
            try
            {
                dataGridView1.Rows.Clear();
                using (System.IO.StreamReader sr = new System.IO.StreamReader(putin + "\\users.akv"))
                {

                    while ((str33 = sr.ReadLine()) != null)
                    {
                        if (str33 == null) continue;
                        int i = dataGridView1.Rows.Count;
                        if (i == 50)
                            break;
                        dataGridView1.Rows.Add();
                        string[] tmp = str33.Split('|');
                        dataGridView1.Rows[i].Cells[0].Value = tmp[0];
                        dataGridView1.Rows[i].Cells[1].Value = tmp[1];
                        dataGridView1.Rows[i].Cells[2].Value = tmp[2];
                        if (tmp[3] == "1")
                            dataGridView1.Rows[i].Cells[3].Value = "Ж";
                        else
                            dataGridView1.Rows[i].Cells[3].Value = "М";
                        dataGridView1.Rows[i].Cells[4].Value = tmp[4];
                        dataGridView1.Rows[i].Cells[5].Value = tmp[5];
                        dataGridView1.Rows[i].Cells[6].Value = tmp[6];
                        dataGridView1.Rows[i].Cells[7].Value = tmp[7];
                        dataGridView1.Rows[i].Cells[8].Value = tmp[8];
                        int mh = Convert.ToInt32(tmp[6]);
                        int dh = Convert.ToInt32(tmp[5]);
                        if (mh == DateTime.Today.Month && dh == DateTime.Today.Day)
                        {
                            dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.PeachPuff;
                            dataGridView1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                            dataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;
                        }
                        else
                        {
                            try
                            {
                                DateTime d1 = new DateTime(DateTime.Today.Year, mh, dh);
                                TimeSpan time = DateTime.Today - d1;
                                if (Math.Abs(time.Days) <= 5)
                                {
                                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                                    dataGridView1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Cyan;
                                    dataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;
                                }
                            }
                            catch { }
                        }
                        dataGridView1.Rows[i].Cells[9].Value = c;
                        c++;
                    }
                    sr.Close();
                    dataGridView1.Rows[0].Selected = true;
                    dataGridView1.FirstDisplayedScrollingRowIndex = 0;
                }
            }
            catch { }
            c = 0;
            str33 = null;
            if(paramert!="")
            {
                textBox12.Text = paramert;
                button46_Click(null, null);
                button4_Click(null, null);
            }
        }

        public void load_and_vibor()
        {
            load();
            button4_Click(null, null);

        }

        public void reload(string id, DataGridViewRow rw)
        {
            int ids = Convert.ToInt32(id);
            for(int h = 0; h<dataGridView1.Rows.Count; h++)
            {
                if(dataGridView1.Rows[h].Cells[9].Value.ToString() == id)
                {
                    for (int i = 0; i < rw.Cells.Count; i++)
                        dataGridView1.Rows[h].Cells[i].Value = rw.Cells[i].Value;
                    break;
                }
            }
           
        }

        public void badklient(DataGridViewRow rw)
        {
            Form1 fs = this.Owner as Form1;
            fs.loadklient(rw);
            this.Close();
        }

        private void Form6_Shown(object sender, EventArgs e)
        {
            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
        }

        private void button45_Click(object sender, EventArgs e)
        {
            load();
        }

        private void button46_Click(object sender, EventArgs e)
        {
            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
            Check_class chf = new Check_class();
            int c = 0;
            string str33 = "";
            try
            {
                dataGridView1.Rows.Clear();
                using (System.IO.StreamReader sr = new System.IO.StreamReader(putin + "\\users.akv"))
                {

                    while ((str33 = sr.ReadLine()) != null)
                    {
                        if (chf.sravnenie(str33, textBox12.Text))
                        {
                            int i = dataGridView1.Rows.Count;
                            dataGridView1.Rows.Add();
                            string[] tmp = str33.Split('|');
                            dataGridView1.Rows[i].Cells[0].Value = tmp[0];
                            dataGridView1.Rows[i].Cells[1].Value = tmp[1];
                            dataGridView1.Rows[i].Cells[2].Value = tmp[2];
                            if (tmp[3] == "1")
                                dataGridView1.Rows[i].Cells[3].Value = "Ж";
                            else
                                dataGridView1.Rows[i].Cells[3].Value = "М";
                            dataGridView1.Rows[i].Cells[4].Value = tmp[4];
                            dataGridView1.Rows[i].Cells[5].Value = tmp[5];
                            dataGridView1.Rows[i].Cells[6].Value = tmp[6];
                            dataGridView1.Rows[i].Cells[7].Value = tmp[7];
                            dataGridView1.Rows[i].Cells[8].Value = tmp[8];
                            int mh = Convert.ToInt32(tmp[6]);
                            int dh = Convert.ToInt32(tmp[5]);
                            if (mh == DateTime.Today.Month && dh == DateTime.Today.Day)
                            {
                                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.PeachPuff;
                                dataGridView1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                                dataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;
                            }
                            else
                            {
                                try
                                {
                                    DateTime d1 = new DateTime(DateTime.Today.Year, mh, dh);
                                    TimeSpan time = DateTime.Today - d1;
                                    if (Math.Abs(time.Days) <= 5)
                                    {
                                        dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                                        dataGridView1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Cyan;
                                        dataGridView1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;
                                    }
                                }
                                catch { }
                            }
                            dataGridView1.Rows[i].Cells[9].Value = c;
                        }
                        c++;
                    }
                    sr.Close();
                    dataGridView1.Rows[0].Selected = true;
                    dataGridView1.FirstDisplayedScrollingRowIndex = 0;
                }
            }
            catch { }
            c = 0;
            str33 = null;
        }

        private void textBox12_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button46_Click(null, null);
            }
        }


        private void button5_Click(object sender, EventArgs e)
        {
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == "Form4")
                {
                    MessageBox.Show("База клиентов уже открыта!");
                    return;
                }
            }
            Form4 f1 = new Form4();
            f1.Owner = this;
            f1.Show();
        }
    }
}
