<?php
session_start();
if (!isset($_SESSION['username'])) {
    echo "<script>window.top.location.href='index.php';</script>"; // Redirect parent window to login page
    exit();
}

include 'connect.php';
$userid=$_GET['updateid'];
$sql="select * from customer_table where customer_id=$userid";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
$cust_name=$row['cust_name'];
$cust_company=$row['cust_company'];
$cust_mobile=$row['cust_mobile'];
$cust_email=$row['cust_email'];

if(isset($_POST['submit'])){
    $cust_name=$_POST['cust_name'];
    $cust_company=$_POST['cust_company'];
    $cust_mobile=$_POST['cust_mobile'];
    $cust_email=$_POST['cust_email'];
   
    //starting of update
    $sql = "update customer_table set cust_name='$cust_name',cust_company='$cust_company', cust_mobile='$cust_mobile', cust_email='$cust_email' where customer_id=$userid";
    $result=mysqli_query($conn, $sql);
    if($result){
        //echo "Data updated succesfully";
       header('location: display_customer.php');
    }else{
        die(mysqli_error($conn));
    }
    
    //tuone
    /*
    if (!empty($password)){
        // Hash the new password using bcrypt
        //$hashedPassword = password_hash($password, PASSWORD_DEFAULT);

        // Update the user with the new password
        $sql = "update users set fname='$fname',mname='$mname', lname='$lname', email='$email', mobile='$mobile', password='$password', role='$role' where userid=$userid";
        $result=mysqli_query($conn, $sql);
        if($result){
            //echo "Data updated succesfully";
           header('location: display.php');
        }else{
            die(mysqli_error($conn));
        }
    } else {
        //update without changing the password
        $sql = "update users set fname='$fname',mname='$mname', lname='$lname', email='$email', mobile='$mobile', role='$role' where userid=$userid";
        $result=mysqli_query($conn, $sql);
        if($result){
            //echo "Data updated succesfully";
           header('location: display.php');
        }else{
            die(mysqli_error($conn));
        }
    }   */
            //tuone tena
            //Hii iliyokuwa commented hapa chini inafanya kazi lakini kama umeencrypt pwd inakuwa shida
        /* $sql = "update users set fname='$fname',mname='$mname', lname='$lname', email='$email', mobile='$mobile', password='$password', role='$role' where userid=$userid";
            //$sql = "insert into users (name, email, mobile, password) values ('$name','$email','$mobile','$password')";
            $result=mysqli_query($conn, $sql);
            if($result){
                //echo "Data updated succesfully";
            header('location: display.php');
            }else{
                die(mysqli_error($conn));
            } */
}

?>

<!doctype html>
<html lang="en" dir="ltr">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
<!--<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css">-->
<link rel="stylesheet" href="css/bootstrap.min.css">

    <title>CRUD APPLICATION</title>
  </head>
  <body>
    <div class="container my-5"> 
        <h3>Change the information filled in the textboxes to update the user</h3>

        <form method="POST">
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Customer Name:</label>
                <input type="text" class="form-control" name="cust_name" autocomplete="off" value=<?php echo $cust_name; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Company:</label>
                <input type="text" class="form-control" name="cust_company" autocomplete="off" value=<?php echo $cust_company; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Mobile No:</label>
                <input type="text" class="form-control" name="cust_mobile" autocomplete="off" value=<?php echo $cust_mobile; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">Email:</label>
                <input type="email" class="form-control" name="cust_email" autocomplete="off" value=<?php echo $cust_email; ?>>
            </div>
            
            <button type="submit" class="btn btn-primary" name="submit">Update</button>
        </form>    

    </div>

    <script src="js/jquery.min.js"></script>
<script src="js/bootstrap.bundle.min.js"></script>
  </body>
</html>