using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace HMI
{
    public partial class Form_reporte : Form
    {
        SqlCommand cmd;
        SqlConnection cn;
        SqlDataReader dr;
        int cant_obs = 0;
        int cant_acep = 0;
        int cant_cat1 = 0;
        int cant_cat2 = 0;
        int cant_cat3 = 0;
        int cant_rech = 0;
        double vol_obs = 0;
        double vol_acep = 0;
        double vol_cat1 = 0;
        double vol_cat2 = 0;
        double vol_cat3 = 0;
        double vol_rech = 0;

   



        public Form_reporte()
        {
            InitializeComponent();
            cn = new SqlConnection("Data Source=.;Initial Catalog=data_hmi;Integrated Security=True");

        }

        string fecha_inicio_text;


        public void button1_Click(object sender, EventArgs e)
        {
            DateTime fecha_inicio = DateTime.Parse(fecha1.Value.ToString("yyyy-MM-dd"));

            fecha_inicio_text = fecha_inicio.ToString("dd/MM/yyyy");

            cn.Open();
            SqlCommand cm = new SqlCommand("select COUNT(rollizo) from rollizos where fecha='"+fecha_inicio_text+"'", cn);
            cant_obs = Convert.ToInt16(cm.ExecuteScalar());
            cn.Close();

            if (cant_obs > 0)
            {
                cn.Open();

                cm = new SqlCommand("select COUNT(rollizo) from rollizos where  categoria != 'rechazado' AND fecha='" + fecha_inicio_text + "'", cn);
                cant_acep = Convert.ToInt16(cm.ExecuteScalar());

                cm = new SqlCommand("select COUNT(rollizo) from rollizos where categoria = 'categoria1' AND fecha='" + fecha_inicio_text + "'", cn);
                cant_cat1 = Convert.ToInt16(cm.ExecuteScalar());

                cm = new SqlCommand("select COUNT(rollizo) from rollizos where categoria = 'categoria2' AND fecha='" + fecha_inicio_text + "'", cn);
                cant_cat2 = Convert.ToInt16(cm.ExecuteScalar());

                cm = new SqlCommand("select COUNT(rollizo) from rollizos where categoria = 'categoria3' AND fecha='" + fecha_inicio_text + "'", cn);
                cant_cat3 = Convert.ToInt16(cm.ExecuteScalar());

                cm = new SqlCommand("select COUNT(rollizo) from rollizos where categoria = 'rechazado' AND fecha='" + fecha_inicio_text + "'", cn);
                cant_rech = Convert.ToInt16(cm.ExecuteScalar());

                cm = new SqlCommand("select sum(volumen) from rollizos where  fecha='" + fecha_inicio_text + "'", cn);
                vol_obs = (Convert.ToDouble(cm.ExecuteScalar())) / 1000;

                cm = new SqlCommand("select sum(volumen) from rollizos where categoria != 'rechazado' AND fecha='" + fecha_inicio_text + "'", cn);
                vol_acep = (Convert.ToDouble(cm.ExecuteScalar())) / 1000;

                cm = new SqlCommand("select sum(volumen) from rollizos where categoria = 'categoria1' AND fecha='" + fecha_inicio_text + "'", cn);
                if (cm.ExecuteScalar() != DBNull.Value) { vol_cat1 = (Convert.ToDouble(cm.ExecuteScalar())) / 1000; }

                cm = new SqlCommand("select sum(volumen) from rollizos where categoria = 'categoria2' AND fecha='" + fecha_inicio_text + "'", cn);
                if (cm.ExecuteScalar() != DBNull.Value) { vol_cat2 = (Convert.ToDouble(cm.ExecuteScalar())) / 1000; }


                cm = new SqlCommand("select sum(volumen) from rollizos where categoria = 'categoria3' AND fecha='" + fecha_inicio_text + "'", cn);
                if (cm.ExecuteScalar() != DBNull.Value) { vol_cat3 = (Convert.ToDouble(cm.ExecuteScalar())) / 1000; }

                cm = new SqlCommand("select sum(volumen) from rollizos where categoria = 'rechazado' AND fecha='" + fecha_inicio_text + "'", cn);
                if (cm.ExecuteScalar() != DBNull.Value) { vol_rech = (Convert.ToDouble(cm.ExecuteScalar())) / 1000; }


                cn.Close();

                text_cant_obs.Text = cant_obs.ToString();
                text_cant_acep.Text = cant_acep.ToString();
                text_cant_cat1.Text = cant_cat1.ToString();
                text_cant_cat2.Text = cant_cat2.ToString();
                text_cant_cat3.Text = cant_cat3.ToString();
                text_cant_rech.Text = cant_rech.ToString();

                text_vol_obs.Text = vol_obs.ToString();
                text_vol_acep.Text = vol_acep.ToString();
                text_vol_cat1.Text = vol_cat1.ToString();
                text_vol_cat2.Text = vol_cat2.ToString();
                text_vol_cat3.Text = vol_cat3.ToString();
                text_vol_rech.Text = vol_rech.ToString();



            }
            else
            {
                text_cant_obs.Text =  "0";
                text_cant_acep.Text = "0";
                text_cant_cat1.Text = "0";
                text_cant_cat2.Text = "0";
                text_cant_cat3.Text = "0";
                text_cant_rech.Text = "0";

                text_vol_obs.Text =  "0";
                text_vol_acep.Text = "0";
                text_vol_cat1.Text = "0";
                text_vol_cat2.Text = "0";
                text_vol_cat3.Text = "0";
                text_vol_rech.Text = "0";


            }
        }


  
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

        }

        public void butt_detalles_Click(object sender, EventArgs e)
        {
            Form Form_detalle = new Form_detalle(fecha_inicio_text);
            Form_detalle.Show();
        }

        private void Form_reporte_Load(object sender, EventArgs e)
        {

        }
    }
}
