using GenCode128;
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 Form19 : Form
    {
        public Form19()
        {
            InitializeComponent();
        }

        public string zakaz = "";

        public string ostatok = "";
        string putin = "";
        double ost = 0;
        public bool flag = false;

        private void Form19_Shown(object sender, EventArgs e)
        {
            if (Application.StartupPath.IndexOf("comp") != -1)
            {
                putin = Application.StartupPath.Substring(0, Application.StartupPath.IndexOf("\\comp"));
            }
            else
            {
                putin = Application.StartupPath;
            }
            if(zakaz.IndexOf(">") == zakaz.Length - 1)
            {
                label2.Text = "Номер заказа: " + zakaz.Replace("<", "").Replace(">", "");
                label1.Text = "Максимальная сумма платежки: " + ostatok + " руб.";
                ost = Convert.ToDouble(ostatok);
            }
            else
            {
                string zak = zakaz.Replace("<", "").Replace(">", " ");
                if (zak.Length > 38) zak = zak.Substring(0, 38) + "...";
                label2.Text = "Номера заказов: " + zak;
                label1.Text = "Максимальная сумма платежки: " + ostatok + " руб.";
                ost = Convert.ToDouble(ostatok);
            }
            if(flag)
            {
                textBox7.Text = ostatok;
                textBox7.Enabled = false;
            }
        }

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

        private void textBox7_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToDouble(textBox7.Text) > ost)
                    textBox7.Text = ost.ToString();
            }
            catch { }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox7.Text == "")
            {
                MessageBox.Show("Не указана сумма доплаты!");
                return;
            }
            if (zakaz.IndexOf(">") == zakaz.Length - 1)
            {
                string f = "";
                if (File.Exists(putin + "\\baza\\" + zakaz.Replace("<", "").Replace(">", "") + "\\doplata.akv"))
                {
                    StreamReader Sr = new StreamReader(new FileStream(putin + "\\baza\\" + zakaz.Replace("<", "").Replace(">", "") + "\\doplata.akv", FileMode.Open, FileAccess.Read, FileShare.Read));
                    f = Sr.ReadToEnd();
                    Sr.Close();
                    Sr = null;
                }
                string s = "";
                if (radioButton1.Checked) s = "1";
                else if (radioButton2.Checked) s = "2";
                else if (radioButton6.Checked) s = "3";
                else s = "4";
                f = DateTime.Now + "|" + textBox7.Text + "|" + s + "|" + richTextBox1.Text.Replace("\n", "<n>") + "\r\n" + f;
                File.WriteAllText(putin + "\\baza\\" + zakaz.Replace("<", "").Replace(">", "") + "\\doplata.akv", f, Encoding.UTF8);
                if (flag)
                {
                    Form1 fs = this.Owner as Form1;
                    fs.pub_vidan();
                }
                else
                {
                    Form17 fs = this.Owner as Form17;
                    fs.reload_active();
                }
                this.Close();
            }
            else
            {
                double opl = Convert.ToDouble(textBox7.Text);
                string[] ga = zakaz.Replace("<", "").Split('>');
                foreach(string tm in ga)
                {
                    if(tm == ""||tm == null||opl<=0) continue;
                    if (File.Exists(putin + "\\baza\\" + tm + "\\zakaz.akv"))
                    {
                        string h = "";
                        StreamReader Sr = new StreamReader(new FileStream(putin + "\\baza\\" + tm + "\\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\\" + tm + "\\doplata.akv"))
                        {
                            string str33 = "";
                            double oplh = Convert.ToDouble(tmp[11]);
                            using (StreamReader sr = new StreamReader(new FileStream(putin + "\\baza\\" + tm + "\\doplata.akv", FileMode.Open, FileAccess.Read, FileShare.Read)))
                            {
                                while ((str33 = sr.ReadLine()) != null)
                                {
                                    if (str33 == "") continue;
                                    oplh += Convert.ToDouble(str33.Split('|')[1]);
                                }
                            }
                            ostatok = Convert.ToDouble(tmp[12]) - oplh;
                        }
                        else
                        {
                           ostatok = Convert.ToDouble(tmp[12]) - Convert.ToDouble(tmp[11]); 
                        }
                        if (opl >= ostatok)
                        {
                            opl -= ostatok;
                            string f = "";
                            if (File.Exists(putin + "\\baza\\" + tm + "\\doplata.akv"))
                            {
                                Sr = new StreamReader(new FileStream(putin + "\\baza\\" + tm + "\\doplata.akv", FileMode.Open, FileAccess.Read, FileShare.Read));
                                f = Sr.ReadToEnd();
                                Sr.Close();
                                Sr = null;
                            }
                            string s = "";
                            if (radioButton1.Checked) s = "1";
                            else if (radioButton2.Checked) s = "2";
                            else if (radioButton6.Checked) s = "3";
                            else s = "4";
                            if (ostatok != 0)
                            {
                                f = DateTime.Now + "|" + ostatok + "|" + s + "|" + richTextBox1.Text.Replace("\n", "<n>") + "\r\n" + f;
                                File.WriteAllText(putin + "\\baza\\" + tm + "\\doplata.akv", f, Encoding.UTF8);
                            }
                        }
                        else
                        {
                            string f = "";
                            if (File.Exists(putin + "\\baza\\" + tm + "\\doplata.akv"))
                            {
                                Sr = new StreamReader(new FileStream(putin + "\\baza\\" + tm + "\\doplata.akv", FileMode.Open, FileAccess.Read, FileShare.Read));
                                f = Sr.ReadToEnd();
                                Sr.Close();
                                Sr = null;
                            }
                            string s = "";
                            if (radioButton1.Checked) s = "1";
                            else if (radioButton2.Checked) s = "2";
                            else if (radioButton6.Checked) s = "3";
                            else s = "4";
                            if (opl != 0)
                            {
                                f = DateTime.Now + "|" + opl + "|" + s + "|" + richTextBox1.Text.Replace("\n", "<n>") + "\r\n" + f;
                                File.WriteAllText(putin + "\\baza\\" + tm + "\\doplata.akv", f, Encoding.UTF8);
                            }
                            opl = 0;
                        }
                    }
                }
                if (flag)
                {
                    Form1 fs = this.Owner as Form1;
                    fs.pub_vidan();
                }
                else
                {
                    Form17 fs = this.Owner as Form17;
                    fs.reload_active();
                }
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox7.Text == "")
            {
                MessageBox.Show("Не указана сумма доплаты!");
                return;
            }
            printPreviewDialog1.Document = printDocument1;
            ((Form)printPreviewDialog1).WindowState = FormWindowState.Maximized;
            ((Form)printPreviewDialog1).Icon = this.Icon;
            printPreviewDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Image myimg = null;
            try
            {
                myimg = Code128Rendering.MakeBarcodeImage("#"+zakaz.Replace("<","").Substring(0,zakaz.IndexOf(">")-1), 2, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text);
            }


            Graphics g = e.Graphics;
            String textToPrint = "";
            int a = 30;
            Font printFont = new Font("Arial", 12);
            Font printFont2 = new Font("Arial", 12, System.Drawing.FontStyle.Bold);
            int b = 15;

            Font font = new Font("Tahoma", 9);
            //Font font = new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point);
            Image bs = Properties.Resources.logo;
            g.DrawImage(bs, new Point(a, b));

            b += 15;
            bs = Properties.Resources.location;
            g.DrawImage(bs, new Point(180, b));
            g.DrawString(Статика.Печать_Адрес, font, Brushes.Black, 200, b);

            bs = Properties.Resources.inet;
            g.DrawImage(bs, new Point(550, b));
            g.DrawString(Статика.Печать_Сайт, font, Brushes.Black, 570, b);

            bs = Properties.Resources.phone;
            g.DrawImage(bs, new Point(180, b + 20));
            g.DrawString(Статика.Печать_Телефон, font, Brushes.Black, 200, b + 20);

            bs = Properties.Resources.mail;
            g.DrawImage(bs, new Point(550, b + 20));
            g.DrawString(Статика.Печать_Почта, font, Brushes.Black, 570, b + 20);

            bs = Properties.Resources.time;
            g.DrawImage(bs, new Point(180, b + 40));
            g.DrawString(Статика.Печать_График, font, Brushes.Black, 200, b + 40);
            int multiline = (int)e.Graphics.MeasureString(Статика.Печать_График, font, 1000).Height;
            if (multiline > 32)
            {
                b += multiline - 32;
            }
            b += 80;

            g.DrawString("Доплата за заказы " + "#" + zakaz.Replace("<", "").Substring(0, zakaz.IndexOf(">") - 1), printFont, Brushes.Black, a + 100, b + 6);
            g.DrawImage(myimg, new Point(770 - (myimg.Width * 72 / 96) - 200, b));
            b += 40;

            g.DrawLine(new Pen(Color.Black, 2), new Point(a, b), new Point(785, b));
            b += 2;
            g.DrawString(Статика.Печать_Организация, font, Brushes.Black, a + 165, b);
            g.DrawLine(new Pen(Color.Black, 2), new Point(a, b + 15), new Point(785, b + 15));
            b += 34;

            font = new Font("Tahoma", 10, FontStyle.Bold, GraphicsUnit.Point);

            string opl = "Оплачено:                  " + (textBox7.Text).ToString() + " руб.";
            if (textBox7.Text != "0")
            {
                if (radioButton1.Checked) opl += " (нал.)";
                else if (radioButton2.Checked) opl += " (терм.)";
                else if (radioButton3.Checked) opl += " (карта)";
                else if (radioButton6.Checked) opl += " (безнал.)";
            }
            g.DrawString(opl, font, Brushes.Black, 30, b);

            g.DrawString("Подпись менеджера:", font, Brushes.Black, 400, b);
            b += 22;
            g.DrawLine(new Pen(Color.Black), new Point(120, b), new Point(385, b));
            g.DrawLine(new Pen(Color.Black), new Point(570, b), new Point(785, b));
            b += 10;

           
        }
    }
}
