#region using
using System;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion

namespace LLNet.Tienda.MainForms
{
    /// <summary>
    /// Formulario de Toma Asistencia de Empleados
    /// </summary>
    public partial class frmClock : GUI.FormBases.frmBase
    {
        #region Declarations

        private decimal? empleado;
        private string nombre;
        #endregion

        #region Constructor

        public frmClock()
        {
            InitializeComponent();

            GUI.CApp.TraducirForm(this);
        }

        #endregion

        #region Events

        #region Form Events

        private void frmClock_Load(object sender, EventArgs e)
        {
            LoadData();
            SetFilter("W1");

        }

        private void frmClock_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnClock.PerformClick();
                    break;
                case Keys.F3:
                    btnPrint.PerformClick();
                    break;
                case Keys.Escape:
                    btnCancelar.PerformClick();
                    break;
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Program.CloseWindow(this);            
        } 

        #endregion

        #region Filter Events

        private void lueFiltro_EditValueChanged(object sender, EventArgs e)
        {
            string strWhere = ((LookUpEdit)sender).GetColumnValue("WHERE_CLAUSE").ToString();
            string strOrderBy = ((LookUpEdit)sender).GetColumnValue("WHERE_CLAUSE").ToString();

            dsGeneral1.RANGOS_FECHA.Clear();

            SetFilter(strWhere);

        }

        private void SetFilter(string strWhere)
        {
            switch (strWhere)
            {
                case "W0":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "Todos"), dsGeneral1);
                    break;
                case "W1":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "Today"), dsGeneral1);
                    break;
                case "W2":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "Yesterday"), dsGeneral1);
                    break;
                case "W3":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "ThisWeek"), dsGeneral1);
                    break;
                case "W4":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "LastWeek"), dsGeneral1);
                    break;
                case "W5":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "ThisMonth"), dsGeneral1);
                    break;
                case "W6":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "LastMonth"), dsGeneral1);
                    break;
                case "W7":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "ThisYear"), dsGeneral1);
                    break;
                case "W8":
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "LastYear"), dsGeneral1);
                    break;
                default:
                    Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.RANGOS_FECHA.TableName, "Todos"), dsGeneral1);
                    break;
            }
            rowDe.Properties.Value = dsGeneral1.RANGOS_FECHA[0].DE;
            rowA.Properties.Value = dsGeneral1.RANGOS_FECHA[0].A;

            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("EMPLEADO", empleado.ToString());
            qp.AddReplaceParam("DESDE", Convert.ToDateTime(rowDe.Properties.Value).ToString("dd/MM/yyyy"));
            qp.AddReplaceParam("HASTA", Convert.ToDateTime(rowA.Properties.Value).ToString("dd/MM/yyyy"));

            dsAttendance1.REH_ASISTENCIAS.Clear();
            Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsAttendance1.GetType(), dsAttendance1.REH_ASISTENCIAS.TableName, qp), dsAttendance1);
        }

        private void datA_EditValueChanged(object sender, EventArgs e)
        {
            if (rowDe.Properties.Value != null)
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("EMPLEADO", empleado.ToString());
                qp.AddReplaceParam("DESDE", Convert.ToDateTime(rowDe.Properties.Value).ToString("dd/MM/yyyy"));
                qp.AddReplaceParam("HASTA", ((DateEdit)sender).DateTime.ToString("dd/MM/yyyy"));

                dsAttendance1.REH_ASISTENCIAS.Clear();
                Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsAttendance1.GetType(), dsAttendance1.REH_ASISTENCIAS.TableName, qp), dsAttendance1);
            }
        }

        private void datDe_EditValueChanged(object sender, EventArgs e)
        {
            if (rowA.Properties.Value != null)
            {
                Utils.QueryParams qp = new LLNet.Utils.QueryParams();
                qp.AddReplaceParam("EMPLEADO", empleado.ToString());
                qp.AddReplaceParam("DESDE", ((DateEdit)sender).DateTime.ToString("dd/MM/yyyy"));
                qp.AddReplaceParam("HASTA", Convert.ToDateTime(rowA.Properties.Value).ToString("dd/MM/yyyy"));

                dsAttendance1.REH_ASISTENCIAS.Clear();
                Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsAttendance1.GetType(), dsAttendance1.REH_ASISTENCIAS.TableName, qp), dsAttendance1);
            }
        }

        #endregion

        #region Action Button Events

        private void btnClock_Click(object sender, EventArgs e)
        {
            Clock(empleado);
        }
        
        private void Clock(decimal? empleado)
        {
            if (empleado == null) return;

            Model.PDV.DsAttendance dsAtt = new LLNet.Model.PDV.DsAttendance();

            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("EMPLEADO", empleado.ToString());

            Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsAtt.GetType(), dsAtt.REH_ASISTENCIAS.TableName, qp, "TurnoIniciado"), dsAtt);

            if (dsAtt.REH_ASISTENCIAS.Rows.Count > 0)
            {
                dsAtt.REH_ASISTENCIAS[0].FIN = DateTime.Now;
            }
            else
            {
                Model.PDV.DsAttendance.REH_ASISTENCIASRow row = dsAtt.REH_ASISTENCIAS.NewREH_ASISTENCIASRow();
                row.EMPLEADO = (decimal)empleado;
                row.INICIO = DateTime.Now;
                dsAtt.REH_ASISTENCIAS.AddREH_ASISTENCIASRow(row);
            }

            GUI.CApp.bll.updateDataSet(dsAtt);

            if (rowA.Properties.Value != null)
            {
                qp.AddReplaceParam("DESDE", Convert.ToDateTime(rowDe.Properties.Value).ToString("dd/MM/yyyy"));
                qp.AddReplaceParam("HASTA", Convert.ToDateTime(rowA.Properties.Value).ToString("dd/MM/yyyy"));

                dsAttendance1.REH_ASISTENCIAS.Clear();
                Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsAttendance1.GetType(), dsAttendance1.REH_ASISTENCIAS.TableName, qp), dsAttendance1);
            }

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            PageHeaderArea headerArea;
            PageFooterArea footerArea;            

            PageHeaderFooter header;

            headerArea = new PageHeaderArea();
            
            string strHeader = "{0} {1} \r\nDe: {2} A: {3}";
            string title = this.Text.ToUpper();
            string strDe = ( rowDe.Properties.Value == null ? " " : rowDe.Properties.Value.ToString().Substring(0, 10));
            string strA = (rowDe.Properties.Value == null ? " " : rowA.Properties.Value.ToString().Substring(0, 10));

            strHeader = string.Format(strHeader, title, nombre, strDe, strA);
            
            headerArea.Content.Add(strHeader);
            headerArea.Font = new Font("Arial", 12, System.Drawing.FontStyle.Bold);
            headerArea.LineAlignment = BrickAlignment.Near;

            footerArea = new PageFooterArea();
            footerArea.LineAlignment = BrickAlignment.Far;
            footerArea.Content.Add("");
            footerArea.Content.Add("");
            footerArea.Content.Add("[Date Printed][Time Printed]");

            header = new PageHeaderFooter(headerArea, footerArea);
            printEngine.PageHeaderFooter = header;

            printEngine.CreateDocument();            
            printEngine.ShowPreview();

        }

        #endregion

        #endregion

        public void LoadData()
        {
            // Obteniendo los datos del usuario logeado
            Utils.QueryParams qp = new LLNet.Utils.QueryParams();
            qp.AddReplaceParam("USUARIO", GUI.CApp.usuario);

            DataSet dsUsr = GUI.CApp.bll.getDataSet("General", "DatosUsuario", qp);
            empleado = Convert.ToDecimal(dsUsr.Tables[0].Rows[0]["SOCIO"]);
            nombre = dsUsr.Tables[0].Rows[0]["NOMBRE"].ToString();

            label1.Text = "Bienvenido " + nombre;
            if (empleado != null)
            {
                qp.Clear();
                qp.AddReplaceParam("MODULE_NAME", GUI.CApp.ModulePrefix + this.ModuleName);
                qp.AddReplaceParam("USER_ID", GUI.CApp.usuario);

                Utils.Helpers.Merge(GUI.CApp.bll.getTypedDataSet(dsGeneral1.GetType(), dsGeneral1.TDA_V_USER_FILTERS.TableName, qp, "Filters"), dsGeneral1);

            }
            vGridControl1.Focus();
            vGridControl1.FocusedRow = rowDe;
        }
    }
}