<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Изменение продажи</title>
</head>
<body>

<?php
include 'db_connection.php';

if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["invoice_number"])) {
    $invoice_number = $_GET["invoice_number"];
    $sql_get_sale = "SELECT * FROM sales WHERE invoice_number = '$invoice_number'";
    $result_get_sale = $conn->query($sql_get_sale);

    if ($result_get_sale->num_rows > 0) {
        $row = $result_get_sale->fetch_assoc();
?>

    <h2>Изменение продажи</h2>

    <form method="post" action="update_sale.php">
        <input type="hidden" name="invoice_number" value="<?php echo $row["invoice_number"]; ?>">
        
        <label for="buyer">Покупатель:</label>
        <input type="text" name="buyer" value="<?php echo $row["buyer"]; ?>" required><br>

        <label for="sale_date">Дата продажи:</label>
        <input type="date" name="sale_date" value="<?php echo $row["sale_date"]; ?>" required><br>

        <label for="total_weight">Общий вес (кг):</label>
        <input type="text" name="total_weight" value="<?php echo $row["total_weight"]; ?>" required><br>

        <label for="sale_amount">Сумма закупки:</label>
        <input type="text" name="sale_amount" value="<?php echo $row["sale_amount"]; ?>" required><br>
        
         <label for="sale_amount">Сумма продажи:</label>
        <input type="text" name="sale_amount2" value="<?php echo $row["sale_amount"]; ?>" required><br>

        <label for="profit_percentage">Процент прибыли:</label>
        <input type="text" name="profit_percentage" value="<?php echo $row["profit_percentage"]; ?>" required><br>

        <input type="submit" name="update_sale" value="Обновить">
    </form>

<?php
    } else {
        echo "<p>Продажа не найдена</p>";
    }
} else {
    echo "<p>Некорректные данные запроса</p>";
}

$conn->close();
?>

</body>
</html>
