using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Resources;
using DevExpress.Web;
/****************************************************************************************************************************
* Project Name    : TimeCheck                              Started Date  : 12-Aug-2007
* Module          : Permission Sanction                    Last Modified : 14-Aug-2007
* Programmer      : Prabakaran.P
* Current Storage : \\Angler56\Projects\Inprogress\SCREEN1206\code\Time_AttendanceApp
* BackUp          :
****************************************************************************************************************************/
public partial class Transactions_TA_UI_Transactions_PermissionSanctionView_Angler : Base.BasePage
{
    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    PermissionEventData_Angler objPermissionEventData = new PermissionEventData_Angler();
    LeaveApplicationEventData_Angler objLeaveApplicationEventData = new LeaveApplicationEventData_Angler();
    YearSettingsEventData objYearSettingsEventData = new YearSettingsEventData();
    cls_Common objcls_Common = new cls_Common();
    Rpt_Common rptcommon = new Rpt_Common();
    DataTable dtPermissionSanctionView = new DataTable();
    DataSet dsYearSetting = new DataSet();
    int vmonthValue;
    string startDate;
    string endDate, gstrDateFormat, LevelId = "", Status, StartMonth, EndMonth, StartYear, EndYear, From_Date, To_Date;
    string strSwipeCardNo = string.Empty;
    string strEmployeeName = string.Empty;
    string strEmployeeCodeId = string.Empty;
    string SelectedShift = string.Empty;
    string source_Language;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        LoadStrings(ci);
        UserRights();
        if (!IsPostBack)
        {
            objcls_Common.menu("PermissionTransaction", "261");
        }
        if (Session["LevelId"] == string.Empty || Session["LevelId"] == null)
        {
            LevelId = Request.QueryString["LevelId"].ToString();
            LevelId = rptcommon.getNodes(Convert.ToInt16(LevelId), Convert.ToString(Session["LevelRights"]));
            Session["LevelId"] = LevelId;
        }
        else
        {
            LevelId = Convert.ToString(Session["LevelId"]);
        }
        if (Session["PermissionSanctionStatus"] == string.Empty || Session["PermissionSanctionStatus"] == null)
        {
            Status = Request.QueryString["Status"].ToString();
            Session["PermissionSanctionStatus"] = Status;
        }
        else
        {
            Status = Convert.ToString(Session["PermissionSanctionStatus"]);
        }
        //Added by Manjula.P on 21-Apr-2014
        if (Request.QueryString["Status"] != string.Empty)
        {
            Status = Request.QueryString["Status"].ToString();
        }
        //**********************************
        if (Session["strSwipeCardNo"] == string.Empty || Session["strSwipeCardNo"] == null)
        {
            if (Request.QueryString["strSwipeCardNo"] != null)
            {
                strSwipeCardNo = Request.QueryString["strSwipeCardNo"].ToString();
            }
            else
            {
                strSwipeCardNo = "";
            }
            Session["strSwipeCardNo"] = strSwipeCardNo;
        }
        else
        {
            strSwipeCardNo = Convert.ToString(Session["strSwipeCardNo"]);
        }
        if (Request.QueryString["Shift"] != null)
        {
            SelectedShift = Convert.ToString(Request.QueryString["Shift"]);
        }
        if (Session["strEmployeeName"] == string.Empty || Session["strEmployeeName"] == null)
        {
            if (Request.QueryString["strEmployeeName"] != null)
            {
                strEmployeeName = Request.QueryString["strEmployeeName"].ToString();
            }
            else
            {
                strEmployeeName = "";
            }
            Session["strEmployeeName"] = strEmployeeName;
        }
        else
        {
            strEmployeeName = Convert.ToString(Session["strEmployeeName"]);
        }
        if (Session["strEmployeeCodeId"] == string.Empty || Session["strEmployeeCodeId"] == null)
        {
            if (Request.QueryString["strEmployeeName"] != null)
            {
                strEmployeeCodeId = Request.QueryString["strEmployeeCodeId"].ToString();
            }
            else
            {
                strEmployeeCodeId = "";
            }
            Session["strEmployeeCodeId"] = strEmployeeCodeId;
        }
        else
        {
            strEmployeeCodeId = Convert.ToString(Session["strEmployeeCodeId"]);
        }
        gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        if (Session["PermissionFrom_Date"] == null || Session["PermissionFrom_Date"] == string.Empty)
        {
            if (Request.QueryString["From_Date"] != null)
            {
                From_Date = Request.QueryString["From_Date"].ToString();
            }
            else
            {
                From_Date = "";
            }
            Session["PermissionFrom_Date"] = From_Date;
        }
        else
        {
            From_Date = Convert.ToString(Session["PermissionFrom_Date"]);
        }
        if (Session["PermissionTo_Date"] == null || Session["PermissionTo_Date"] == string.Empty)
        {
            if (Request.QueryString["To_Date"] != null)
            {
                To_Date = Request.QueryString["To_Date"].ToString();
            }
            else
            {
                To_Date = "";
            }
            Session["PermissionTo_Date"] = To_Date;
        }
        else
        {
            To_Date = Convert.ToString(Session["PermissionTo_Date"]);
        }
        if (From_Date != "")
        {
            From_Date = objcls_Common.DateConvertion(gstrDateFormat, From_Date);
        }
        if (To_Date != "")
        {
            To_Date = objcls_Common.DateConvertion(gstrDateFormat, To_Date);
        }
        dsYearSetting = objYearSettingsEventData.GetActiveYearSettings();
        if (dsYearSetting.Tables[0].Rows.Count <= 0)
        {
            return;
        }
        else
        {
            StartMonth = dsYearSetting.Tables[0].Rows[0]["StartMonth"].ToString();
            StartYear = dsYearSetting.Tables[0].Rows[0]["StartYear"].ToString();
            vmonthValue = MonthValue(StartMonth);
            startDate = vmonthValue.ToString() + "/1/" + StartYear;
            EndMonth = dsYearSetting.Tables[0].Rows[0]["EndMonth"].ToString();
            EndYear = dsYearSetting.Tables[0].Rows[0]["EndYear"].ToString();
            vmonthValue = MonthValue(EndMonth);
            endDate = vmonthValue.ToString() + "/31/" + EndYear;
        }
        if (Convert.ToString(Session["isDashBoard"]) != "Yes")
        {
            if (LevelId == "0")
            {
                LevelId = null;
            }
        }
        if (Status == "-Select Status-")
        {
            Status = null;
        }
        if (Status != null && Status != "")
        {
            Status = Status.Replace(",", "','");
        }
        if (From_Date == "")
        {
            From_Date = startDate;
        }
        if (To_Date == "")
        {
            To_Date = endDate;
        }
        bindpermissionsanctiondetails();
    }
    #endregion
    #region userDefinedFunction
    // Getting Information from resource file and display it in label  
    public void clearAllSession()
    {
        Session["LevelId"] = string.Empty;
        Session["PermissionSanctionStatus"] = string.Empty;
        Session["strSwipeCardNo"] = string.Empty;
        Session["strEmployeeName"] = string.Empty;
        Session["strEmployeeCodeId"] = string.Empty;
        Session["PermissionFrom_Date"] = string.Empty;
        Session["PermissionTo_Date"] = string.Empty;
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_HeaderPermissionSanction.Text = rm.GetString("Transaction_PermissionSanction_PermissionSanction", ci);
        btnBack.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        gdPermissionSanctionView.Columns[0].Caption = rm.GetString("email_EmployeeName", ci);
        gdPermissionSanctionView.Columns[1].Caption = rm.GetString("Menu_Masters_OrgLevels", ci);
        gdPermissionSanctionView.Columns[2].Caption = rm.GetString("LeaveApplication_LeaveApplicationDate", ci);
        gdPermissionSanctionView.Columns[3].Caption = rm.GetString("Report_Duration", ci);
        gdPermissionSanctionView.Columns[4].Caption = rm.GetString("email_Status", ci);
        gdPermissionSanctionView.Columns[3].CellStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#5D5D5D");
        gdPermissionSanctionView.Columns[5].Caption = rm.GetString("email_Reason", ci);
    }
    public int MonthValue(string monthName)
    {
        if (monthName == "January")
            return 1;
        else if (monthName == "February")
            return 2;
        else if (monthName == "March")
            return 3;
        else if (monthName == "April")
            return 4;
        else if (monthName == "May")
            return 5;
        else if (monthName == "June")
            return 6;
        else if (monthName == "July")
            return 7;
        else if (monthName == "August")
            return 8;
        else if (monthName == "September")
            return 9;
        else if (monthName == "October")
            return 10;
        else if (monthName == "November")
            return 11;
        else if (monthName == "December")
            return 12;
        else
            return 0;
    }
    public void SortGrid(string SortColumn, string SortOrder)
    {
        DataTable dtSrch = new DataTable();
        DataView dvSearch = new DataView();
        dtSrch = (DataTable)ViewState["DataPermissionSanction"];
        dvSearch.Table = dtSrch;
        dvSearch.Sort = SortColumn + " " + SortOrder;
        gdPermissionSanctionView.DataSource = dvSearch;
        gdPermissionSanctionView.DataBind();
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(261, Convert.ToInt32(Session["userGroup_Id"]), "viewPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
    public void bindpermissionsanctiondetails()
    {
        if (Convert.ToString(Session["UserGroupName"]) == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
        {
            dtPermissionSanctionView = objPermissionEventData.GetPermissionSanctionDetailsForSuperAdmin(LevelId, Status, Convert.ToDateTime(From_Date), Convert.ToDateTime(To_Date), strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]));
            gdPermissionSanctionView.DataSource = dtPermissionSanctionView;
            ViewState["DataPermissionSanction"] = dtPermissionSanctionView;
            gdPermissionSanctionView.DataBind();
        }
        else
        {
            dtPermissionSanctionView = objPermissionEventData.GetPermissionSanctionDetails(Convert.ToInt32(Session["EmpAutoId"]), LevelId, Status, Convert.ToDateTime(From_Date), Convert.ToDateTime(To_Date), strSwipeCardNo, strEmployeeName, strEmployeeCodeId, Convert.ToString(Session["UserGroupName"]), Convert.ToInt16(Session["parentLevelId"]));
            gdPermissionSanctionView.DataSource = dtPermissionSanctionView;
            ViewState["DataPermissionSanction"] = dtPermissionSanctionView;
            gdPermissionSanctionView.DataBind();
        }
    }
    public void GetEmptyText(object sender, EventArgs e)
    {
        Label lblNoRecords = (Label)sender;
        lblNoRecords.Text = rm.GetString("Alert_Norecordsfound", ci);
    }
    #endregion
    #region GridviewEvents
    protected void gdPermissionSanctionView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdPermissionSanctionView.PageIndex = e.NewPageIndex;
        bindpermissionsanctiondetails();
    }
    protected void gdPermissionSanctionView_Sorting(object sender, GridViewSortEventArgs e)
    {
        ViewState["SortExpression"] = e.SortExpression;
        if (Convert.ToString(Session["SortOrder"]) == string.Empty)
        {
            Session["SortOrder"] = "Asc";
        }
        else if (Convert.ToString(Session["SortOrder"]) == "Asc")
        {
            Session["SortOrder"] = "Desc";
        }
        else
        {
            Session["SortOrder"] = "Asc";
        }
        SortGrid(e.SortExpression, Convert.ToString(Session["SortOrder"]));
    }
    protected void gdPermissionSanctionView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl_Duration = (Label)e.Row.FindControl("lbl_Duration");
            Label lbl = (Label)e.Row.FindControl("PermissionAppliedDate");
            Label Lbl_FromTime = (Label)e.Row.FindControl("Lbl_FromTime");
            Label Lbl_EndTime = (Label)e.Row.FindControl("Lbl_EndTime");
            string TEndTime;
            TEndTime = DateTime.Parse(Lbl_EndTime.Text).ToShortTimeString();
            string TFromTime;
            //TFromTime = DateTime.Parse(Lbl_FromTime.Text).ToShortDateString() + " " + DateTime.Parse(Lbl_FromTime.Text).ToShortTimeString();
            TFromTime = DateTime.Parse(Lbl_FromTime.Text).ToString(Session["MDateFormat"].ToString()) + "&nbsp;&nbsp;&nbsp;" + DateTime.Parse(Lbl_FromTime.Text).ToString(Session["MTimeFormat"].ToString());
            //lbl_Duration.Text = (Convert.ToDateTime(TFromTime)).ToString(Session["MDateFormat"].ToString()) + " To " + TEndTime;
            lbl_Duration.Text = TFromTime + " To " + TEndTime;
            string date1 = (Convert.ToDateTime(lbl.Text)).ToString(Session["MDateFormat"].ToString());
            lbl.Text = date1;
            // e.Row.Cells[3].Controls.Add(lbl);
            Label lblStatus = (Label)e.Row.FindControl("lblPermissionStatus");
            if (lblStatus.Text == "Approved")
            {
                lblStatus.Text = rm.GetString("btn_Approved", ci);
            }
            else if (lblStatus.Text == "Forwarded")
            {
                lblStatus.Text = rm.GetString("btn_Forwarded", ci);
            }
            else if (lblStatus.Text == "Pending")
            {
                lblStatus.Text = rm.GetString("btn_Pending", ci);
            }
            else if (lblStatus.Text == "Rejected")
            {
                lblStatus.Text = rm.GetString("btn_Reject", ci);
            }
            else if (lblStatus.Text == "Resend")
            {
                lblStatus.Text = rm.GetString("Sanction_Resend", ci);
            }
            else if (lblStatus.Text == "Reply")
            {
                lblStatus.Text = rm.GetString("Sanction_Reply", ci);
            }
        }
    }
    protected void gdPermissionSanctionView_PageIndexChanged(object sender, EventArgs e)
    {
        try
        {
            // To display the sorted records in the new page if the Vendor has Sorted the Gridview by clicking any header
            if ((ViewState["SortExpression"]) != null)
            {
                string SortColumn = Convert.ToString(ViewState["SortExpression"]);
                string SortOrder = Convert.ToString(Session["SortOrder"]);
                SortGrid(SortColumn, SortOrder);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void gdPermissionSanctionView_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == GridViewRowType.Data)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            string lbl = Convert.ToString(e.GetValue("Permission_AppliedDate"));
            string Lbl_FromTime = Convert.ToString(e.GetValue("Permission_FromTime"));
            string Lbl_EndTime = Convert.ToString(e.GetValue("Permission_ToTime"));
            string TEndTime;
            TEndTime = DateTime.Parse(Lbl_EndTime).ToShortTimeString();
            string TFromTime;
            TFromTime = DateTime.Parse(Lbl_FromTime).ToString(Session["MDateFormat"].ToString()) + "&nbsp;" + DateTime.Parse(Lbl_FromTime).ToString(Session["MTimeFormat"].ToString());
            e.Row.Cells[3].Text = TFromTime + " To " + TEndTime;
            Label lblStatus = (Label)gdPermissionSanctionView.FindRowCellTemplateControl(e.VisibleIndex, null, "lblPermissionStatus");
            if (lblStatus.Text == "Approved")
            {
                lblStatus.Text = rm.GetString("btn_Approved", ci);
            }
            else if (lblStatus.Text == "Forwarded")
            {
                lblStatus.Text = rm.GetString("btn_Forwarded", ci);
            }
            else if (lblStatus.Text == "Pending")
            {
                lblStatus.Text = rm.GetString("btn_Pending", ci);
            }
            else if (lblStatus.Text == "Rejected")
            {
                lblStatus.Text = rm.GetString("btn_Reject", ci);
            }
            //Added by Manjula.P on 21-Apr-2014 for Getting Resend and Replay string as the PRODUCT
            else if (lblStatus.Text == "Resend")
            {
                lblStatus.Text = rm.GetString("Sanction_Resend", ci);
            }
            else if (lblStatus.Text == "Reply")
            {
                lblStatus.Text = rm.GetString("Sanction_Reply", ci);
            }
        }
    }
    #endregion
    #region ButtonEvents
    protected void btnBack_Click(object sender, EventArgs e)
    {
        clearAllSession();
        if (Convert.ToString(Session["isDashBoard"]) != "Yes")
        {
            Response.Redirect("TA_UI_Transactions_PermissionSanction_Angler.aspx");
        }
        else
        {
            Response.Redirect("../DashBoard.aspx?LevelId=" + Request.QueryString["LevelId"] + "&From_Date=" + From_Date + "&Shift=" + SelectedShift);
        }
    }
    #endregion
}
