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.Windows.Forms;

namespace StekloMaster
{
    public partial class Form9 : Form
    {
        public Form9()
        {
            InitializeComponent();
        }

        public string edit = "";
        bool load = false;
        private void Form9_Shown(object sender, EventArgs e)
        {
            Form1 g = this.Owner as Form1;
            int c = 0;
            string str33 = "";
            try
            {
                using (StreamReader sr = new StreamReader(g.putin + "\\data\\skinali\\virez.akv"))
                {
                    comboBox2.BeginUpdate();
                    comboBox2.Items.Clear();
                    while ((str33 = sr.ReadLine()) != null)
                    {
                        if (str33 != "" && str33 != null)
                        {
                            c++;
                            string[] tmp = str33.Replace("\r", "").Split('|');
                            comboBox2.Items.AddRange(new object[] { tmp[0] });
                        }

                    }
                    if (comboBox2.Items.Count != 0)
                        comboBox2.SelectedIndex = 0;
                    comboBox2.EndUpdate();
                }
            }
            catch
            {
                comboBox2.EndUpdate();
            }
            if (edit != "")
            {
                button1.Text = "Редактировать";
                string[] s = edit.Split('|');
                comboBox2.Text = s[1];
                load = true;
                numericUpDown1.Value = Convert.ToDecimal(s[2]);
                load = false;
                numericUpDown2.Value = Convert.ToDecimal(s[3]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //цена?!

            Form1 g = this.Owner as Form1;
            if ("Создать отверстие" != button1.Text)
            {
                int i = g.dataGridView2.CurrentRow.Index;

                g.dataGridView2.Rows[i].Cells[1].Value = comboBox2.Text;
                g.dataGridView2.Rows[i].Cells[2].Value = numericUpDown1.Value;
                g.dataGridView2.Rows[i].Cells[3].Value = numericUpDown2.Value;
                //обновление
                string s = "";
                for (i = 0; i < g.dataGridView2.Rows.Count; i++)
                {
                    for (int j = 0; j < 4; j++)
                        s += g.dataGridView2.Rows[i].Cells[j].Value + "@";
                    s += "`";
                }
                g.dataGridView1.Rows[g.dataGridView1.CurrentRow.Index].Cells[10].Value = s;
                //разблок
                g.dataGridView1.Enabled = true;
                g.button12.Enabled = true;
                g.button13.Enabled = true;
                g.button14.Enabled = true;
                g.dataGridView2.Enabled = true;
                g.button16.Enabled = true;
                g.button17.Enabled = true;
                g.button18.Enabled = true;
            }
            else
            {
                //Создание
                
                int i = g.dataGridView2.Rows.Count;
                g.dataGridView2.Rows.Add();

                g.dataGridView2.Rows[i].Cells[0].Value = i + 1;
                g.dataGridView2.Rows[i].Cells[1].Value = comboBox2.Text;
                g.dataGridView2.Rows[i].Cells[2].Value = numericUpDown1.Value;
                g.dataGridView2.Rows[i].Cells[3].Value = numericUpDown2.Value;

                g.dataGridView2.Rows[i].Selected = true;
                g.dataGridView2.FirstDisplayedScrollingRowIndex = i;
                
                //обновление
                string s = "";
                 for (i = 0; i < g.dataGridView2.Rows.Count; i++)
                    {
                        for (int j = 0; j < 5; j++)
                            s += g.dataGridView2.Rows[i].Cells[j].Value + "@";
                        s += "`";
                    }
                 g.dataGridView1.Rows[g.dataGridView1.CurrentRow.Index].Cells[10].Value = s;
                //разблок
                 g.dataGridView1.Enabled = true;
                 g.button12.Enabled = true;
                 g.button13.Enabled = true;
                 g.button14.Enabled = true;
            }
            g.raschetdirok();
            g.sum();
            this.Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            Form1 g = this.Owner as Form1;
            g.dataGridView1.Enabled = true;
            g.button12.Enabled = true;
            g.button13.Enabled = true;
            g.button14.Enabled = true;
            if ("Создать панель" != button1.Text)
            {
                //при редактировании
                g.dataGridView2.Enabled = true;
                g.button16.Enabled = true;
                g.button17.Enabled = true;
                g.button18.Enabled = true;
            }

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (load) return;
            Form1 g = this.Owner as Form1;
            string str33 = "";
            try
            {
                using (StreamReader sr = new StreamReader(g.putin + "\\data\\skinali\\virez.akv"))
                {
                    while ((str33 = sr.ReadLine()) != null)
                    {
                        if (str33 != "" && str33 != null)
                        {
                            string[] tmp = str33.Replace("\r", "").Split('|');
                            if (tmp[0] == comboBox2.Text)
                            {
                                numericUpDown1.Value = Convert.ToDecimal(tmp[1]);
                                break;
                            }
                        }

                    }
                   
                }
            }
            catch
            {
            }
            
        }
    }
}
