<?php
$page_title = 'Edit Data';
require_once('includes/load.php');

// Establish a database connection
$db = new MySqli_DB(DB_HOST, DB_USER, DB_PASS, DB_NAME);
page_require_level(1);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get the ID of the data to be edited from the URL
    $data_id = $_GET['id'];
    
    $part_number = $_POST['part_number'];
    $description = $_POST['description'];
    $color = $_POST['color'];
    $unit_price = $_POST['unit_price'];
    $unit_measurements = $_POST['unit_measurements'];

    $errors = [];

    // Validasi data
    if (empty($part_number)) {
        $errors[] = "Part Number is required.";
    }
    if (empty($description)) {
        $errors[] = "Description is required.";
    }
    // Lakukan validasi tambahan untuk atribut lainnya seperti description, color, unit_price, dll.

    if (empty($errors)) {
        // Lakukan proses update data ke database
        $result = update_data($data_id, $part_number, $description, $color, $unit_price, $unit_measurements, $db);
        if ($result) {
            $session->msg('s', "Data updated successfully.");
            redirect("edit_data.php?id={$data_id}", false);
        } else {
            $session->msg('d', "Failed to update data.");
            redirect("edit_data.php?id={$data_id}", false);
        }
    } else {
        $session->msg('d', implode("<br>", $errors));
        redirect("edit_data.php?id={$data_id}", false);
    }
}

// Get the ID of the data to be edited from the URL
$data_id = $_GET['id'];

// Retrieve the data to be edited from the database
$data = get_data_by_id($data_id, $db);

?>
<?php include_once('layouts/header.php'); ?>
<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Edit Data</h3>
            </div>
            <div class="panel-body">
                <form method="post" action="edit_data.php?id=<?php echo $data_id; ?>">
                    <div class="form-group">
                        <label for="part_number">Part Number</label>
                        <input type="text" class="form-control" name="part_number" value="<?php echo $data['part_number']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="description">Description</label>
                        <input type="text" class="form-control" name="description" value="<?php echo $data['description']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="color">Color</label>
                        <input type="text" class="form-control" name="color" value="<?php echo $data['color']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="unit_price">Unit Price</label>
                        <input type="text" class="form-control" name="unit_price" value="<?php echo $data['unit_price']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="unit_measurements">Unit of Measurement</label>
                        <input type="text" class="form-control" name="unit_measurements" value="<?php echo $data['unit_measurements']; ?>" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>
