<?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 supplier_table where sup_id=$userid";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_assoc($result);
$sup_name=$row['sup_name'];
$sup_company=$row['sup_company'];
$sup_mobile=$row['sup_mobile'];
$sup_email=$row['sup_email'];
$product_desc=$row['product_desc'];

if(isset($_POST['submit'])){
    $sup_name=$_POST['sup_name'];
    $sup_company=$_POST['sup_company'];
    $sup_mobile=$_POST['sup_mobile'];
    $sup_email=$_POST['sup_email'];
    $product_desc=$_POST['product_desc'];
   
    //starting of update
    $sql = "update supplier_table set sup_name='$sup_name',sup_company='$sup_company', sup_mobile='$sup_mobile', sup_email='$sup_email', product_desc ='$product_desc' where sup_id=$userid";
    $result=mysqli_query($conn, $sql);
    if($result){
        //echo "Data updated succesfully";
       header('location: display_supplier.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">Supplier Name:</label>
                <input type="text" class="form-control" name="sup_name" autocomplete="off" value=<?php echo $sup_name; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Company:</label>
                <input type="text" class="form-control" name="sup_company" autocomplete="off" value=<?php echo $sup_company; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput" class="form-label">Mobile No:</label>
                <input type="text" class="form-control" name="sup_mobile" autocomplete="off" value=<?php echo $sup_mobile; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">Email:</label>
                <input type="email" class="form-control" name="sup_email" autocomplete="off" value=<?php echo $sup_email; ?>>
            </div>
            <div class="mb-3">
                <label for="formGroupExampleInput2" class="form-label">Description:</label>
                <textarea name="product_desc" class="form-control" rows="2" cols="50" autocomplete="off" > <?php echo $product_desc; ?> </textarea>
            </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>