<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
include_once ('../include/session.php');
include_once ('../include/dbconfig.php');
require_once '../include/class.user.php';
if(!isset($_SESSION['uname'])){
header("Location: login.php");
exit(); 
}

// Database connection
$connection = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Process form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Sanitize form inputs
    $username = htmlspecialchars($_POST['username'] ?? '');
    $email = htmlspecialchars($_POST['email'] ?? '');
    $alt_email = htmlspecialchars($_POST['alt_email'] ?? '');
    $fname = htmlspecialchars($_POST['fname'] ?? '');
    $lname = htmlspecialchars($_POST['lname'] ?? '');
    $work = htmlspecialchars($_POST['work'] ?? '');
    $phone = htmlspecialchars($_POST['phone'] ?? '');
    $alt_phone = htmlspecialchars($_POST['alt_phone'] ?? '');
    $phone_verify = htmlspecialchars($_POST['phone_verify'] ?? '');
    $gender = htmlspecialchars($_POST['gender'] ?? '');
    $marital_status = htmlspecialchars($_POST['marital_status'] ?? '');
    $address = htmlspecialchars($_POST['address'] ?? '');

    // Prepare and execute the update query
    $update_query = "UPDATE account SET 
        username = ?, email = ?, alt_email = ?, fname = ?, lname = ?, 
        work = ?, phone = ?, alt_phone = ?, phone_verify = ?, 
        sex = ?, marry = ?, addr = ? WHERE uname = ?";

    if ($stmt = $connection->prepare($update_query)) {
        $stmt->bind_param(
            'sssssssssssss',
            $username, $email, $alt_email, $fname, $lname, $work, $phone,
            $alt_phone, $phone_verify, $gender, $marital_status, $address, $_SESSION['uname']
        );

        if ($stmt->execute()) {
            $success_message = "Profile updated successfully.";
        } else {
            $error_message = "Failed to update profile. Error: " . $stmt->error;
        }

        $stmt->close();
    } else {
        $error_message = "Failed to prepare the statement. Error: " . $connection->error;
    }
}

// Close the connection
$connection->close();

$reg_user = new USER();

$stmt = $reg_user->runQuery("SELECT * FROM account WHERE uname=:uname");
$stmt->execute(array(":uname"=>$_SESSION['uname']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

include_once ('counter.php');?>

 <?php include 'header.php'; ?>
 
 <!---begin of Mobile View Here   only from Team-->
 
       <?php include 'mobmenu.php'; ?>
  
  <!---End of Mobile View Here   only from Team-->
  
   <!---begin of Main Menu View View Here   only from Team-->
   <?php include 'mainmenu.php'; ?>

     <!---End of Main Menu View Here   only from Team-->
     
     
     
  
    <div class="container-fluid mt--7">
      
      
      
      
      
      
      
     <div class="row">
        
        <div class="col-xl-8 order-xl-1">
          <d <div class="col-xl-8 order-xl-1"><br><br>
          <div class="card bg"> 
            <div class="card-header border-0" style="background-color:<?php echo $tab; ?>; color:<?php echo $tab_font; ?>;">
              <div class="row align-items-center">
                <div class="col-8">
                  <h3 class="mb-0">Customer Profile</h3>
                </div>
                <div class="col-4 text-right">
                  <a href="ticket.php" class="btn btn-warning">Help</a>
                </div>
              </div>
            </div>
            <div class="card-body">
                <?php if (!empty($success_message)) echo "<div class='alert alert-success'>$success_message</div>"; ?>
                <?php if (!empty($error_message)) echo "<div class='alert alert-danger'>$error_message</div>"; ?>

              <form method="POST" action="">
                <h6 class="heading-small text-muted mb-4">User information</h6>
                <div class="pl-lg-4">
                    <center><img class="nav-profile-img mr-2" alt="" src="admin/pic/<?php echo $row['username']; ?>.jpg" alt="photo"  style="width:100px; height:100px; border-radius:60px;"/></center>
                 <hr>
                  <div class="row">
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-username">Username</label>
                        <input type="text" id="input-username" class="form-control form-control-alternative" name="username"  value="<?php echo $row['username']; ?>">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-email">Email address</label>
                        <input type="email" id="input-email" class="form-control form-control-alternative" name="email" value="<?php echo $row['email']; ?>">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-alt-email">Alternative Email address</label>
                        <input type="email" id="input-alt-email" class="form-control form-control-alternative" name="alt_email" value="<?php echo $row['alt_email']; ?>">
                      </div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-first-name">First name</label>
                        <input type="text" id="input-first-name" class="form-control form-control-alternative" name="fname" value="<?php echo $row['fname']; ?> ">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-last-name">Last name</label>
                        <input type="text" id="input-last-name" class="form-control form-control-alternative" placeholder="Last name" name="lname" value="<?php echo $row['lname']; ?>">
                      </div>
                    </div>
                  </div>
                   <div class="row">
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-first-name">Work/Employment</label>
                        <input type="text" id="input-first-name" class="form-control form-control-alternative" name="work" value="<?php echo $row['work']; ?> ">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-last-name">Mobile Number</label>
                        <input type="text" id="input-last-name" class="form-control form-control-alternative" name="phone" value="<?php echo $row['phone']; ?>">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-alt-mobile">Alternative Mobile Number</label>
                        <input type="text" id="input-alt-mobile" class="form-control form-control-alternative" name="alt_phone" value="<?php echo $row['alt_phone']; ?>">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-alt-mobile">Select Login Verification Method</label>
                        <select name="phone_verify" class="form-control input-sm validate[required]">
                            <option value="">Select Verification Type</option>
                            <option <?= $row['phone_verify'] == '1' ? 'selected' : ''; ?> value="1">PIN</option>
                            <option <?= $row['phone_verify'] == '0' ? 'selected' : ''; ?> value="0">Two-Factor Authentication (2FA)</option>
                        </select>
                      </div>
                    </div>
                  </div>
                  
                     <div class="row">
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-first-name">Gender</label>
                        <input type="text" id="input-first-name" class="form-control form-control-alternative" name="gender" value="<?php echo $row['sex']; ?> ">
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group">
                        <label class="form-control-label" for="input-last-name">Marital Status</label>
                        <input type="text" id="input-last-name" class="form-control form-control-alternative" name="marital_status" value="<?php echo $row['marry']; ?>">
                      </div>
                    </div>
                  </div>
                </div>
                <hr class="my-4" />
                <!-- Address -->
                <h6 class="heading-small text-muted mb-4">Contact information</h6>
                <div class="pl-lg-4">
                  <div class="row">
                    <div class="col-md-12">
                      <div class="form-group">
                        <label class="form-control-label" for="input-address">Contact Address</label>
                        <input id="input-address" class="form-control form-control-alternative" placeholder="Home Address" name="address" value="<?php echo $row['addr']; ?>" type="text">
                      </div>
                    </div>
                  </div>
                 
                </div>
                <button type="submit" class="btn btn-primary w-100 mt-3">Save Changes</button>
              </form>
            </div>
          </div>
        </div>
      </div>
      <!-- Footer -->
       <?php include 'footercopyright.php'; ?>
      
     