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 Form11 : Form
    {
        public Form11()
        {
            InitializeComponent();
        }
        string putin = "";
        private void Form11_Load(object sender, EventArgs e)
        {
            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
            StreamReader sr2 = new StreamReader(putin + "\\podem.akv");
            string k2 = sr2.ReadToEnd();
            sr2.Close();
            string[] rn2 = k2.Split('\n');
            foreach (string s in rn2)
            {
                string tmp = s.Replace("\r", "");
                if (tmp == "" || tmp == null) continue;
                int i = dataGridView2.Rows.Count;
                dataGridView2.Rows.Add();
                dataGridView2.Rows[i].Cells[0].Value = tmp.Split('|')[0];
                dataGridView2.Rows[i].Cells[1].Value = tmp.Split('|')[1];
            }
            sr2 = new StreamReader(putin + "\\raone.akv");
            k2 = sr2.ReadToEnd();
            sr2.Close();
            string[] rn = k2.Split('\n');
            foreach(string s in rn)
            {
                string tmp = s.Replace("\r", "");
                if (tmp == "" || tmp == null) continue;
                int i = dataGridView1.Rows.Count;
                dataGridView1.Rows.Add();
                dataGridView1.Rows[i].Cells[0].Value = tmp.Split('|')[0];
                dataGridView1.Rows[i].Cells[1].Value = tmp.Split('|')[1];
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar)) return;
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                    e.Handled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) return;
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
            string otv = "";
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                otv += dataGridView1.Rows[i].Cells[0].Value + "|" + dataGridView1.Rows[i].Cells[1].Value + "\r\n";
            }
            File.WriteAllText(putin + "\\raone.akv", otv, Encoding.UTF8);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string otv = "";
            for(int i = 0; i<dataGridView1.Rows.Count; i++)
            {
                otv += dataGridView1.Rows[i].Cells[0].Value + "|" + dataGridView1.Rows[i].Cells[1].Value + "\r\n";
            }
            otv += textBox2.Text + "|" + textBox3.Text + "\r\n";
            File.WriteAllText(putin + "\\raone.akv", otv, Encoding.UTF8);
            int s = dataGridView1.Rows.Count;
            dataGridView1.Rows.Add();
            dataGridView1.Rows[s].Cells[0].Value = textBox2.Text;
            dataGridView1.Rows[s].Cells[1].Value = textBox3.Text;
            textBox2.Text = "";
            textBox3.Text = "";
            button5_Click(null, null);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            panel2.Visible = true;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            panel2.Visible = false;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string otv = "";
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                otv += dataGridView2.Rows[i].Cells[0].Value + "|" + dataGridView2.Rows[i].Cells[1].Value + "\r\n";
            }
            otv += textBox5.Text + "|" + textBox4.Text + "\r\n";
            File.WriteAllText(putin + "\\podem.akv", otv, Encoding.UTF8);
            int s = dataGridView2.Rows.Count;
            dataGridView2.Rows.Add();
            dataGridView2.Rows[s].Cells[0].Value = textBox5.Text;
            dataGridView2.Rows[s].Cells[1].Value = textBox4.Text;
            textBox5.Text = "";
            textBox4.Text = "";
            button8_Click(null, null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (dataGridView2.CurrentRow == null) return;
            dataGridView2.Rows.RemoveAt(dataGridView2.CurrentRow.Index);
            string otv = "";
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                otv += dataGridView2.Rows[i].Cells[0].Value + "|" + dataGridView2.Rows[i].Cells[1].Value + "\r\n";
            }
            File.WriteAllText(putin + "\\podem.akv", otv, Encoding.UTF8);
        }
    }
}
