using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using DTO;
using Helper;
using Telerik.Web.UI;

namespace NikaiDSR.Executive
{
    public partial class DSREntry : PageBase
    {
        #region Declararion
        CommonManager CM = new CommonManager();
        DSRManager DSRM = new DSRManager();
        int SrNo = 0;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageTitle = "NIKAI- DSR ENTRY";
                lblDate.Text = Request.QueryString["Dt"].ToString();
                FillShiftNames();
                FillDSRHead();
                int User_Id = (Request.QueryString["UID"] == null) ? UserID : Convert.ToInt32(Request.QueryString["UID"]);
                btnAddVisit.OnClientClick = "openRadWin('AddVisit.aspx?Dt=" + lblDate.Text + "&UID=" + User_Id + "','AddVisit'); return false;";

                if (LanguageId == 27)
                {
                    Grid.MasterTableView.GetColumn("Customer_Name_BL").Display = true;
                    Grid.MasterTableView.GetColumn("Contact_Name_BL").Display = true;
                    Grid.MasterTableView.GetColumn("Location_Name_BL").Display = true;
                }
                else
                {
                    Grid.MasterTableView.GetColumn("Customer_Name").Display = true;
                    Grid.MasterTableView.GetColumn("Contact_Name").Display = true;
                    Grid.MasterTableView.GetColumn("Location_Name").Display = true;
                }
            }
        }

        void FillShiftNames()
        {
            IList<ParameterDTO> ShiftNames = CM.GetShiftNames();
            Helper.Validation.FillTelerikDropdown(cmbShift, ShiftNames, (LanguageId != 27) ? "Description" : "DescriptionBL", "ParameterDetId", "Select", "0");
        }

        void FillDSRHead()
        {
            int User_Id = (Request.QueryString["UID"] == null) ? UserID : Convert.ToInt32(Request.QueryString["UID"]);
            DSRHeaderDTO DsrHeader = DSRM.GetDsrHeader(User_Id, Convert.ToDateTime(lblDate.Text));
            if (DsrHeader != null)
            {
                hdnDsrHeaderID.Value = DsrHeader.DSR_Head_Id.ToString();
                cmbShift.SelectedValue = DsrHeader.Shift;
                if (DsrHeader.Morning_Time_In != null)
                    tpMorningIn.SelectedDate = DateTime.Parse(DsrHeader.Morning_Time_In.ToString());
                if (DsrHeader.Morning_Time_Out != null)
                    tpMorningOut.SelectedDate = DateTime.Parse(DsrHeader.Morning_Time_Out.ToString());
                if (DsrHeader.Evening_Time_In != null)
                    tpEveningIn.SelectedDate = DateTime.Parse(DsrHeader.Evening_Time_In.ToString());
                if (DsrHeader.Evening_Time_Out != null)
                    tpEveningOut.SelectedDate = DateTime.Parse(DsrHeader.Evening_Time_Out.ToString());
                btnAddVisit.Enabled = true;
            }
        }

        protected void btnSaveOfficeTime_Click(object sender, EventArgs e)
        {
            string Error = "";
            if (!Validate(ref Error))
                ShowErrorMessage(Error, errMsg, Common.ERROR_MESSAGE_TYPE.ERROR);
            else
            {
                int User_Id = (Request.QueryString["UID"] == null) ? UserID : Convert.ToInt32(Request.QueryString["UID"]);

                TimeSpan? MorIn = null;
                if (tpMorningIn.SelectedDate != null)
                    MorIn = ((DateTime)tpMorningIn.SelectedDate).TimeOfDay;

                TimeSpan? MorOut = null;
                if (tpMorningOut.SelectedDate != null)
                    MorOut = ((DateTime)tpMorningOut.SelectedDate).TimeOfDay;

                TimeSpan? EveIn = null;
                if (tpEveningIn.SelectedDate != null)
                    EveIn = ((DateTime)tpEveningIn.SelectedDate).TimeOfDay;

                TimeSpan? EveOut = null;
                if (tpEveningOut.SelectedDate != null)
                    EveOut = ((DateTime)tpEveningOut.SelectedDate).TimeOfDay;


                DSRM.InsertUpdateDSRHeader(Convert.ToDateTime(lblDate.Text), cmbShift.SelectedValue, MorIn,
                                           MorOut, EveIn, EveOut, "", User_Id);

                ShowErrorMessage((string)GetGlobalResourceObject("Res", "Updated_Successfully"), errMsg, Common.ERROR_MESSAGE_TYPE.SUCCESS);
                btnAddVisit.Enabled = true;
            }
        }

        bool Validate(ref string Error)
        {
            bool validateFlag = true;

            if (!Validation.ComboBoxValidate(cmbShift))
            {
                Error = (string)GetGlobalResourceObject("Res", "Fill_all_mandatory_fields");
                validateFlag = false;
            }
            else if (cmbShift.SelectedValue == "11")
            {
                if (!(Validation.TimePickerValidate(tpMorningIn) && Validation.TimePickerValidate(tpMorningOut)))
                {
                    Error = (string)GetGlobalResourceObject("Res", "Fill_all_mandatory_fields");
                    validateFlag = false;
                }
                else if (((DateTime)tpMorningIn.SelectedDate).TimeOfDay > ((DateTime)tpMorningOut.SelectedDate).TimeOfDay)
                {
                    Error = (string)GetGlobalResourceObject("Res", "OutTime_Intime");
                    validateFlag = false;
                }
            }
            else if (cmbShift.SelectedValue == "12")
            {
                if (!(Validation.TimePickerValidate(tpEveningIn) && Validation.TimePickerValidate(tpEveningOut)))
                {
                    Error = (string)GetGlobalResourceObject("Res", "Fill_all_mandatory_fields");
                    validateFlag = false;
                }
                else if (((DateTime)tpEveningIn.SelectedDate).TimeOfDay > ((DateTime)tpEveningOut.SelectedDate).TimeOfDay)
                {
                    Error = (string)GetGlobalResourceObject("Res", "OutTime_Intime");
                    validateFlag = false;
                }
            }

            if (Validation.TimePickerValidate(tpMorningIn) && !Validation.TimePickerValidate(tpMorningOut))
            {
                Error = (string)GetGlobalResourceObject("Res", "Enter_In_Out");
                validateFlag = false;
            }
            else if (!Validation.TimePickerValidate(tpMorningIn) && Validation.TimePickerValidate(tpMorningOut))
            {
                Error = (string)GetGlobalResourceObject("Res", "Enter_In_Out");
                validateFlag = false;
            }
            else if (!Validation.TimePickerValidate(tpEveningIn) && Validation.TimePickerValidate(tpEveningOut))
            {
                Error = (string)GetGlobalResourceObject("Res", "Enter_In_Out");
                validateFlag = false;
            }
            else if (Validation.TimePickerValidate(tpEveningIn) && !Validation.TimePickerValidate(tpEveningOut))
            {
                Error = (string)GetGlobalResourceObject("Res", "Enter_In_Out");
                validateFlag = false;
            }

            else if (Validation.TimePickerValidate(tpMorningOut) && Validation.TimePickerValidate(tpEveningIn)
                && ((DateTime)tpMorningOut.SelectedDate).TimeOfDay > ((DateTime)tpEveningIn.SelectedDate).TimeOfDay)
            {
                Error = (string)GetGlobalResourceObject("Res", "Evening_In_Morning_Out");
                validateFlag = false;
            }
           else if (Validation.TimePickerValidate(tpMorningOut) && Validation.TimePickerValidate(tpEveningIn)
                   && ((DateTime)tpMorningIn.SelectedDate).TimeOfDay > ((DateTime)tpMorningOut.SelectedDate).TimeOfDay)
            {
                Error = (string)GetGlobalResourceObject("Res", "OutTime_Intime");
                validateFlag = false;
            }

            else if (Validation.TimePickerValidate(tpEveningIn) && Validation.TimePickerValidate(tpEveningOut)
                &&((DateTime)tpEveningIn.SelectedDate).TimeOfDay > ((DateTime)tpEveningOut.SelectedDate).TimeOfDay)
            {
                Error = (string)GetGlobalResourceObject("Res", "OutTime_Intime");
                validateFlag = false;
            }

            //else if ((((DateTime)tpMorningIn.SelectedDate).TimeOfDay > ((DateTime)tpMorningOut.SelectedDate).TimeOfDay)
            //        || (((DateTime)tpEveningIn.SelectedDate).TimeOfDay > ((DateTime)tpEveningOut.SelectedDate).TimeOfDay)
            //        || (((DateTime)tpMorningOut.SelectedDate).TimeOfDay > ((DateTime)tpEveningIn.SelectedDate).TimeOfDay))
            //{
            //    Error = (string)GetGlobalResourceObject("Res", "OutTime_Intime");
            //}

            return validateFlag;
        }

        protected void Grid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            int User_Id = (Request.QueryString["UID"] == null) ? UserID : Convert.ToInt32(Request.QueryString["UID"]);
            Grid.DataSource = DSRM.GetDsrContactCustomerLocation(User_Id, Convert.ToDateTime(lblDate.Text)).OrderByDescending(c => c.Date);
        }

        protected void Grid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                item["SrNo"].Text = (++SrNo).ToString();

                Button btnEdit = (Button)item["Edit"].Controls[1];

                btnEdit.OnClientClick = "openRadWin('../Controls/EditCustomer.aspx?CustId=" + item["Customer_Id"].Text + "','EditCustomer'); return false;";

                btnEdit = (Button)item["Edit"].Controls[3];
                btnEdit.OnClientClick = "openRadWin('../Controls/EditContact.aspx?ContId=" + item["Contact_Id"].Text + "','EditContact'); return false;";

                btnEdit = (Button)item["Edit"].Controls[5];

                int User_Id = (Request.QueryString["UID"] == null) ? UserID : Convert.ToInt32(Request.QueryString["UID"]);
                btnEdit.OnClientClick = "openRadWin('AddVisit.aspx?Dt=" + lblDate.Text + "&DSRId=" + item["DSR_Id"].Text + "&UID=" + User_Id + "','AddVisit'); return false;";
            }
        }
    }
}