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 СТАТУСЫ_выдача_заказа : Form
    {
        public СТАТУСЫ_выдача_заказа()
        {
            InitializeComponent();
        }

        //сканер, звуки
        public string putin = "";
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == "СТАТУСЫ_клавиатура_для_ввода_номера")
                {
                    MessageBox.Show("Клавиатура для номера заказа уже открыта!");
                    return;
                }
            }

            СТАТУСЫ_клавиатура_для_ввода_номера form1 = new СТАТУСЫ_клавиатура_для_ввода_номера();
            form1.Owner = this;
            form1.Show();
        }

        public void button2_Click(object sender, EventArgs e)
        {
            if (maskedTextBox2.Text == "")
            {
                MessageBox.Show("Номер заказа не может быть пустым!");
                return;
            }
            maskedTextBox2.Text = maskedTextBox2.Text.ToUpper().Replace("А", "A").Replace("В", "B").Replace("С", "C").Replace("Е", "E").Replace("Т", "T").Replace("О", "O").Replace("Р", "P").Replace("Н", "H").Replace("К", "K").Replace("Х", "X").Replace("М", "M");

            if (Directory.Exists(putin + "\\baza\\" + maskedTextBox2.Text))
            {
                if (!File.Exists(putin + "\\baza\\" + maskedTextBox2.Text + "\\zabran.akv"))
                {
                    if (File.Exists(putin + "\\baza\\" + maskedTextBox2.Text + "\\zakaz.akv"))
                    {
                        string h = "";
                        StreamReader Sr = new StreamReader(new FileStream(putin + "\\baza\\" + maskedTextBox2.Text + "\\zakaz.akv", FileMode.Open, FileAccess.Read, FileShare.Read));
                        h = Sr.ReadToEnd();
                        Sr.Close();
                        string[] tmp = h.Split('|');

                        double ostatok = 0;

                        if (File.Exists(putin + "\\baza\\" + maskedTextBox2.Text + "\\doplata.akv"))
                        {
                            string str33 = "";
                            double opl = Convert.ToDouble(tmp[11]);
                            using (StreamReader sr = new StreamReader(new FileStream(putin + "\\baza\\" + maskedTextBox2.Text + "\\doplata.akv", FileMode.Open, FileAccess.Read, FileShare.Read)))
                            {
                                while ((str33 = sr.ReadLine()) != null)
                                {
                                    if (str33 == "") continue;
                                    opl += Convert.ToDouble(str33.Split('|')[1]);
                                }
                            }
                            // textBox10.Text = opl.ToString();
                            ostatok = Convert.ToDouble(tmp[12]) - opl;
                        }
                        else
                        {
                            //textBox10.Text = tmp[11];
                            ostatok = Convert.ToDouble(tmp[12]) - Convert.ToDouble(tmp[11]);
                        }
                        if (ostatok <= 0)
                        {
                            //все заплачено
                            string fg = DateTime.Now + "|Мастерская|";
                            File.WriteAllText(putin + "\\baza\\" + maskedTextBox2.Text + "\\zabran.akv", fg, Encoding.UTF8);

                            label1.Text = "Заказ №" + maskedTextBox2.Text + " успешно выдан!";
                            pictureBox1.Image = Properties.Resources.correct_vinkje;
                            try
                            {
                                System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
                                sp.Stream = Properties.Resources.ok;
                                sp.Play();
                                sp.Dispose();
                            }
                            catch { }

                            maskedTextBox2.Text = "";
                        }
                        else
                        {
                            //футболим
                            label1.Text = "Заказ №" + maskedTextBox2.Text + " выдача запрещена!\r\nТребуется доплата!";
                            pictureBox1.Image = Properties.Resources.w256h2561339405847Delete256;
                            try
                            {
                                System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
                                sp.Stream = Properties.Resources.bad;
                                sp.Play();
                                sp.Dispose();
                            }
                            catch { }
                            maskedTextBox2.Text = "";
                        }
                    }
                    else
                    {
                        label1.Text = "Заказ №" + maskedTextBox2.Text + " поврежден!";
                        try
                        {
                            System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
                            sp.Stream = Properties.Resources.err;
                            sp.Play();
                            sp.Dispose();
                        }
                        catch { }
                        pictureBox1.Image = Properties.Resources.DocScrap;

                    }
                }
                else
                {
                    label1.Text = "Заказ №" + maskedTextBox2.Text + " уже выдан!";
                    try
                    {
                        System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
                        sp.Stream = Properties.Resources.err;
                        sp.Play();
                        sp.Dispose();
                    }
                    catch { }
                    pictureBox1.Image = Properties.Resources.w256h2561339405847Delete256;
                }
            }
            else
            {
                label1.Text = "Заказ №" + maskedTextBox2.Text + " не существует!";
                try
                {
                    System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
                    sp.Stream = Properties.Resources.err;
                    sp.Play();
                    sp.Dispose();
                }
                catch { }
                pictureBox1.Image = Properties.Resources.icon_not_found;

            }
            maskedTextBox2.Text = "";
        }
    }
}
