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 Masters_TA_UI_Master_AttandanceCodeModify : Base.BasePage
{
    ResourceManager rm;
    CultureInfo ci;
    cls_Common objcommon = new cls_Common();
    string strSetAlert;
    protected override void Page_Load(object sender, EventArgs e)
    {        // session checking
        base.Page_Load(sender, e);
        //get the culture info to set the language
        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);
        //Set UserRights
        UserRights();
        ddlCompany.Focus();
        if (!IsPostBack)
        {
            btn_add.OnClientClick = "if(Page_ClientValidate()) return confirm('" + rm.GetString("Alert_ConfirmSave") + "');return false;";
            CompanyBinding();
            objcommon.menu("Others", "99");
            int iAttandanceID = 0;
            if (Request.QueryString["Id"] != null)
            {
                iAttandanceID = Convert.ToInt32(Request.QueryString["Id"]);
            }
            AttandanceCodeEventData objClass = new AttandanceCodeEventData();
            DataSet ds = new DataSet();
            ds = objClass.SelectAttandanceByID(iAttandanceID, "No", "", "", Convert.ToInt16(Session["parentLevelId"]));
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["AttandanceCode"] != DBNull.Value)
                {
                    txt_AttandanceCode.Text = Convert.ToString(ds.Tables[0].Rows[0]["AttandanceCode"]);
                }
                if (ds.Tables[0].Rows[0]["CompanyID"] != DBNull.Value && Convert.ToInt32(ds.Tables[0].Rows[0]["CompanyID"]) != 0)
                {
                    ddlCompany.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["CompanyID"]);
                }
                if (ds.Tables[0].Rows[0]["AttandanceValue"] != DBNull.Value)
                {
                    ddlAttandance.SelectedValue = Convert.ToString(ds.Tables[0].Rows[0]["AttandanceValue"]);
                }
            }
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("MaxLengthh", strSetAlert);
        }
    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        int iAttandanceID = 0;
        if (Request.QueryString["Id"] != null)
        {
            iAttandanceID = Convert.ToInt32(Request.QueryString["Id"]);
        }
        string strAttandanceCode = "";
        string strAttandanceValue = "";
        string strOpr = "";
        int iCompanyID = 0;
        iCompanyID = Convert.ToInt32(ddlCompany.SelectedValue);
        strAttandanceCode = txt_AttandanceCode.Text.Trim().Replace("'", "''");
        strAttandanceValue = Convert.ToString(ddlAttandance.SelectedValue).Trim();
        strOpr = "U";
        string strScript = "";
        DataSet dsCheck = new DataSet();
        AttandanceCodeEventData objClass = new AttandanceCodeEventData();
        dsCheck = objClass.CheckAttandance(iAttandanceID, strAttandanceCode, strAttandanceValue, iCompanyID, strOpr);
        if (dsCheck != null && dsCheck.Tables.Count > 0 && dsCheck.Tables[0].Rows.Count > 0)
        {
            if (dsCheck.Tables[0].Rows[0]["RecCount"] != DBNull.Value)
            {
                if (Convert.ToInt16(dsCheck.Tables[0].Rows[0]["RecCount"]) > 0)
                {
                    strScript = "alert('" + rm.GetString("Alert_AttandanceCodealreadyexists", ci) + "');";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", strScript, true);
                    return;
                }
            }
        }
        int EmpID = Convert.ToInt32(Session["EmpAutoID"].ToString());
        objClass.UpdateAttandance(iAttandanceID, strAttandanceCode, strAttandanceValue, iCompanyID, EmpID);
        strScript = "alert('" + rm.GetString("Alert_AttandanceCodeModifiedsuccessfully", ci) + "');location.replace('TA_UI_Master_AttandanceCode.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", strScript, true);
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_AttandanceCode.aspx");
    }
    private void LoadStrings(CultureInfo ci)
    {
        lbl_Attandance.Text = rm.GetString("Attandance", ci);
        lbl_AttandanceCode.Text = rm.GetString("AttandanceCode", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        lbl_Header.Text = rm.GetString("AttandanceCode", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lblCompany.Text = rm.GetString("EntranceMaster_Company", ci);
        ddlAttandance.Items[0].Text = rm.GetString("Select", ci);
        ddlAttandance.Items[1].Text = rm.GetString("Dash_Absent", ci);
        ddlAttandance.Items[2].Text = rm.GetString("AttendanceCode_Present", ci);
        ddlAttandance.Items[3].Text = rm.GetString("LeaveApplication_Leave", ci);
        RequiredFieldValidator2.ErrorMessage = rm.GetString("Alert_Select_Company", ci);
        RequiredFieldValidator1.ErrorMessage = rm.GetString("Alert_SelectAttandance", ci);
        RequiredFieldValidator3.ErrorMessage = rm.GetString("Alert_EnterAttandanceCode", ci);
        ddlCompany.Items[0].Text = rm.GetString("Select", ci);
    }
    private void UserRights()
    {
        Cls_CheckUser objCheckUser = new Cls_CheckUser();
        DataSet ds_Checkuser = new DataSet();
        ds_Checkuser = objCheckUser.checkRights(99, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
        else
        {
            //   
        }
    }
    private void CompanyBinding()
    {
        ShiftEvenData objShift = new ShiftEvenData();
        string gstrUserGroupName = string.Empty;
        DataSet dsCompany = new DataSet();
        gstrUserGroupName = Convert.ToString(Session["UserGroupName"]);
        dsCompany = objShift.SelectCompany(gstrUserGroupName, Convert.ToString(Session["LevelRights"]), 0);
        if (dsCompany != null)
        {
            if (dsCompany.Tables[0].Rows.Count > 0)
            {
                ddlCompany.DataSource = dsCompany;
                ddlCompany.DataTextField = "CompanyName";
                ddlCompany.DataValueField = "CompanyId";
                ddlCompany.DataBind();
            }
        }
        foreach (ListItem item in ddlCompany.Items)
        {
            item.Attributes.Add("title", item.Text);
        }
    }
}
