using compabetov2.database.modelos;
using compabetov2.database;
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 compabetov2.Ventas
{
    public partial class Importe1 : Form
    {
        private vender ventanaPadre;
        private List<ImporteModel> importes;
        private List<DetalleImporte> detalles = new List<DetalleImporte>(); 
        public Importe1(vender ventaPadre)
        {
            InitializeComponent();
            ventanaPadre = ventaPadre;
            llenarPanel();
        }

        private void getData()
        {
            this.importes = service.conseguirImportes();
        }

        private void llenarPanel()
        {
            getData();
            int i = 0;
            foreach (ImporteModel importe in importes)
            {
                detalles.Add(new DetalleImporte(importe.idImporte,0,importe.precioImporte));

                TableLayoutPanel filaLayout = new TableLayoutPanel();
                filaLayout.RowCount = 1;
                filaLayout.ColumnCount = 2;
                filaLayout.Height = 100;
                filaLayout.Width = 650;
                filaLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                filaLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                filaLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));

                TableLayoutPanel informacionProducto = new TableLayoutPanel();
                informacionProducto.RowCount = 1;
                informacionProducto.ColumnCount = 2;
                informacionProducto.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
                informacionProducto.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F));
                informacionProducto.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
                informacionProducto.Dock = DockStyle.Fill;

                PictureBox imagen = new PictureBox();
                string rutaImg = Path.Combine(Application.StartupPath, "Resources", importe.img);
                imagen.Image = Image.FromFile(rutaImg);
                imagen.Dock = DockStyle.Fill;
                imagen.BackgroundImageLayout = ImageLayout.Stretch;
                imagen.SizeMode = PictureBoxSizeMode.Zoom;



                Label nombreStock = new Label();
                nombreStock.Text = importe.tipo;
                nombreStock.Font = new Font(nombreStock.Font.FontFamily, 16);
                nombreStock.Dock = DockStyle.Fill;
                nombreStock.TextAlign = ContentAlignment.MiddleCenter;

                //panel de agregar stock
                TableLayoutPanel agregarPanel = new TableLayoutPanel();
                agregarPanel.Dock = DockStyle.Fill;
                agregarPanel.RowCount = 1;
                agregarPanel.ColumnCount = 3;
                agregarPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 60F));
                agregarPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
                agregarPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
                agregarPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));

                TextBox txtStockAgregar = new TextBox();
                txtStockAgregar.Text = "0";
                txtStockAgregar.Height = 60;
                txtStockAgregar.Width = 200;
                txtStockAgregar.Anchor = AnchorStyles.None;
                txtStockAgregar.Font = new Font(txtStockAgregar.Font.FontFamily, 17);

                Button btnMenos = new Button();
                btnMenos.Text = "-";
                btnMenos.Width = 60;
                btnMenos.Height = 60;
                btnMenos.Font = new Font(btnMenos.Font.FontFamily, 30);
                btnMenos.ForeColor = Color.White;
                btnMenos.FlatStyle = FlatStyle.Flat;
                btnMenos.FlatAppearance.BorderSize = 0;
                btnMenos.BackColor = Color.FromArgb(33, 90, 255);
                btnMenos.Anchor = AnchorStyles.None;
                btnMenos.Click += new EventHandler(delegate (object sender, EventArgs e)
                {
                    try
                    {
                        int numeroContador = int.Parse(txtStockAgregar.Text);
                        txtStockAgregar.Text = (numeroContador - 1).ToString();
                        modificarCantidad(numeroContador-1, importe.idImporte);
                    }
                    catch (Exception)
                    {
                        txtStockAgregar.Text = "0";
                        modificarCantidad(0, importe.idImporte);
                    }
                });

                Button btnMas = new Button();
                btnMas.Text = "+";
                btnMas.Width = 60;
                btnMas.Height = 60;
                btnMas.Font = new Font(btnMas.Font.FontFamily, 30);
                btnMas.ForeColor = Color.White;
                btnMas.BackColor = Color.FromArgb(33, 90, 255);
                btnMas.FlatStyle = FlatStyle.Flat;
                btnMas.FlatAppearance.BorderSize = 0;
                btnMas.Anchor = AnchorStyles.None;
                btnMas.Click += new EventHandler(delegate (object sender, EventArgs e)
                {
                    try
                    {
                        int numeroContador = int.Parse(txtStockAgregar.Text);
                        txtStockAgregar.Text = (numeroContador + 1).ToString();
                        modificarCantidad(numeroContador+1, importe.idImporte);
                    }
                    catch (Exception)
                    {
                        txtStockAgregar.Text = "0";
                        modificarCantidad(0, importe.idImporte);
                    }

                });


                informacionProducto.Controls.Add(imagen, 0, 0);
                informacionProducto.Controls.Add(nombreStock, 1, 0);

                agregarPanel.Controls.Add(txtStockAgregar, 0, 0);
                agregarPanel.Controls.Add(btnMenos, 1, 0);
                agregarPanel.Controls.Add(btnMas, 2, 0);


                filaLayout.Controls.Add(informacionProducto, 0, 0);
                filaLayout.Controls.Add(agregarPanel, 1, 0);
                panelImporte.Controls.Add(filaLayout);
                i++;
            }
        }

        private void modificarCantidad(int cantidad, int idImporte)
        {
            detalles[idImporte-1].cantidad = cantidad;
        }

        private void Importe1_Load(object sender, EventArgs e)
        {

        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            ventanaPadre.importes = detalles;
            ventanaPadre.actualizarImporteTotal();
            this.Close();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            ventanaPadre.importes = detalles;
            ventanaPadre.actualizarImporteTotal();
            this.Close();
        }

        private void pictureBox2_Click_1(object sender, EventArgs e)
        {
            ventanaPadre.importes = detalles;
            ventanaPadre.actualizarImporteTotal();
            this.Close();
        }

        private void panelImporte_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
