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.Resources;
using System.Globalization;
using System.Threading;
public partial class Transactions_TA_UI_Transactions_AttendanceEntry : Base.BasePage
{
    #region Variable Declaration
    DataSet dsAttendanceData = new DataSet();
    DataTable dtAttendanceData = new DataTable();
    AttendanceEntry ar = new AttendanceEntry();
    string id = string.Empty;
    ResourceManager rm;
    CultureInfo ci;
    Rpt_Common rptcommon = new Rpt_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    #endregion
    #region Page Load Event
    /// <summary>
    /// Page Load Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected override void Page_Load(object sender, EventArgs e)
    {
        // session checking and reset date format
        base.Page_Load(sender, e);    
        string source_Language;
        if (Session["Session_ActiveLanguage"] != null)
        {
            source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
            rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
        }
        if (!IsPostBack)
        {
            int ToYear = Convert.ToInt32(DateTime.Now.Year);
            int FromYear = Convert.ToInt32(DateTime.Now.Year) - 20;
            for (int i = ToYear; i >= FromYear; i--)
            {
                ListItem LI = new ListItem();
                LI.Value = Convert.ToString(i);
                LI.Text = Convert.ToString(i);
                ddlYear.Items.Add(LI);
            }
            ListItem liSelect = new ListItem();
            liSelect.Value = "Select";
            if (rm != null)
            {
                liSelect.Text = rm.GetString("Select", ci);
            }
            else
            { }
            ddlYear.Items.Insert(0, liSelect);
            LoadStrings(ci);
            BindingEmployeeCategoryandType();
            FillDesignation();
        }
    }
    #endregion
    #region Loading Control Events
    /// <summary>
    /// Load the Controls Text Property
    /// </summary>
    /// <param name="ci"></param>
    private void LoadStrings(CultureInfo ci)
    {
        lblEmpSwipeCard.Text = rm.GetString("TerminalDataType_Card_No", ci);
        lbl_EmployeeName.Text = rm.GetString("email_EmployeeName", ci);
        lblEmployeeId.Text = rm.GetString("email_EmployeeId", ci);
        lblEmpCategory.Text = rm.GetString("lblEmpCategory", ci);
        lblEmpType.Text = rm.GetString("lblEmpType", ci);
        lblDesignation.Text = rm.GetString("EmployeeMaster_Designation", ci);
        lblGender.Text = rm.GetString("EmployeeMaster_Gender", ci);
        btn_View.Text = rm.GetString("btn_View", ci);
        ddlDesignation.Items[0].Text = rm.GetString("ExportData_All", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Alert_SelectYear", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("Alert_SelectMonth", ci);
    }
    /// <summary>
    /// Loading the Employee Type and Category Dropdown
    /// </summary>
    private void BindingEmployeeCategoryandType()
    {
        EmployeeEventData objEmployee = new EmployeeEventData();
        // employee category binding
        DataSet dsCategory = new DataSet();
        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);
            }
        }
        // employee type binding
        DataSet dsType = new DataSet();
        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);
            }
        }
    }
    /// <summary>
    /// Fills the designation in the drop down
    /// </summary>
    private void FillDesignation()
    {
        DataSet ds_designation = new DataSet();
        ds_designation = objEmployee.select_CommonDesignation();
        if (ds_designation != null)
        {
            if (ds_designation.Tables[0].Rows.Count > 0)
            {
                ddlDesignation.DataSource = ds_designation;
                ddlDesignation.DataTextField = "TitleName";
                ddlDesignation.DataValueField = "TitleId";
                ddlDesignation.DataBind();
            }
            ds_designation.Dispose();
            foreach (ListItem item in ddlDesignation.Items)
            {
                item.Attributes.Add("title", item.Text);
            }
        }
    }
    /// <summary>
    /// Load the Grid
    /// </summary>
    //string CardNo,string EmployeeName,string EmployeeId,string EmpCategory,string EmployeeType,string Designation,string Gender,string ParentLevalId,string Month,string Year)
    public void LoadGrid()
    {
        dsAttendanceData = ar.GetAttendanceReport(txtSwipeCard.Text.Trim().ToString() ,
            txtEmployeeName.Text.Trim().ToString(), txtEmployeeId.Text.Trim().ToString(),Convert.ToInt32(ddlEmpCatagory.SelectedValue),
            Convert.ToInt32(ddlEmpType.SelectedValue),Convert.ToInt32(ddlDesignation.SelectedValue), ddlGender.SelectedValue.ToString(),Convert.ToInt32(ddlMonth.SelectedValue),
           Convert.ToInt32(ddlYear.SelectedValue) , Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]),
            Convert.ToInt16(Session["parentLevelId"]), "0", 0, 0, "Select");
        //dsAttendanceData = ar.SelectAttenceReport1("0", 0, 0);
        grv_AttendenceEntry.DataSource = dsAttendanceData.Tables[0];
        ViewState["AttendenceData"] = dsAttendanceData.Tables[0];
        grv_AttendenceEntry.DataBind();
    }
    #endregion
    #region Grid Events
    protected void grv_AttendenceEntry_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.Cells[5].FindControl("lblEmpId");
            TextBox txtPresentDays = (TextBox)e.Row.FindControl("txtPresentDays");
            HiddenField hdnDays = (HiddenField)e.Row.FindControl("htnPresentday");
            if (txtPresentDays != null && hdnDays != null)
            {
                txtPresentDays.Text = hdnDays.Value;
            }
        }
    }
    /// <summary>
    /// Row Editing Event for Attendence Entry Grid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_AttendenceEntry_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grv_AttendenceEntry.EditIndex = e.NewEditIndex;
        LoadGrid();
    }
    /// <summary>
    /// Pagging in Attendence Entry Grid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_AttendenceEntry_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dtAttendanceData = (DataTable)ViewState["AttendenceData"];
        grv_AttendenceEntry.DataSource = dtAttendanceData;
        grv_AttendenceEntry.PageIndex = e.NewPageIndex;
        grv_AttendenceEntry.DataBind();
    }
    /// <summary>
    /// Updating the Rows in Attendence Entry Grid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_AttendenceEntry_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataSet dsResult = new DataSet();
        try
        {
            decimal intTotalDays = 31;
            string id = grv_AttendenceEntry.DataKeys[e.RowIndex].Value.ToString();
            //GridViewRow grdRow1 = (GridViewRow)grv_AttendenceEntry.Rows[e.RowIndex];
            TextBox txtPresentDays = (TextBox)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("txtPresentDays");
            Label lblOd = (Label)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("lblOD");
            Label lblEL = (Label)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("lblEL");
            Label lblCL = (Label)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("lblCL");
            Label lblSL = (Label)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("lblSL");
            Label lblLOP = (Label)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("lblLOP");
            TextBox txtDifferentdays = (TextBox)grv_AttendenceEntry.Rows[e.RowIndex].FindControl("txtDifferentdays");
            if (txtPresentDays.Text.Length != 0)
            {
                if (txtDifferentdays.Text.Length != 0)
                {
                    decimal intTotal = Convert.ToDecimal(txtPresentDays.Text) + Convert.ToDecimal(lblOd.Text) + Convert.ToDecimal(lblEL.Text) +
                    Convert.ToDecimal(lblCL.Text) + Convert.ToDecimal(lblSL.Text) +
                    Convert.ToDecimal(lblLOP.Text) + Convert.ToDecimal(txtDifferentdays.Text);
                    if (intTotal == intTotalDays)
                    {
                        dsResult = ar.GetAttendanceReport(txtSwipeCard.Text.Trim().ToString(),
            txtEmployeeName.Text.Trim().ToString(), txtEmployeeId.Text.Trim().ToString(), Convert.ToInt32(ddlEmpCatagory.SelectedValue),
            Convert.ToInt32(ddlEmpType.SelectedValue),Convert.ToInt32(ddlDesignation.SelectedValue), ddlGender.SelectedValue.ToString(),Convert.ToInt32(ddlMonth.SelectedValue),
            Convert.ToInt32(ddlYear.SelectedValue), Convert.ToString(Session["UserGroupName"]), Convert.ToString(Session["GradeLevel"]),
            Convert.ToInt16(Session["parentLevelId"]), id, Convert.ToDecimal(txtPresentDays.Text),
            Convert.ToDecimal(txtDifferentdays.Text), "Update");
                        if (dsResult.Tables[0].Rows[0][0].ToString()== "0")
                        {
                            string strMsg;
                            strMsg = "<script language='javascript'>";
                            strMsg = strMsg + "alert('" + rm.GetString("Alert_UpdateAttendance", ci) + "');";
                            strMsg = strMsg + "</script>";
                            Page.RegisterStartupScript("Display Message", strMsg);
                            grv_AttendenceEntry.EditIndex = -1;
                            LoadGrid();
                            return;
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                        string strMsg;
                        strMsg = "<script language='javascript'>";
                        strMsg = strMsg + "alert('" + rm.GetString("Alert_Totaldays", ci) + "');";
                        strMsg = strMsg + "</script>";
                        Page.RegisterStartupScript("Display Message", strMsg);
                        grv_AttendenceEntry.EditIndex = -1;
                        LoadGrid();
                        return;
                    }
                }
                else
                {
                    string strMsg;
                    strMsg = "<script language='javascript'>";
                    strMsg = strMsg + "alert('" + rm.GetString("Alert_Differentdays", ci) + "');";
                    strMsg = strMsg + "</script>";
                    Page.RegisterStartupScript("Display Message", strMsg);
                    txtDifferentdays.Focus();
                    return;
                }
            }
            else
            {
                string strMsg;
                strMsg = "<script language='javascript'>";
                strMsg = strMsg + "alert('" + rm.GetString("Alert_PresentDays", ci) + "');";
                strMsg = strMsg + "</script>";
                Page.RegisterStartupScript("Display Message", strMsg);
            }
        }
        catch
        {
        }
    }
    /// <summary>
    /// Cancel Event in Attendence Entry Grid
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_AttendenceEntry_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grv_AttendenceEntry.EditIndex = -1;
        LoadGrid();
    }
    /// <summary>
    /// Sorting in Attendence Entry Grid
    /// </summary>
    public SortDirection gridviewsortdirection
    {
        get
        {
            if (ViewState["sortdirection"] == null)
            {
                ViewState["sortdirection"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["sortdirection"];
        }
        set
        {
            ViewState["sortdirection"] = value;
        }
    }
    protected void grv_AttendenceEntry_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
            if (ViewState["AttendenceData"] != null)
            {
                string sort = e.SortExpression;
                if (gridviewsortdirection == SortDirection.Ascending)
                {
                    gridviewsortdirection = SortDirection.Descending;
                    sortgridview(sort, "DESC");
                }
                else
                {
                    gridviewsortdirection = SortDirection.Ascending;
                    sortgridview(sort, "ASC");
                }
            }
        }
        catch (Exception ex)
        {
            // scriptmanager.scriptmanagermessage(this.Page, ex.Message);
        }
    }
    public void sortgridview(string sortexpression, string direction)
    {
        DataTable dt = new DataTable();
        dt = ViewState["AttendenceData"] as DataTable;
        DataView dv = new DataView(dt);
        dv.Sort = sortexpression + " " + direction;
        ViewState["AttendenceData"] = dv.ToTable();
        grv_AttendenceEntry.DataSource = dv;
        grv_AttendenceEntry.DataBind();
    }
    #endregion
    #region Button Click Events
    /// <summary>
    /// View Button Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_View_Click(object sender, EventArgs e)
    {
        if (Page.IsValid == true)
        {
            LoadGrid();
        }
    }
    #endregion
}
