<?php 
	
    include('header.php');
    
    $countries = array(
        "AF" => "Afghanistan",
        "AX" => "Åland Islands",
        "AL" => "Albania",
        "DZ" => "Algeria",
        "AS" => "American Samoa",
        "AD" => "Andorra",
        "AO" => "Angola",
        "AI" => "Anguilla",
        "AQ" => "Antarctica",
        "AG" => "Antigua and Barbuda",
        "AR" => "Argentina",
        "AM" => "Armenia",
        "AW" => "Aruba",
        "AU" => "Australia",
        "AT" => "Austria",
        "AZ" => "Azerbaijan",
        "BS" => "Bahamas",
        "BH" => "Bahrain",
        "BD" => "Bangladesh",
        "BB" => "Barbados",
        "BY" => "Belarus",
        "BE" => "Belgium",
        "BZ" => "Belize",
        "BJ" => "Benin",
        "BM" => "Bermuda",
        "BT" => "Bhutan",
        "BO" => "Bolivia",
        "BA" => "Bosnia and Herzegovina",
        "BW" => "Botswana",
        "BV" => "Bouvet Island",
        "BR" => "Brazil",
        "IO" => "British Indian Ocean Territory",
        "BN" => "Brunei Darussalam",
        "BG" => "Bulgaria",
        "BF" => "Burkina Faso",
        "BI" => "Burundi",
        "KH" => "Cambodia",
        "CM" => "Cameroon",
        "CA" => "Canada",
        "CV" => "Cape Verde",
        "KY" => "Cayman Islands",
        "CF" => "Central African Republic",
        "TD" => "Chad",
        "CL" => "Chile",
        "CN" => "China",
        "CX" => "Christmas Island",
        "CC" => "Cocos (Keeling) Islands",
        "CO" => "Colombia",
        "KM" => "Comoros",
        "CG" => "Congo",
        "CD" => "Congo, The Democratic Republic of The",
        "CK" => "Cook Islands",
        "CR" => "Costa Rica",
        "CI" => "Cote D'ivoire",
        "HR" => "Croatia",
        "CU" => "Cuba",
        "CY" => "Cyprus",
        "CZ" => "Czech Republic",
        "DK" => "Denmark",
        "DJ" => "Djibouti",
        "DM" => "Dominica",
        "DO" => "Dominican Republic",
        "EC" => "Ecuador",
        "EG" => "Egypt",
        "SV" => "El Salvador",
        "GQ" => "Equatorial Guinea",
        "ER" => "Eritrea",
        "EE" => "Estonia",
        "ET" => "Ethiopia",
        "FK" => "Falkland Islands (Malvinas)",
        "FO" => "Faroe Islands",
        "FJ" => "Fiji",
        "FI" => "Finland",
        "FR" => "France",
        "GF" => "French Guiana",
        "PF" => "French Polynesia",
        "TF" => "French Southern Territories",
        "GA" => "Gabon",
        "GM" => "Gambia",
        "GE" => "Georgia",
        "DE" => "Germany",
        "GH" => "Ghana",
        "GI" => "Gibraltar",
        "GR" => "Greece",
        "GL" => "Greenland",
        "GD" => "Grenada",
        "GP" => "Guadeloupe",
        "GU" => "Guam",
        "GT" => "Guatemala",
        "GG" => "Guernsey",
        "GN" => "Guinea",
        "GW" => "Guinea-bissau",
        "GY" => "Guyana",
        "HT" => "Haiti",
        "HM" => "Heard Island and Mcdonald Islands",
        "VA" => "Holy See (Vatican City State)",
        "HN" => "Honduras",
        "HK" => "Hong Kong",
        "HU" => "Hungary",
        "IS" => "Iceland",
        "IN" => "India",
        "ID" => "Indonesia",
        "IR" => "Iran, Islamic Republic of",
        "IQ" => "Iraq",
        "IE" => "Ireland",
        "IM" => "Isle of Man",
        "IL" => "Israel",
        "IT" => "Italy",
        "JM" => "Jamaica",
        "JP" => "Japan",
        "JE" => "Jersey",
        "JO" => "Jordan",
        "KZ" => "Kazakhstan",
        "KE" => "Kenya",
        "KI" => "Kiribati",
        "KP" => "Korea, Democratic People's Republic of",
        "KR" => "Korea, Republic of",
        "KW" => "Kuwait",
        "KG" => "Kyrgyzstan",
        "LA" => "Lao People's Democratic Republic",
        "LV" => "Latvia",
        "LB" => "Lebanon",
        "LS" => "Lesotho",
        "LR" => "Liberia",
        "LY" => "Libyan Arab Jamahiriya",
        "LI" => "Liechtenstein",
        "LT" => "Lithuania",
        "LU" => "Luxembourg",
        "MO" => "Macao",
        "MK" => "Macedonia, The Former Yugoslav Republic of",
        "MG" => "Madagascar",
        "MW" => "Malawi",
        "MY" => "Malaysia",
        "MV" => "Maldives",
        "ML" => "Mali",
        "MT" => "Malta",
        "MH" => "Marshall Islands",
        "MQ" => "Martinique",
        "MR" => "Mauritania",
        "MU" => "Mauritius",
        "YT" => "Mayotte",
        "MX" => "Mexico",
        "FM" => "Micronesia, Federated States of",
        "MD" => "Moldova, Republic of",
        "MC" => "Monaco",
        "MN" => "Mongolia",
        "ME" => "Montenegro",
        "MS" => "Montserrat",
        "MA" => "Morocco",
        "MZ" => "Mozambique",
        "MM" => "Myanmar",
        "NA" => "Namibia",
        "NR" => "Nauru",
        "NP" => "Nepal",
        "NL" => "Netherlands",
        "AN" => "Netherlands Antilles",
        "NC" => "New Caledonia",
        "NZ" => "New Zealand",
        "NI" => "Nicaragua",
        "NE" => "Niger",
        "NG" => "Nigeria",
        "NU" => "Niue",
        "NF" => "Norfolk Island",
        "MP" => "Northern Mariana Islands",
        "NO" => "Norway",
        "OM" => "Oman",
        "PK" => "Pakistan",
        "PW" => "Palau",
        "PS" => "Palestinian Territory, Occupied",
        "PA" => "Panama",
        "PG" => "Papua New Guinea",
        "PY" => "Paraguay",
        "PE" => "Peru",
        "PH" => "Philippines",
        "PN" => "Pitcairn",
        "PL" => "Poland",
        "PT" => "Portugal",
        "PR" => "Puerto Rico",
        "QA" => "Qatar",
        "RE" => "Reunion",
        "RO" => "Romania",
        "RU" => "Russian Federation",
        "RW" => "Rwanda",
        "SH" => "Saint Helena",
        "KN" => "Saint Kitts and Nevis",
        "LC" => "Saint Lucia",
        "PM" => "Saint Pierre and Miquelon",
        "VC" => "Saint Vincent and The Grenadines",
        "WS" => "Samoa",
        "SM" => "San Marino",
        "ST" => "Sao Tome and Principe",
        "SA" => "Saudi Arabia",
        "SN" => "Senegal",
        "RS" => "Serbia",
        "SC" => "Seychelles",
        "SL" => "Sierra Leone",
        "SG" => "Singapore",
        "SK" => "Slovakia",
        "SI" => "Slovenia",
        "SB" => "Solomon Islands",
        "SO" => "Somalia",
        "ZA" => "South Africa",
        "GS" => "South Georgia and The South Sandwich Islands",
        "ES" => "Spain",
        "LK" => "Sri Lanka",
        "SD" => "Sudan",
        "SR" => "Suriname",
        "SJ" => "Svalbard and Jan Mayen",
        "SZ" => "Swaziland",
        "SE" => "Sweden",
        "CH" => "Switzerland",
        "SY" => "Syrian Arab Republic",
        "TW" => "Taiwan, Province of China",
        "TJ" => "Tajikistan",
        "TZ" => "Tanzania, United Republic of",
        "TH" => "Thailand",
        "TL" => "Timor-leste",
        "TG" => "Togo",
        "TK" => "Tokelau",
        "TO" => "Tonga",
        "TT" => "Trinidad and Tobago",
        "TN" => "Tunisia",
        "TR" => "Turkey",
        "TM" => "Turkmenistan",
        "TC" => "Turks and Caicos Islands",
        "TV" => "Tuvalu",
        "UG" => "Uganda",
        "UA" => "Ukraine",
        "AE" => "United Arab Emirates",
        "GB" => "United Kingdom",
        "US" => "United States",
        "UM" => "United States Minor Outlying Islands",
        "UY" => "Uruguay",
        "UZ" => "Uzbekistan",
        "VU" => "Vanuatu",
        "VE" => "Venezuela",
        "VN" => "Viet Nam",
        "VG" => "Virgin Islands, British",
        "VI" => "Virgin Islands, U.S.",
        "WF" => "Wallis and Futuna",
        "EH" => "Western Sahara",
        "YE" => "Yemen",
        "ZM" => "Zambia",
        "ZW" => "Zimbabwe"
    );
    
    $report="";
    
    if(isset($_POST['cAcc'])){
		
		$fn = $_POST['fn'];
		$ln = $_POST['ln'];
		$ad = $_POST['add'];
		$ct = $_POST['city'];
		$st = $_POST['state'];
		$cn = $_POST['country'];
		$pz = $_POST['pz'];
		$ph = $_POST['ph_N'];
		$em = $_POST['email'];
		$dob = $_POST['dob'];
		$gen = $_POST['gender'];
		$nat = $_POST['nation'];
		$occ = $_POST['occu'];
        $pword = $_POST['pw'];
        
        $login_id = rand(11111111,99999999);
                
        $sl="SELECT * FROM users WHERE email ='$em'";
        $rs=mysqli_query($conn,$sl);
        $row=mysqli_fetch_array($rs);
        $cnt = mysqli_num_rows($rs); // if uname/pass correct it returns must be 1 row
                                    
        if($cnt > 0) 
        {
          $report="<p style='color: indianred;'>Error! An account with the specified email address exists already!</p>";
        }else{
            $sst="INSERT INTO settings(memID, sA1, sA2, sA3, eP, eB, eT, e3D, eEa, eSa, b2, b3, b4, b5)values('".$login_id."', 'yes', 'yes', 'yes', 'yes', 'yes','yes','yes','yes','yes','yes','yes','yes','yes')";
            $rss=mysqli_query($conn,$sst);
    
            $sql="INSERT INTO users(fn, ln, address, townCity, state, country, zip, pNumber, email, dob, gender, nation, occu, memID, pass, status)values('".mysqli_real_escape_string($conn, $fn)."', '".mysqli_real_escape_string($conn, $ln)."', '".mysqli_real_escape_string($conn, $ad)."', '".mysqli_real_escape_string($conn, $ct)."', '".mysqli_real_escape_string($conn, $st)."', '".mysqli_real_escape_string($conn, $cn)."', '".mysqli_real_escape_string($conn, $pz)."', '".mysqli_real_escape_string($conn, $ph)."', '".mysqli_real_escape_string($conn, $em)."', '".mysqli_real_escape_string($conn, $dob)."', '".mysqli_real_escape_string($conn, $gen)."', '".mysqli_real_escape_string($conn, $nat)."', '".mysqli_real_escape_string($conn, $occ)."', '".mysqli_real_escape_string($conn, $login_id)."', '".mysqli_real_escape_string($conn, $pword)."', '1')";
			$result=mysqli_query($conn,$sql);
                                
            if ($result) {
                $last_id = mysqli_insert_id($conn);
                      
                echo "<meta http-equiv='refresh' content='3; url=client_info.php?id=".$last_id."' >";
                $report = "<p style='color: limegreen;'>Account created successfully! Redirecting...</p>";	
            } else {
                $report = "<p style='color: indianred;'>Something went wrong, try again later...</p>";	
            }
        }
    }



// Report message
$report = "";

// Handle phone update
if (isset($_POST['update_phone'])) {
    $phone = htmlspecialchars($_POST['phone']);

    $check = $conn->query("SELECT * FROM site_contact_info WHERE type='phone'");
    if ($check->num_rows > 0) {
        $conn->query("UPDATE site_contact_info SET value='$phone' WHERE type='phone'");
    } else {
        $conn->query("INSERT INTO site_contact_info (type, value) VALUES ('phone', '$phone')");
    }
    $report = "<div class='alert alert-success'>Phone number updated.</div>";
}

// Handle email update
if (isset($_POST['update_email'])) {
    $email = htmlspecialchars($_POST['email']);

    $check = $conn->query("SELECT * FROM site_contact_info WHERE type='email'");
    if ($check->num_rows > 0) {
        $conn->query("UPDATE site_contact_info SET value='$email' WHERE type='email'");
    } else {
        $conn->query("INSERT INTO site_contact_info (type, value) VALUES ('email', '$email')");
    }
    $report = "<div class='alert alert-success'>Email address updated.</div>";
}

// Fetch current data
$phoneResult = $conn->query("SELECT value FROM site_contact_info WHERE type='phone'");
$emailResult = $conn->query("SELECT value FROM site_contact_info WHERE type='email'");
$phone = $phoneResult->num_rows > 0 ? $phoneResult->fetch_assoc()['value'] : '';
$email = $emailResult->num_rows > 0 ? $emailResult->fetch_assoc()['value'] : '';
?>

    
    <!-- MENU SECTION END-->
    <div class="content-wrapper">
        <div class="container" >
            <div class="row">
				 <div class="col-md-6 col-sm-6 col-xs-12" >
					 <div class="col-md-12 col-sm-12 col-xs-12">
					 <a href="client_records.php" style="text-decoration: none;">
						<div class="dashboard-div-wrapper bk-clr-two" style="background-color:#011021">
							<img src="assets/img/cr.png" style="width:80px">                       
						   <h3>View Client Records</h3>
						</div>
						</a>
					</div>
					<div class="col-md-12 col-sm-12 col-xs-12" >
					 <a href="admins.php" style="text-decoration: none;">
						<div class="dashboard-div-wrapper bk-clr-one" style="background-color:#011021">
							<img src="assets/img/Users-User-Male-2-icon.png" style="width:80px">
							 <h3>Manage Admin's</h3>
						</div>
						</a>
					</div>	
					<div class="col-md-12 col-sm-12 col-xs-12"> 
  <a href="update_contact.php" style="text-decoration: none;">
    <div class="dashboard-div-wrapper" style="background-color:#011021; border-radius: 12px; padding: 20px; text-align: center; transition: 0.3s ease; box-shadow: 0 4px 10px rgba(0,0,0,0.2);">
      <img src="assets/img/contact-icon.png" alt="Contact Icon" style="width: 60px; margin-bottom: 15px;">
      <h4 style="color: #ffffff; margin: 0; font-weight: 600;">Update Wallet/Contact Info</h4>
    </div>
  </a>
</div>

				</div>
				<div class="col-md-6 col-sm-6 col-xs-12" >
                    <div class="panel panel-success">
                        <div class="panel-heading">
                            <span style="font-weight: bold;">Add New User</span>
                        </div>
                        <div class="panel-body">
                            <?php echo $report; ?>
                        	<form action="" method="post">
                                <div class="form-group">
                                    <label >First Name</label>
                                    <br>
                                    <input type="text" name="fn" class="form-control" placeholder="Enter First Name" required />
                                </div>
                                
                                <div class="form-group">
                                    <label >Last Name</label>
                                    <br>
                                    <input type="text" name="ln" class="form-control" placeholder="Enter Last Name" required  />
                                </div>  
                          
                                <div class="form-group">
                                    <label >Phone Number</label>
                                    <input type="text" class="form-control" name="ph_N" placeholder="Enter Phone Number" required  />
                                </div>
                          
                                <div class="form-group">
                                    <label >Email Address</label>
                                    <input type="email" class="form-control" name="email" placeholder="Enter Email Address" required />
                                </div>
                                
                                <div class="form-group">
                                    	      
                                    <label >Date Of Birth (yyyy-mm-dd)</label>
                                    <input type="value" id="flatpickr" placeholder="YYY-MM-DD"  class="form-control" name="dob"  required  />
                                    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
                                    <script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
                                    <script>
                                        flatpickr('#flatpickr',{
                                          dateFormat: 'Y-m-d',
                                          disableMobile: "true"
                                        });
                                    </script>
                            
                                </div>
                                
                                <div class="form-group">
                                    <label >Select Gender</label><br>
                                    
                                	<label><input type="radio" value="male" name="gender" > Male</label> &nbsp; &nbsp;
                                	<label><input type="radio" value="female" name="gender" > Female</label>
                                </div>
                          
                                <div class="form-group">
                                    <label >Nationality</label>
                                    <input type="text" class="form-control" name="nation" id="nationaliity" placeholder="Enter Nationality" required />
                                </div>
                          
                                <div class="form-group">
                                    <label >Occupation</label>
                                    <input type="text" class="form-control" name="occu" placeholder="Enter Occupation" required />
                                </div>
                          
                                <div class="form-group">
                                    <label >Residential Address</label>
                                    <input type="text" name="add" class="form-control" placeholder="Enter Residential Address" required  />
                                </div>
                          
                                <div class="form-group">
                                    <label >Postcode / Zip</label>
                                    <input type="text" class="form-control" name="pz" placeholder="Enter Postcode / Zip" required />
                                </div>
                        
                                <div class="form-group">
                                    <label >Town/City</label>
                                    <input type="text" class="form-control" name="city" placeholder="Enter Town / City" required  />
                                </div>
                          
                                <div class="form-group">
                                    <label >State/County</label>
                                    <input type="text" class="form-control" name="state" placeholder="Enter State / County" required />
                                </div>
                        
                        	    <div class="form-group">
                                    <label >Country</label>
                                    <select class="form-control" name="country" required >
                                        <option value="" disabled selected>Country</option>
                                    	<?php
                                    	foreach($countries as $key => $value) {
                                    	?>
                                    	<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
                                    	<?php
                                    	}
                                    	?>
                                    </select>
                                </div>
                        	
                                <div class="form-group">
                                    <label >Password</label>
                                    <input type="password" class="form-control" id="pw" name="pw" placeholder="Enter Password" required />
                                </div>
                        	
                                <div class="form-group">
                                    <input type="submit" class="btn btn-success" style="width:100%;" value="Add User" name="cAcc">
                                </div>
                            </form>
	                    </div>
                    </div>
				</div>
            </div>
        </div>
    
   <?php
// Initialize variables for the current phone and email (These could come from a database or session)
$phone = "Current phone number from DB or session";
$email = "Current email address from DB or session";

// Handle form submission for phone update
if (isset($_POST['update_phone'])) {
    $new_phone = $_POST['phone'];
    
    // Validate and sanitize the phone number before updating
    if (empty($new_phone)) {
        $report = "Phone number cannot be empty.";
    } else {
        // Update the phone number in your database
        // Example: update_phone_number($new_phone);
        $phone = $new_phone;
        $report = "Phone number updated successfully.";
    }
}

// Handle form submission for email update
if (isset($_POST['update_email'])) {
    $new_email = $_POST['email'];

    // Validate and sanitize the email before updating
    if (empty($new_email)) {
        $report = "Email address cannot be empty.";
    } elseif (!filter_var($new_email, FILTER_VALIDATE_EMAIL)) {
        $report = "Invalid email address.";
    } else {
        // Update the email in your database
        // Example: update_email_address($new_email);
        $email = $new_email;
        $report = "Email address updated successfully.";
    }
}
?>

<!-- HTML Form -->
<!--<div class="col-md-6 col-sm-6 col-xs-12">-->
<!--    <div class="panel panel-success">-->
<!--        <div class="panel-heading">-->
<!--            <strong>Manage Contact Info</strong>-->
<!--        </div>-->
<!--        <div class="panel-body">-->
<!--            <?php echo $report; ?>-->

            <!-- View Section -->
<!--            <div class="form-group">-->
<!--                <label>Current Phone Number:</label>-->
<!--                <div class="well"><?php echo $phone ?: 'Not set'; ?></div>-->
<!--            </div>-->
<!--            <div class="form-group">-->
<!--                <label>Current Email Address:</label>-->
<!--                <div class="well"><?php echo $email ?: 'Not set'; ?></div>-->
<!--            </div>-->

<!--            <hr>-->

            <!-- Update Phone Form -->
<!--            <form action="" method="post">-->
<!--                <div class="form-group">-->
<!--                    <label>Update Phone Number</label>-->
<!--                    <input type="text" class="form-control" name="phone" value="<?php echo htmlspecialchars($phone); ?>" required>-->
<!--                </div>-->
<!--                <div class="form-group">-->
<!--                    <button type="submit" name="update_phone" class="btn btn-primary">Update Phone</button>-->
<!--                </div>-->
<!--            </form>-->

<!--            <hr>-->

            <!-- Update Email Form -->
<!--            <form action="" method="post">-->
<!--                <div class="form-group">-->
<!--                    <label>Update Email Address</label>-->
<!--                    <input type="email" class="form-control" name="email" value="<?php echo htmlspecialchars($email); ?>" required>-->
<!--                </div>-->
<!--                <div class="form-group">-->
<!--                    <button type="submit" name="update_email" class="btn btn-primary">Update Email</button>-->
<!--                </div>-->
<!--            </form>-->
<!--        </div>-->
<!--    </div>-->
<!--</div>-->



    
    </div>
   
    <!-- CONTENT-WRAPPER SECTION END-->
    <footer style="background-color:#071A30">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    Copyright &copy; <?php echo date("Y"); ?> | ALL RIGHTS RESERVED. 
                </div>

            </div>
        </div>
    </footer>
    <!-- FOOTER SECTION END-->
    <!-- JAVASCRIPT AT THE BOTTOM TO REDUCE THE LOADING TIME  -->
    <!-- CORE JQUERY SCRIPTS -->
    <script src="assets/js/jquery-1.11.1.js"></script>
    <!-- BOOTSTRAP SCRIPTS  -->
    <script src="assets/js/bootstrap.js"></script>
</body>
</html>
