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; 
public partial class Transactions_TA_UI_Transactions_LeaveApplicationAdmin : Base.BasePage
{
    #region "Variable Declaration"
    ResourceManager rm;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    LeaveApplicationEventData objLeaveHolidays = new LeaveApplicationEventData();
    cls_Common objClassCommon = new cls_Common();
    DataSet dsLeaveHolidays = new DataSet();
    //    Cls_Connect cl = new Cls_Connect();
    LeaveApplicationEventData objLeaveApplicationEventData = new LeaveApplicationEventData();
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking
        base.Page_Load(sender, e);
        if (!Page.IsPostBack)
        {
            Hidden1.Value = Convert.ToString(Session["IDateFormat"]);
            base.CheckPages(0);
        }
        if (Convert.ToString(Session["UserGroupName"]) != "SuperAdmin")
        {
            Response.Redirect("TA_UI_Transactions_LeaveApplication.aspx");
        }
        //get the culture info to set the language
        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;
        LoadStrings(ci);
        //Set UserRights
        UserRights();
        if (!Page.IsPostBack)
        {
            btn_Add.Focus();
            dsLeaveHolidays = objLeaveHolidays.SelectRestrictedHolidayDetails(Convert.ToInt32(Session["EmpAutoId"]));
            DataSet dsEmployeeGroup = new DataSet();
            //SqlParameter pEmpIdGroup = new SqlParameter("@employee_Id", Session["EmpAutoId"]);
            //SqlParameter[] pParamsGroup ={ pEmpIdGroup };
            //dsEmployeeGroup = cl.fillDs_SP("Proc_TA_SelectEmployeeGroupName", pParamsGroup);
            dsEmployeeGroup = objLeaveApplicationEventData.GetEmployeeGroupName(Convert.ToInt32(Session["EmpAutoId"]));
            if (dsEmployeeGroup.Tables[0].Rows.Count > 0)
            {
                if (dsEmployeeGroup.Tables[0].Rows[0]["GroupName"].ToString() == "SuperAdmin" || Convert.ToString(Session["HasSpecialRights"]) == "True")
                {
                    grv_LeaveApplication.DataSourceID = "";
                    grv_LeaveApplication.DataSourceID = ObjectDataSource2.ID;
                    ViewState["GroupName"] = "SuperAdmin";
                }
                else
                {
                    grv_LeaveApplication.DataSourceID = ObjectDataSource1.ID;
                    grv_LeaveApplication.Columns[2].Visible = false;
                    grv_LeaveApplication.Columns[3].Visible = false;
                    ViewState["GroupName"] = "Normal";
                }
            }
            else
            {
                grv_LeaveApplication.DataSourceID = ObjectDataSource1.ID;
                grv_LeaveApplication.Columns[2].Visible = false;
                grv_LeaveApplication.Columns[3].Visible = false;
                ViewState["GroupName"] = "Normal";
            }
        }
        if (ViewState["GroupName"].ToString() == "Normal")
        {
            trSwipeCardNo.Visible = false;
            trEmployeeDetails.Visible = false;
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Transactions_MultipleLeaveApplicationAdd.aspx");
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
    }
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Header.Text = rm.GetString("Transactions_LeaveApplication", ci);
        lbl_EmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lbl_EmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblTimePeriod.Text = rm.GetString("ShiftExchange_DateRange", ci); 
        lblLeaveStartDate.Text = rm.GetString("email_FromDate", ci);
        lblLeaveEndDate.Text = rm.GetString("email_ToDate", ci);
        btn_Search.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Search", ci);
        btn_Clear.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Clear", ci);
        btn_Add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Add", ci);
        grv_LeaveApplication.Columns[1].HeaderText = rm.GetString("LeaveApplication_LeaveApplicationDate", ci);
        grv_LeaveApplication.Columns[1].HeaderText = rm.GetString("LeaveApplication_LeaveApplicationDate", ci);
        grv_LeaveApplication.Columns[2].HeaderText = rm.GetString("email_EmployeeName", ci);
        grv_LeaveApplication.Columns[3].HeaderText = rm.GetString("LevelDetails_LevelName", ci);
        grv_LeaveApplication.Columns[4].HeaderText = rm.GetString("Report_Duration", ci);
        grv_LeaveApplication.Columns[6].HeaderText = rm.GetString("email_Status", ci);
        grv_LeaveApplication.Columns[7].HeaderText = rm.GetString("email_Status", ci);
        grv_LeaveApplication.Columns[8].HeaderText = rm.GetString("email_Remarks", ci);
        lblEmpSwipe.Text = rm.GetString("TerminalDataType_Card_No", ci);
    }
    string previousCat = "";
    int firstRow = -1;
    protected void grv_LeaveApplication_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.Cells[1].FindControl("AppDate1");
            lbl.Text = (Convert.ToDateTime(lbl.Text.ToString())).ToString(Session["MDateFormat"].ToString());
            e.Row.Cells[1].Controls.Add(lbl);
            Label lblLeave = (Label)e.Row.Cells[4].FindControl("lbl_Duration");
            string[] dates = lblLeave.Text.Split(" - ".ToCharArray());
            string date1 = (Convert.ToDateTime(dates[0])).ToString(Session["MDateFormat"].ToString());
            string date2 = (Convert.ToDateTime(dates[3])).ToString(Session["MDateFormat"].ToString());
            if (date1 == date2)
            {
                lblLeave.Text = date1;
            }
            else
            {
                lblLeave.Text = date1 + "&nbsp; To &nbsp;" + date2;
            }
            //Merge Rows
            //We're only interested in Rows that contain data
            //get a reference to the data used to databound the row
            DataRowView drv = ((DataRowView)e.Row.DataItem);
            if (previousCat == drv["LeaveApplication_Id"].ToString())
            {
                if (grv_LeaveApplication.Rows.Count >= 1)
                {
                    if (firstRow < grv_LeaveApplication.Rows.Count)
                    {
                        //If it's the same category as the previous one
                        //Increment the rowspan
                        if (grv_LeaveApplication.Rows[firstRow].Cells[1].RowSpan == 0)
                        {
                            grv_LeaveApplication.Rows[firstRow].Cells[1].RowSpan = 2;
                            //grv_LeaveApplication.Rows[firstRow].Cells[2].RowSpan = 2;
                            //grv_LeaveApplication.Rows[firstRow].Cells[3].RowSpan = 2;
                            //grv_LeaveApplication.Rows[firstRow].Cells[6].RowSpan = 2;
                            //grv_LeaveApplication.Rows[firstRow].Cells[7].RowSpan = 2;
                            //grv_LeaveApplication.Rows[firstRow].Cells[8].RowSpan = 2;
                        }
                        else
                        {
                            grv_LeaveApplication.Rows[firstRow].Cells[1].RowSpan += 1;
                            //grv_LeaveApplication.Rows[firstRow].Cells[2].RowSpan += 1;
                            //grv_LeaveApplication.Rows[firstRow].Cells[3].RowSpan += 1;
                            //grv_LeaveApplication.Rows[firstRow].Cells[6].RowSpan += 1;
                            //grv_LeaveApplication.Rows[firstRow].Cells[7].RowSpan += 1;
                            //grv_LeaveApplication.Rows[firstRow].Cells[8].RowSpan += 1;
                        }
                        //if (grv_LeaveApplication.Rows[firstRow].Cells[2].RowSpan == 0)
                        //{
                        //    grv_LeaveApplication.Rows[firstRow].Cells[2].RowSpan = 2;
                        //}
                        //else
                        //{
                        //    grv_LeaveApplication.Rows[firstRow].Cells[2].RowSpan += 1;
                        //}
                        //if (grv_LeaveApplication.Rows[firstRow].Cells[3].RowSpan == 0)
                        //{
                        //    grv_LeaveApplication.Rows[firstRow].Cells[3].RowSpan = 2;
                        //}
                        //else
                        //{
                        //    grv_LeaveApplication.Rows[firstRow].Cells[3].RowSpan += 1;
                        //}
                        //Remove the cell
                        e.Row.Cells.RemoveAt(1);
                        //e.Row.Cells.RemoveAt(2);
                        //e.Row.Cells.RemoveAt(3);
                        //e.Row.Cells.RemoveAt(6);
                        //e.Row.Cells.RemoveAt(7);
                        //e.Row.Cells.RemoveAt(8);
                    }
                    else //It's a new category
                    {
                        //Set the vertical alignment to top
                        e.Row.VerticalAlign = VerticalAlign.Middle;
                        //Maintain the category in memory
                        previousCat = "";
                        firstRow = -1;
                    }
                }
            }
            else //It's a new category
            {
                //Set the vertical alignment to top
                e.Row.VerticalAlign = VerticalAlign.Middle;
                //Maintain the category in memory
                previousCat = drv["LeaveApplication_Id"].ToString();
                firstRow = e.Row.RowIndex;
            }
        }
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(22, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_Add.Visible = true;
            }
            else
            {
                this.btn_Add.Visible = false;
            }
        }
        else
        {
            Response.Redirect("TransactionLandingPage.aspx");
        }
    }
    protected void btn_Clear_Click(object sender, EventArgs e)
    {
        grid_RestoreDefault();
        lbl_Error.Text = string.Empty;
    }
    private void grid_RestoreDefault()
    {
        //Restores the Default view of the grid
        txt_EmpName.Text = string.Empty;
        txtEmployeeId.Text = string.Empty;
        txtLeaveStartDate.Text = string.Empty;
        txtLeaveEndDate.Text = string.Empty;
        txtSwipeCard.Text = string.Empty;
        grv_LeaveApplication.PageIndex = 0;
        if (ViewState["GroupName"].ToString() == "SuperAdmin")
        {
            grv_LeaveApplication.DataSourceID = ObjectDataSource2.ID;
        }
        else
        {
            grv_LeaveApplication.DataSourceID = ObjectDataSource1.ID;
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        //fill gridview based on search details given in textbox
        FillGrid_Search();
    }
    private void FillGrid_Search()
    {
        string empName = txt_EmpName.Text.Trim();
        txt_EmpName.Text = empName;
        string empId = txtEmployeeId.Text.Trim();
        txtEmployeeId.Text = empId;
        string strLeaveStartDate = txtLeaveStartDate.Text;
        string strLeaveEndDate = txtLeaveEndDate.Text;
        bool boolErrorStatus = true;
        string gstrDateFormat = Convert.ToString(Session["MDateFormat"]);
        strLeaveStartDate = objClassCommon.DateConvertion(gstrDateFormat, strLeaveStartDate);
        strLeaveEndDate = objClassCommon.DateConvertion(gstrDateFormat, strLeaveEndDate);
        string strSwipeCard = "";
        strSwipeCard = txtSwipeCard.Text.ToString().Trim().Replace("'", "''");
        if (empName == string.Empty && empId == string.Empty && strLeaveStartDate == string.Empty && strLeaveEndDate == string.Empty && strSwipeCard == string.Empty && ViewState["GroupName"].ToString() == "SuperAdmin")
        {
            //lbl_Error.Text = "Please Enter any of the above details";
            //base.Message("Please Enter any of the above details");
            grv_LeaveApplication.DataSourceID = "";
            //grv_LeaveApplication.DataBind();
            txt_EmpName.Focus();
            boolErrorStatus = true;
            return;
        }
        else
        {
            //Checks weather both the start and end order date are available or not available
            if (strLeaveStartDate == string.Empty && strLeaveEndDate == string.Empty)
            {
                //Both date not given
                boolErrorStatus = false;
            }
            else if (strLeaveStartDate != string.Empty && strLeaveEndDate != string.Empty)
            {
                //Both date Given
                //if (((Convert.ToDateTime(strLeaveStartDate).Date) > System.DateTime.Now.Date) || ((Convert.ToDateTime(strLeaveEndDate).Date) > System.DateTime.Now.Date))
                //{
                //    //Checks weather the start date and end date is larger than today's date
                //    lbl_Error.Text = "Date should not exceed todays date";
                //    boolErrorStatus = true;
                //}
                if (Convert.ToDateTime(strLeaveEndDate).Date < Convert.ToDateTime(strLeaveStartDate).Date)
                {
                    //Checks weather the start date is less than end date
                    //lbl_Error.Text = "From date should be smaller than To date";
                    base.Message("From date should be smaller than To date");
                    grv_LeaveApplication.DataSourceID = "";
                    return;
                    boolErrorStatus = true;
                }
                else
                {
                    boolErrorStatus = false;
                }
            }
            else
            {
                //Any one date is given
                //lbl_Error.Text = "Please enter both the From and To date";
                //base.Message("Please enter both the From and To date");
                grv_LeaveApplication.DataSourceID = "";
                return;
                boolErrorStatus = true;
            }
        }
        if (boolErrorStatus == true)
        {
            grid_RestoreDefault();
        }
        else
        {
            lbl_Error.Text = string.Empty;
            grv_LeaveApplication.PageIndex = 0;
            if (ViewState["GroupName"].ToString() == "SuperAdmin")
            {
                grv_LeaveApplication.DataSourceID = ObjectDataSource2.ID;
            }
            else
            {
                grv_LeaveApplication.DataSourceID = ObjectDataSource1.ID;
            }
        }
    }
}
