<?php
require_once "../config/auth.php";
require_once "../config/db.php";
require_once "../config/helpers.php";

require_once "../includes/header.php";
require_once "../includes/sidebar.php";
require_once "../includes/topbar.php";

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

$service = $conn->query("SELECT * FROM services WHERE id=$id")->fetch_assoc();
$customers = $conn->query("SELECT id,name FROM customers");

if(!$service){
    die("Service not found");
}
?>

<div class="main-content">
<div class="col-lg-12">

<div class="topbar">
    <h5>Edit Service</h5>
</div>

<div class="p-4">

<form method="POST" action="../actions/update_service.php">

<input type="hidden" name="id" value="<?= $service['id'] ?>">

<!-- Customer -->
<div class="mb-3">
<label>Customer</label>
<select name="customer_id" class="form-control" required>
<?php while($c = $customers->fetch_assoc()){ ?>
<option value="<?= $c['id'] ?>" 
<?= ($c['id']==$service['customer_id']) ? 'selected' : '' ?>>
<?= e($c['name']) ?>
</option>
<?php } ?>
</select>
</div>

<!-- Service Name -->
<div class="mb-3">
<label>Service Name *</label>
<input type="text" name="service_name" class="form-control"
value="<?= e($service['service_name']) ?>" required>
</div>

<!-- Type -->
<div class="mb-3">
<label>Type</label>
<select name="type" class="form-control">
<option value="domain" <?= ($service['type']=='domain')?'selected':'' ?>>Domain</option>
<option value="hosting" <?= ($service['type']=='hosting')?'selected':'' ?>>Hosting</option>
<option value="amc" <?= ($service['type']=='amc')?'selected':'' ?>>AMC</option>
<option value="other" <?= ($service['type']=='other')?'selected':'' ?>>Other</option>
</select>
</div>

<!-- Selling Price -->
<div class="mb-3">
<label>Selling Price</label>
<input type="number" name="amount" class="form-control"
value="<?= $service['amount'] ?>" step="0.01" required>
</div>

<!-- Cost -->
<div class="mb-3">
<label>Cost</label>
<input type="number" name="cost" class="form-control"
value="<?= $service['cost'] ?>" step="0.01">
</div>

<!-- Start Date -->
<div class="mb-3">
<label>Start Date</label>
<input type="date" name="start_date" class="form-control"
value="<?= $service['start_date'] ?>">
</div>

<!-- Renewal Date -->
<div class="mb-3">
<label>Renewal Date</label>
<input type="date" name="renewal_date" class="form-control"
value="<?= $service['renewal_date'] ?>">
</div>

<button class="btn btn-primary">Update Service</button>

</form>

</div>
</div>
</div>

<?php require_once "../includes/footer.php"; ?>
