using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Masters_TA_UI_Master_EmployeeContractorModify : Base.BasePage
{

    #region VariableDeclaration
    ResourceManager rm;
    CultureInfo ci;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    string strSetAlert;
    cls_Common objcommon = new cls_Common();
    TypeEventData objempType = new TypeEventData();
    int EmpId;
    int ContractorID;
    #endregion
    #region PageLoad
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        EmpId = Convert.ToInt32(Session["EmpAutoID"].ToString());
        //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();
        txt_ContractorID.Focus();
        ContractorID = Convert.ToInt16(Request.QueryString["Id"]);
        if (!IsPostBack)
        { 
            DataSet ds = new DataSet();
            string userGroupName = string.Empty;
            ds = objempType.GetContractorEditDetails(ContractorID, userGroupName);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_ContractorName.Text = ds.Tables[0].Rows[0]["ContractorName"].ToString();
                txt_ContractorID.Text = ds.Tables[0].Rows[0]["ContractorID"].ToString();
                txt_EmailID.Text = ds.Tables[0].Rows[0]["ContractorEmailID"].ToString();
                txt_Mobileno.Text = ds.Tables[0].Rows[0]["ContractorMobileNumber"].ToString();
                txt_ContactorAddress.Text = ds.Tables[0].Rows[0]["ContractorAddress"].ToString();
            }
            ds.Dispose();
            //btn_add.OnClientClick = "if(Page_ClientValidate()) return confirm('" + rm.GetString("Alert_ConfirmSave", ci) + "');return false;";
            strSetAlert = strSetAlert + "<script language='javascript'>";
            strSetAlert = strSetAlert + "MaxLengthh('" + rm.GetString("Alert_MaximumLengthis", ci) + "');";
            strSetAlert = strSetAlert + "</script>";
            Page.RegisterStartupScript("MaxLengthh", strSetAlert);
            objcommon.menu("EmployeeMaster", "228");
        }


    }
    #endregion
    #region ButtonEvents

    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TA_UI_Master_EmployeeContractorList.aspx");
    }
    #endregion
    #region UserDefinedFunctions
    //Getting Information from resource file and display it in label
    private void LoadStrings(CultureInfo ci)
    {
        lbl_ContractorName.Text = rm.GetString("Contractor_Name", ci);
        lbl_ContractorID.Text = rm.GetString("Contractor_ID", ci);
        lbl_EmailID.Text = rm.GetString("Contractor_Email", ci);
        lbl_Mobileno.Text = rm.GetString("Contractor_Mobileno", ci);
        lbl_IndicatesMandatoryFields.Text = rm.GetString("IndicatesMandatoryFields", ci);
        //RequiredFieldValidator2.ErrorMessage = rm.GetString("Validator_Grade_GradeName", ci);
        //RequiredFieldValidator1.ErrorMessage = rm.GetString("Validator_Grade_GradeLevel", ci);
        lbl_HeaderContractor.Text = rm.GetString("Master_contractor", ci);
        btn_add.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        btn_Cancel.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Back", ci);
        lbl_ContactorAddress.Text = rm.GetString("EmployeeMaster_ContactAddress", ci);
    }
    private void UserRights()
    {
        ds_Checkuser = objCheckUser.checkRights(228, Convert.ToInt32(Session["userGroup_Id"]), "addPrv");
        if (ds_Checkuser.Tables[0].Rows.Count == 0)
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    #endregion

    protected void btn_add_Click(object sender, EventArgs e)
    {

        // For Contractorname and other string values
        string Contractorid = txt_ContractorID.Text.Trim().Replace("'", "''");
        string Contractorname = txt_ContractorName.Text.Trim().Replace("'", "''");
        string Contractormail = txt_EmailID.Text.Trim().Replace("'", "''");
        string Contractorphone = txt_Mobileno.Text.Trim().Replace("'", "''");
        string ContractorAddress = txt_ContactorAddress.Text.Trim().Replace("'", "''");
        // For Contractorid, use TryParse to safely convert the string to an integer
        //int Contractorid;
        //if (!int.TryParse(txt_ContractorID.Text.Trim().Replace("'", "''"), out Contractorid))
        //{
        //    // Handle the error if the conversion fails, such as displaying a message
        //    // Example: "Please enter a valid Contractor ID."
        //    Console.WriteLine("Invalid Contractor ID.");
        //}

        //int count = objempType.CheckContractorID_Insert(Contractorid);
        //if (count > 0)
        //{
        //    base.Message(rm.GetString("Masters_Contractor_Already_Declared", ci));
        //    return;
        //}

        if (string.IsNullOrEmpty(Contractormail))
        {
            // Skip validation if the email field is empty or null
        }
        else if (!IsValidEmail(Contractormail))
        {
            // Return an error message if the email is invalid
            base.Message("Enter valid email.");
            return;
        }

        objempType.UpdateContractor(Contractorname, Contractorid, Contractormail, Contractorphone, ContractorID, ContractorAddress);
        string str = "alert('" + rm.GetString("Alert_ContractorModifysuccessfully", ci) + "');location.replace('TA_UI_Master_EmployeeContractorList.aspx');";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertBox", str, true);

    }
    private bool IsValidEmail(string email)
    {
        // Use a regular expression to validate the email format
        var emailRegex = new System.Text.RegularExpressions.Regex(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$");
        return emailRegex.IsMatch(email);
    }

}