using compabetov2.database;
using compabetov2.database.modelos;
using compabetov2.Empleados;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace compabetov2
{
    public partial class mermas : Form

    {

        private Point lastPoint;
        private LoginModel loginPrincipal;
        private List<Merma> mermasList;
        public mermas(LoginModel loginPrincipal)
        {
            InitializeComponent();
            this.loginPrincipal = loginPrincipal;
            llenarPanel();
        }

        private void InitializeMermaPanel()
        {
            // Asegúrate de que mermaPanel ya está creado en tu formulario

            // Habilitar eventos táctiles simulados
            mermaPanel.MouseDown += MermaPanel_MouseDown;
            mermaPanel.MouseMove += MermaPanel_MouseMove;
            mermaPanel.MouseUp += MermaPanel_MouseUp;
        }

        private void MermaPanel_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
            mermaPanel.Capture = true;
        }

        private void MermaPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int deltaX = lastPoint.X - e.X;
                int deltaY = lastPoint.Y - e.Y;

                // Ajustar la dirección del desplazamiento
                mermaPanel.AutoScrollPosition = new Point(mermaPanel.AutoScrollPosition.X + deltaX, mermaPanel.AutoScrollPosition.Y - deltaY);

                lastPoint = new Point(e.X, e.Y);
            }
        }

        private void MermaPanel_MouseUp(object sender, MouseEventArgs e)
        {
            mermaPanel.Capture = false;
        }


        public void llenarPanel()
        {
            mermaPanel.Controls.Clear();
            getData();
            foreach (Merma merma in mermasList)
            {
                TableLayoutPanel carta = new TableLayoutPanel();
                carta.RowCount = 1;
                carta.ColumnCount = 2;
                carta.Height = 100;
                carta.Width = 770;
                carta.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                carta.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                carta.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
                carta.BorderStyle = BorderStyle.FixedSingle;


                Label fecha = new Label();
                fecha.Text = merma.fecha;
                fecha.Dock = DockStyle.Fill;
                fecha.TextAlign = ContentAlignment.MiddleLeft;
                fecha.Font = new Font(fecha.Font.FontFamily, 16, FontStyle.Bold);

                TableLayoutPanel panelBotones = new TableLayoutPanel();
                panelBotones.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                panelBotones.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                panelBotones.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                panelBotones.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
                panelBotones.Dock = DockStyle.Fill;

                Button btnVer = new Button();
                btnVer.Text = "Ver";
                btnVer.Dock = DockStyle.Fill;
                btnVer.FlatStyle = FlatStyle.Flat;
                btnVer.FlatAppearance.BorderSize = 0;
                btnVer.BackColor = Color.FromArgb(33, 90, 255);
                btnVer.Font = new Font(btnVer.Font.FontFamily, 16);
                btnVer.ForeColor = Color.White;
                btnVer.Click += new EventHandler(delegate (object sender, EventArgs e)
                {
                    verMerma ver = new verMerma(merma);
                    ver.Owner = this;
                    ver.Show();
                });

                Button btnEditar = new Button();
                btnEditar.Text = "Editar";
                btnEditar.Dock = DockStyle.Fill;
                btnEditar.FlatStyle = FlatStyle.Flat;
                btnEditar.FlatAppearance.BorderSize = 0;
                btnEditar.BackColor = Color.FromArgb(33, 90, 255);
                btnEditar.Font = new Font(btnVer.Font.FontFamily, 16);
                btnEditar.ForeColor = Color.White;
                btnEditar.Click += new EventHandler(delegate (object sender, EventArgs e)
                {
                    ActualizarMerma ver = new ActualizarMerma(merma,this);
                    ver.Owner = this;
                    ver.Show();
                });


                Button btnEliminar = new Button();
                btnEliminar.Text = "Eliminar";
                btnEliminar.Dock = DockStyle.Fill;
                btnEliminar.BackColor = Color.Red;
                btnEliminar.FlatStyle = FlatStyle.Flat;
                btnEliminar.FlatAppearance.BorderSize = 0;
                btnEliminar.Font = new Font(btnVer.Font.FontFamily, 16);
                btnEliminar.ForeColor = Color.White;
                btnEliminar.Click += new EventHandler(delegate (object sender, EventArgs e)
                {
                    DialogResult result = MessageBox.Show("¿Está seguro de que desea eliminar este registro?",
                        "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        if (service.eliminarMerma(merma))
                        {
                            llenarPanel();
                        }
                    }
                });


                panelBotones.Controls.Add(btnVer,0,0);
                panelBotones.Controls.Add(btnEditar,1,0);
                panelBotones.Controls.Add(btnEliminar,2,0);

                carta.Controls.Add(fecha,0,0);
                carta.Controls.Add(panelBotones,1,0);

                mermaPanel.Controls.Add(carta);
            }
        }

        private void getData()
        {
            mermasList = service.conseguirMerma();
        }

        private void mermas_Load(object sender, EventArgs e)
        {

        }

        private void horafecha_Tick(object sender, EventArgs e)
        {

        }

        private void horafecha_Tick_1(object sender, EventArgs e)
        {

        }

        private void horafecha_Tick_2(object sender, EventArgs e)
        {
            lblhora.Text = DateTime.Now.ToString("h:mm:ss");
            lblfecha.Text = DateTime.Now.ToLongDateString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Cerrar el formulario de ventas
            this.Close();

            // Verificar si ya hay una instancia del formulario principal
            MenuPrincipal menuprincipalForm = Application.OpenForms.OfType<MenuPrincipal>().FirstOrDefault();

            if (menuprincipalForm == null)
            {
                // Si no hay una instancia, crear una nueva
                menuprincipalForm = new MenuPrincipal(loginPrincipal);

                // Establecer el formulario principal como el formulario MDI padre
                menuprincipalForm.MdiParent = this.MdiParent;

                // Mostrar el formulario principal
                menuprincipalForm.Show();
            }
            else
            {
                // Si ya hay una instancia, simplemente llevarlo al frente
                menuprincipalForm.BringToFront();
            }

        }

        private void mermaPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            agregarmermas agregarmermas = new agregarmermas(this);
            agregarmermas.Owner = this;
            agregarmermas.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Estadiscticas verstats = new Estadiscticas();
            verstats.Show();
        }
    }
}
