namespace LagaoBoli; using Microsoft.Maui.Media; using System.Text; using LagaoBoli.Models; using Microsoft.Maui.Controls; using System.Text.Json; using System.Text.RegularExpressions; using System.Net.Http; using System.Threading.Tasks; public partial class SignUpPage : ContentPage { List stateList = new(); List cityList = new(); int val = 0; string base64Image = ""; bool isPasswordVisible = false; public SignUpPage(int a) { InitializeComponent(); OnAppearing(); if (a == 1) { val = a; txtName.Text = UserProfile_Static.Name; txtFName.Text = UserProfile_Static.FName; txtMobile.Text = UserProfile_Static.Mobile; LoadStateCities(); LoadCities(); btnShowPassword.IsVisible = false; txtPassword.Text = "EditProfile"; txtPassword.IsVisible = false; txtOTP.IsVisible = false; txtEmaild.Text = UserProfile_Static.EmailID; btnGetOtp.IsVisible = false; imgProfileCon.IsVisible = false; btnProfileUpdate.Text = "Update Profile"; } } public async Task LoadStateCities() { HttpClient client = new HttpClient(); var response = await client.GetStringAsync("https://funkhelojeeto.com/lb_Mob/service1.svc/GetStates"); stateList = JsonSerializer.Deserialize>(response); pickerState.ItemsSource = stateList; pickerState.ItemDisplayBinding = new Binding("Name"); // ensure UI thread (important in MAUI) MainThread.BeginInvokeOnMainThread(() => { int index = stateList.FindIndex(x => x.StateId.ToString() == UserProfile_Static.State); if (index >= 0) pickerState.SelectedIndex = index; }); } public async Task LoadCities() { HttpClient client = new HttpClient(); var response = await client.GetStringAsync($"https://funkhelojeeto.com/lb_Mob/service1.svc/GetCities/{UserProfile_Static.State}"); cityList = JsonSerializer.Deserialize>(response); pickerCity.ItemsSource = cityList; pickerCity.ItemDisplayBinding = new Binding("Name"); // ensure UI thread (important in MAUI) MainThread.BeginInvokeOnMainThread(() => { int index = cityList.FindIndex(x => x.CityId == UserProfile_Static.City); if (index >= 0) pickerCity.SelectedIndex = index; }); } protected override async void OnAppearing() { base.OnAppearing(); await LoadStates(); } private async void pickerState_SelectedIndexChanged(object sender, EventArgs e) { if (pickerState.SelectedItem is StateModel selectedState) { HttpClient client = new HttpClient(); var response = await client.GetStringAsync( $"https://funkhelojeeto.com/lb_Mob/service1.svc/GetCities/{selectedState.StateId}"); cityList = JsonSerializer.Deserialize>(response); pickerCity.ItemsSource = cityList; pickerCity.ItemDisplayBinding = new Binding("Name"); } } // Static HttpClient configured with the base WCF service domain private static readonly HttpClient client = new HttpClient { BaseAddress = new Uri("https://funkhelojeeto.com") }; /// /// Handles sending OTP by calling WCF API and opening WhatsApp to deliver the OTP. /// private async void OnGetOtpClicked(object sender, EventArgs e) { try { string mobile = txtMobile.Text?.Trim(); if (string.IsNullOrEmpty(mobile) || mobile.Length != 10) { lblmsg.Text = "Enter valid 10-digit mobile number"; await DisplayAlert("Validation Error", "Please enter a valid 10-digit mobile number.", "OK"); return; } btnGetOtp.IsEnabled = false; // 1. Call WCF Service API (GetMobileOTP) to generate the OTP and register it in the DB var response = await client.GetAsync($"/lb_Mob/service1.svc/GetMobileOTP/{mobile}"); if (response.IsSuccessStatusCode) { string resultJson = await response.Content.ReadAsStringAsync(); // Parse returned OTP value string otpVal = resultJson?.Replace("\"", "").Trim(); if (!string.IsNullOrEmpty(otpVal)) { // 2. Open WhatsApp to send the OTP message directly via WhatsApp string whatsappUrl = $"https://wa.me/91{mobile}?text=Your%20OTP%20for%20Lagao%20Boli%20signup%20is%20{otpVal}"; // Launch WhatsApp app/web await Launcher.Default.OpenAsync(new Uri(whatsappUrl)); await DisplayAlert("OTP Sent", "We have registered your OTP and opened WhatsApp. Please send the pre-filled message to receive your OTP.", "OK"); #if DEBUG System.Diagnostics.Debug.WriteLine($"WCF Service OTP: {otpVal}"); #endif } else { await DisplayAlert("Error", "Invalid OTP value returned from server.", "OK"); } } else { throw new Exception($"WCF API returned HTTP status {response.StatusCode}"); } } catch (Exception ex) { lblmsg.Text = "Error: " + ex.Message; await DisplayAlert("Error", "Failed to send OTP: " + ex.Message, "OK"); } finally { btnGetOtp.IsEnabled = true; } } private async Task LoadStates() { HttpClient client = new HttpClient(); var response = await client.GetStringAsync("https://funkhelojeeto.com/lb_Mob/service1.svc/GetStates"); stateList = JsonSerializer.Deserialize>(response); pickerState.ItemsSource = stateList; pickerState.ItemDisplayBinding = new Binding("Name"); } private void OnMobileTextChanged(object sender, TextChangedEventArgs e) { string mobile = txtMobile.Text; // Check: 10 digits & only numbers & starts with 6-9 (India mobile rule) if (!string.IsNullOrEmpty(mobile) && mobile.Length == 10 && System.Text.RegularExpressions.Regex.IsMatch(mobile, "^[6-9][0-9]{9}$")) { btnGetOtp.IsEnabled = true; lblmsg.Text = ""; } else { btnGetOtp.IsEnabled = false; lblmsg.Text = "Enter valid 10-digit mobile number"; } } private async void OnSignUpClicked(object sender, EventArgs e) { try { if (!await ValidateForm()) return; var requestData = new SignUpRequest { Name = txtName.Text.Trim(), FName = txtFName.Text.Trim(), Mobile = txtMobile.Text.Trim(), State = ((StateModel)pickerState.SelectedItem).StateId, City = ((CityModel)pickerCity.SelectedItem).CityId, Password = txtPassword.Text, OTP = txtOTP.Text, EmailID = txtEmaild.Text.Trim(), ProfileImage = base64Image }; string json = JsonSerializer.Serialize(requestData); var content = new StringContent(json, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("/lb_Mob/service1.svc/SignUp", content); string resultJson = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { lblmsg.Text = "Server Error: " + response.StatusCode; return; } var result = JsonSerializer.Deserialize( resultJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (result != null) { lblmsg.Text = result.Message; if (result.Status == "Success") { await Navigation.PushAsync(new WelcomePage(requestData.Name)); } } else { lblmsg.Text = "Invalid server response."; } } catch (Exception ex) { lblmsg.Text = "Error: " + ex.Message; } } private async Task ValidateForm() { if (string.IsNullOrWhiteSpace(txtName.Text)) { lblmsg.Text = "Name is required"; return false; } if (string.IsNullOrWhiteSpace(txtMobile.Text)) { lblmsg.Text = "Mobile number is required"; return false; } if (txtMobile.Text.Length != 10) { lblmsg.Text = "Enter valid 10-digit mobile number"; return false; } if (pickerState.SelectedItem == null) { lblmsg.Text = "Please select State"; return false; } if (pickerCity.SelectedItem == null) { lblmsg.Text = "Please select City"; return false; } if (string.IsNullOrWhiteSpace(txtPassword.Text) && val == 0) { lblmsg.Text = "Password is required"; return false; } if (string.IsNullOrWhiteSpace(txtOTP.Text) && val == 0) { lblmsg.Text = "OTP is required"; return false; } return true; } private async void OnSignInTapped(object sender, EventArgs e) { await Navigation.PushAsync(new SignInPage()); } private async void OnCameraClicked(object sender, EventArgs e) { try { if (MediaPicker.Default.IsCaptureSupported) { FileResult photo = await MediaPicker.Default.CapturePhotoAsync(); if (photo != null) { var stream = await photo.OpenReadAsync(); imgProfile.Source = ImageSource.FromStream(() => stream); } } } catch (Exception ex) { await DisplayAlert("Error", ex.Message, "OK"); } } private async void OnGalleryClicked(object sender, EventArgs e) { try { FileResult photo = await MediaPicker.Default.PickPhotoAsync(); if (photo != null) { var stream = await photo.OpenReadAsync(); imgProfile.Source = ImageSource.FromStream(() => stream); } } catch (Exception ex) { await DisplayAlert("Error", ex.Message, "OK"); } } private void OnPasswordTextChanged(object sender, TextChangedEventArgs e) { string password = txtPassword.Text; string pattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$"; if (!string.IsNullOrEmpty(password) && Regex.IsMatch(password, pattern)) { lblmsg.Text = ""; } else { lblmsg.Text = "Password must be at least 8 characters with uppercase, lowercase, number & special character."; } } private void OnShowPasswordClicked(object sender, EventArgs e) { isPasswordVisible = !isPasswordVisible; txtPassword.IsPassword = !isPasswordVisible; btnShowPassword.Source = isPasswordVisible ? "eye_off.png" : "eye.png"; } }