<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

require_once __DIR__.'/../../includes/auth.php';
requireLogin();

require_once __DIR__.'/../../config/database.php';

require_once __DIR__.'/../../includes/header.php';
require_once __DIR__.'/../../includes/sidebar.php';

$company_id = $_SESSION['company_id'];

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

/* ===============================
FETCH SUPPLIER
================================ */

$stmt = $pdo->prepare("
SELECT *
FROM suppliers
WHERE id = ?
AND company_id = ?
");

$stmt->execute([$id,$company_id]);
$supplier = $stmt->fetch();

if(!$supplier){

echo "<div class='main-content'><div class='stat-card'>Supplier not found.</div></div>";
require_once __DIR__.'/../../includes/footer.php';
exit;

}

$message = "";

/* ===============================
UPDATE SUPPLIER
================================ */

if($_SERVER['REQUEST_METHOD'] == 'POST'){

$supplier_code = $_POST['supplier_code'];
$supplier_name = $_POST['supplier_name'];
$supplier_type = $_POST['supplier_type'];
$vat_number = $_POST['vat_number'];
$contact_person = $_POST['contact_person'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$city = $_POST['city'];
$country = $_POST['country'];
$status = $_POST['status'];

$stmt = $pdo->prepare("

UPDATE suppliers
SET
supplier_code=?,
supplier_name=?,
supplier_type=?,
vat_number=?,
contact_person=?,
email=?,
phone=?,
address=?,
city=?,
country=?,
status=?

WHERE id=?
AND company_id=?

");

$stmt->execute([
$supplier_code,
$supplier_name,
$supplier_type,
$vat_number,
$contact_person,
$email,
$phone,
$address,
$city,
$country,
$status,
$id,
$company_id
]);

$message = "Supplier updated successfully.";

/* reload supplier */

$stmt = $pdo->prepare("
SELECT *
FROM suppliers
WHERE id = ?
AND company_id = ?
");

$stmt->execute([$id,$company_id]);
$supplier = $stmt->fetch();

}

?>

<div class="main-content">

<div class="page-header">
<h2>Edit Supplier</h2>
</div>

<div class="stat-card">

<?php if($message){ ?>

<div class="alert alert-success">
<?php echo $message; ?>
</div>

<?php } ?>

<form method="POST">

<div class="form-row">

<div class="form-group">
<label>Supplier Code</label>
<input type="text" name="supplier_code" value="<?php echo htmlspecialchars($supplier['supplier_code']); ?>">
</div>

<div class="form-group">
<label>Supplier Name</label>
<input type="text" name="supplier_name" value="<?php echo htmlspecialchars($supplier['supplier_name']); ?>">
</div>

</div>

<div class="form-row">

<div class="form-group">
<label>Supplier Type</label>
<select name="supplier_type">

<option value="water" <?php if($supplier['supplier_type']=='water') echo 'selected'; ?>>Water</option>

<option value="electricity" <?php if($supplier['supplier_type']=='electricity') echo 'selected'; ?>>Electricity</option>

<option value="general" <?php if($supplier['supplier_type']=='general') echo 'selected'; ?>>General</option>

</select>
</div>

<div class="form-group">
<label>VAT Number</label>
<input type="text" name="vat_number" value="<?php echo htmlspecialchars($supplier['vat_number']); ?>">
</div>

</div>

<div class="form-row">

<div class="form-group">
<label>Contact Person</label>
<input type="text" name="contact_person" value="<?php echo htmlspecialchars($supplier['contact_person']); ?>">
</div>

<div class="form-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo htmlspecialchars($supplier['email']); ?>">
</div>

</div>

<div class="form-row">

<div class="form-group">
<label>Phone</label>
<input type="text" name="phone" value="<?php echo htmlspecialchars($supplier['phone']); ?>">
</div>

<div class="form-group">
<label>City</label>
<input type="text" name="city" value="<?php echo htmlspecialchars($supplier['city']); ?>">
</div>

</div>

<div class="form-group">
<label>Address</label>
<textarea name="address"><?php echo htmlspecialchars($supplier['address']); ?></textarea>
</div>

<div class="form-row">

<div class="form-group">
<label>Country</label>
<input type="text" name="country" value="<?php echo htmlspecialchars($supplier['country']); ?>">
</div>

<div class="form-group">
<label>Status</label>
<select name="status">

<option value="active" <?php if($supplier['status']=='active') echo 'selected'; ?>>Active</option>

<option value="inactive" <?php if($supplier['status']=='inactive') echo 'selected'; ?>>Inactive</option>

</select>
</div>

</div>

<div class="form-actions">

<button type="submit" class="btn btn-primary">
Update Supplier
</button>

<a href="view.php?id=<?php echo $supplier['id']; ?>" class="btn btn-secondary">
Cancel
</a>

</div>

</form>

</div>

</div>

<?php require_once __DIR__.'/../../includes/footer.php'; ?>