using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Transactions_TA_UI_Transactions_OnDutySanction : Base.BasePage
{
    ResourceManager rm; CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    ManualEntryEventData objManualEntryEventData = new ManualEntryEventData();
    cls_Common objClassCommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    DataSet dsEmployeeGroup = new DataSet();
    DataSet ds_Checkuser = new DataSet();
    DateTime CurrentDate;
    int month;
    int year;
    int date;
    int TotalDays;
    string Employeeid, EmployeeName, EmpCategory, EmpType, dtFromDate, dtToDate, Status, SwipeCard;
    string gstrDateFormat, strFromDateTime, strToDateTime;
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            base.CheckPages(0);
            objClassCommon.menu("OnDuty", "174");
        }
        CultureInfo ci;
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        LoadStrings(ci);
        string strSetAlert = "";
        strSetAlert = strSetAlert + "<script language='javascript'>";
        strSetAlert = strSetAlert + "SetAlertMessages('" + rm.GetString("Alert_SelectStatus", ci) + "','" + rm.GetString("Alert_EnterDate", ci) + "','" + rm.GetString("Alert_EnterToDate", ci) + "','" + rm.GetString("Msg_GrearterThanFrmDate", ci) + "');";
        strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
        strSetAlert = strSetAlert + "SetAlertCheckDate('" + rm.GetString("Alert_Validmonthdatefourdigit", ci) + "','" + rm.GetString("Alert_Validdatemonthfourdigit", ci) + "','" + rm.GetString("Alert_Invalidyearentry", ci) + "','" + rm.GetString("Alert_Invalidmonthentry", ci) + "','" + rm.GetString("Alert_Invaliddateentry", ci) + "','" + rm.GetString("Alert_IncorrectdateformatDDMONYYYY", ci) + "','" + rm.GetString("Alert_Entertime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "','" + rm.GetString("Alert_Invalidtimeformat1", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMM", ci) + "','" + rm.GetString("Alert_ValiddatemonthMMDDYYHHMMTT", ci) + "','" + rm.GetString("Alert_PleaseentervalidDateandTime", ci) + "','" + rm.GetString("Alert_Invalidvalueforhours", ci) + "','" + rm.GetString("Alert_Invalidvalueforminites", ci) + "');";
        strSetAlert = strSetAlert + "</script>";
        Page.RegisterStartupScript("SetAlertMessages", strSetAlert);
        UserRights();
        Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
        if (!IsPostBack)
        {

            txtFromDate.Text = (Convert.ToDateTime(CurrentMonthStartDate().ToString())).ToString(Session["MDateFormat"].ToString());
            txtToDate.Text = (Convert.ToDateTime(NextMonthEndDate())).ToString(Session["MDateFormat"].ToString());
            BindingEmployeeCategoryandType();
        }
    }
    public DateTime CurrentMonthStartDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddDays(-(CurrentDate.AddDays(-1).Day));
        CurrentDate = CurrentDate.AddMonths(-1);
        return CurrentDate;
    }
    public DateTime NextMonthEndDate()
    {
        CurrentDate = (DateTime.Now);
        CurrentDate = CurrentDate.AddMonths(1);
        month = CurrentDate.Month;
        year = CurrentDate.Year;
        TotalDays = System.DateTime.DaysInMonth(year, month);
        date = CurrentDate.Day;
        date = TotalDays - date;
        CurrentDate = CurrentDate.AddDays(date);
        return CurrentDate;
    }
    /// <summary>
    /// Compare FromDate and ToDate
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void CompareDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            strToDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtToDate.Text, "D");
            DateTime dtToDate = DateTime.Parse(strToDateTime);
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff = dtToDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    /// <summary>
    /// Compare From Date with Current Date
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    /// 
    public void ValidateFromDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strFromDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtFromDate.Text, "D");
            DateTime dtCurrentDate = DateTime.Now;
            DateTime dtFromDate = DateTime.Parse(strFromDateTime);
            TimeSpan diff = dtCurrentDate.Subtract(dtFromDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    /// <summary>
    /// Compare To Date with Current Date
    /// </summary>
    /// <param name="s"></param>
    /// <param name="e"></param>
    public void ValidateToDate(object s, ServerValidateEventArgs e)
    {
        try
        {
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            strToDateTime = objClassCommon.DateTimeConvertion((gstrDateFormat), txtToDate.Text, "D");
            DateTime dtCurrentDate = DateTime.Now;
            DateTime dtToDate = DateTime.Parse(strToDateTime);
            TimeSpan diff = dtCurrentDate.Subtract(dtToDate);
            int day = diff.Days;
            if (day >= 0)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
        catch
        {
        }
    }
    protected void btn_View_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            string strScript = string.Empty;
            string strStatus = string.Empty;
            strScript = strScript + "<script language='javascript'>";
            int LevelId = Convert.ToInt32(Session["parentLevelId"]);
            if (Session["parentLevelId"] == null || Convert.ToInt32(Session["parentLevelId"]) == 0)
            {
                strStatus = rm.GetString("Alert_SelectCompany", ci);
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
            Employeeid = txtEmployeeId.Text;
            EmployeeName = txtEmployeeName.Text;
            EmpCategory = ddlEmpCatagory.SelectedItem.Value;
            EmpType = ddlEmpType.SelectedItem.Value;
            dtFromDate = txtFromDate.Text;
            dtToDate = txtToDate.Text;
            Status = ddlStatus.SelectedItem.Value;
            SwipeCard = txtSwipeCard.Text;
            string strStatus1 = "";
            strStatus1 = CbApproved.Checked == true ? "Approved," : strStatus1;
            strStatus1 = CBPending.Checked == true ? strStatus1 + "Pending," : strStatus1;
            strStatus1 = CBResend.Checked == true ? strStatus1 + "Resend," : strStatus1;
            strStatus1 = CBForwarded.Checked == true ? strStatus1 + "Forwarded," : strStatus1;
            strStatus1 = CBRejected.Checked == true ? strStatus1 + "Rejected," : strStatus1;
            strStatus1 = CBReply.Checked == true ? strStatus1 + "Reply," : strStatus1;
            strStatus1 = strStatus1.Length > 0 ? strStatus1.Substring(0, strStatus1.Length - 1) : strStatus1;
            strStatus1 = strStatus1 == "" ? "Approved,Pending,Resend,Forwarded,Rejected,Reply" : strStatus1;
            int intCount = 0;
            intCount = BindOndutySanctionDetails(Convert.ToString(LevelId), strStatus1, Employeeid, EmployeeName, Convert.ToInt32(EmpCategory), Convert.ToInt32(EmpType), objClassCommon.DateConvertion(gstrDateFormat, dtFromDate), objClassCommon.DateConvertion(gstrDateFormat, dtToDate), SwipeCard);
            if (intCount == 0)
            {
                strStatus = rm.GetString("Alert_Thereisnorecord", ci);
                strScript = strScript + "alert('" + strStatus + "');";
                strScript = strScript + "</script>";
                Page.RegisterStartupScript("AlertManualApproval", strScript);
                return;
            }
            else
            {
                Session["OndutyStatus"] = strStatus1;
                if (strStatus1 == "Forwarded" || strStatus1 == "Pending")
                {
                    Response.Redirect("../Transactions/TA_UI_Transactions_OnDutySanctionViewForBulkApproval.aspx?EmpId=" + Employeeid + "&EmpName=" + EmployeeName + "&EmpCategory=" + EmpCategory + "&EmpType=" + EmpType + "&FromDate=" + dtFromDate + "&ToDate=" + dtToDate + "&Status=" + strStatus1 + "&SwipeCard=" + SwipeCard);
                }
                else
                {
                    Response.Redirect("../Transactions/TA_UI_Transactions_OnDutySanctionView.aspx?EmpId=" + Employeeid + "&EmpName=" + EmployeeName + "&EmpCategory=" + EmpCategory + "&EmpType=" + EmpType + "&FromDate=" + dtFromDate + "&ToDate=" + dtToDate + "&Status=" + strStatus1 + "&SwipeCard=" + SwipeCard);
                }
            }
        }
    }

    public int BindOndutySanctionDetails(string LevelId, string Status, string EmployeeID, string EmployeeName, int EmployeeCategory, int EmployeeType, string From_Date, string To_Date, string strSwipeCard)
    {
        int intCount = 0;
        if (Status == "Approved,Pending,Resend,Forwarded,Rejected,Reply")
        {
            Status = "All";
        }
        if (Status != null && Status != "")
        {
            Status = Status.Replace(",", "','");
        }
        DataTable dtManualEntrySanctionView = new DataTable();
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtManualEntrySanctionView = objManualEntryEventData.GetOnDutyEntrySanctionForSuperAdmin(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, EmployeeID, EmployeeName, EmployeeCategory, EmployeeType, From_Date, To_Date, strSwipeCard, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]));
            intCount = dtManualEntrySanctionView.Rows.Count;
        }
        else
        {
            dtManualEntrySanctionView = objManualEntryEventData.GetOnDutySanctionForUser(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, EmployeeID, EmployeeName, EmployeeCategory, EmployeeType, From_Date, To_Date, strSwipeCard, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]));
            intCount = dtManualEntrySanctionView.Rows.Count;
        }
        return intCount;
    }
    // Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lblFilter.Text = rm.GetString("lblFilter", ci);
        lbl_Header.Text = rm.GetString("OnDutyApproval_Header", ci);
        lblEmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("Master_Category", ci);
        lblEmpType.Text = rm.GetString("Master_Type", ci);
        lbl_From.Text = rm.GetString("email_FromDate", ci);
        lbl_Status.Text = rm.GetString("email_Status", ci);
        lbl_To.Text = rm.GetString("email_ToDate", ci);
        cus_ComDate.ErrorMessage = rm.GetString("Msg_GrearterThanFrmDate", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        btn_View.Text = "&nbsp;&nbsp;" + rm.GetString("btn_View", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
        ddlStatus.Items[0].Text = rm.GetString("ExportData_All", ci);
        ddlStatus.Items[1].Text = rm.GetString("btn_Approved", ci);
        ddlStatus.Items[2].Text = rm.GetString("btn_Forwarded", ci);
        ddlStatus.Items[3].Text = rm.GetString("btn_Pending", ci);
        ddlStatus.Items[4].Text = rm.GetString("btn_Reject", ci);
        //cus_FromDate.ErrorMessage = rm.GetString("Alert_SelectvalidFromDate", ci);
        //cus_ToDate.ErrorMessage = rm.GetString("Alert_SelectvalidToDate", ci);
        cus_ComDate.ErrorMessage = rm.GetString("Custom_TodateShouldbegreaterthanFromdate", ci);
        ddlEmpCatagory.Items[0].Text = rm.GetString("Select", ci);
        ddlEmpType.Items[0].Text = rm.GetString("Select", ci);
        cbAll.Text = rm.GetString("ExportData_All", ci);
        CbApproved.Text = rm.GetString("btn_Approved", ci);
        CBForwarded.Text = rm.GetString("btn_Forwarded", ci);
        CBPending.Text = rm.GetString("btn_Pending", ci);
        CBRejected.Text = rm.GetString("btn_Reject", ci);
    }
    #region "EmployeeCategoryandType
    /// <summary>
    /// Bindings the type of the employee category and Type.
    /// </summary>
    private void BindingEmployeeCategoryandType()
    {
        // employee category binding
        DataSet dsCategory = new DataSet();
        try
        {
            dsCategory = objEmployee.SelectCategoryDetailsEmployee("");
            if (dsCategory != null)
            {
                if (dsCategory.Tables[0].Rows.Count > 0)
                {
                    ddlEmpCatagory.DataSource = dsCategory;
                    ddlEmpCatagory.DataTextField = "CategoryName";
                    ddlEmpCatagory.DataValueField = "CategoryId";
                    ddlEmpCatagory.DataBind();
                }
                dsCategory.Dispose();
                foreach (ListItem item in ddlEmpCatagory.Items)
                {
                    item.Attributes.Add("title", item.Text);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        DataSet dsType = new DataSet();
        try
        {
            dsType = objEmployee.SelectTypeDetailsEmployee("");
            if (dsType != null)
            {
                if (dsType.Tables[0].Rows.Count > 0)
                {
                    ddlEmpType.DataSource = dsType;
                    ddlEmpType.DataTextField = "TypeName";
                    ddlEmpType.DataValueField = "TypeId";
                    ddlEmpType.DataBind();
                }
                dsType.Dispose();
                foreach (ListItem item in ddlEmpType.Items)
                {
                    item.Attributes.Add("title", item.Text);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    #endregion
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(174, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Transactions/TA_UI_Transactions_ManualTimeEntryList.aspx");
    }
}
