<?php
include("../includes/db_connection.php");
include("../includes/auth.php");
include("partials/header.php");

$id = $_GET['id'] ?? '';

$name=$address=$city=$country=$phone=$fax=$email=$associate_id="";

if($id){
$stmt = $conn->prepare("SELECT * FROM clients WHERE id=?");
$stmt->bind_param("i",$id);
$stmt->execute();
$data = $stmt->get_result()->fetch_assoc();

extract($data);
}
?>

<div class="container py-4">
<div class="card shadow">
<div class="card-header bg-dark text-white">
<?= $id ? "Edit Client" : "Add Client" ?>
</div>

<div class="card-body">
<form method="POST" action="client_save.php">

<input type="hidden" name="id" value="<?= $id ?>">

<div class="row g-3">

<div class="col-md-6">
<label class="form-label">Client Name *</label>
<input type="text" name="name" class="form-control" value="<?= $name ?>" required>
</div>

<div class="col-md-6">
<label class="form-label">Associate *</label>
<select name="associate_id" class="form-select" required>
<option value="">Select Associate</option>
<?php
$associates = $conn->query("SELECT id,name FROM associates WHERE status='active'");
while($a=$associates->fetch_assoc()):
?>
<option value="<?= $a['id'] ?>" <?= ($associate_id==$a['id'])?'selected':'' ?>>
<?= $a['name'] ?>
</option>
<?php endwhile; ?>
</select>
</div>

<div class="col-md-6">
<label class="form-label">Email</label>
<input type="email" name="email" class="form-control" value="<?= $email ?>">
</div>

<div class="col-md-6">
<label class="form-label">Phone</label>
<input type="text" name="phone" class="form-control" value="<?= $phone ?>">
</div>

<div class="col-md-6">
<label class="form-label">City</label>
<input type="text" name="city" class="form-control" value="<?= $city ?>">
</div>

<div class="col-md-6">
<label class="form-label">Country *</label>
<select name="country" class="form-select" required>

<?php
$countries = [
"Afghanistan","Albania","Algeria","Andorra","Angola","Antigua and Barbuda",
"Argentina","Armenia","Australia","Austria","Azerbaijan","Bahamas","Bahrain",
"Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bhutan",
"Bolivia","Bosnia and Herzegovina","Botswana","Brazil","Brunei","Bulgaria",
"Burkina Faso","Burundi","Cambodia","Cameroon","Canada","Cape Verde",
"Central African Republic","Chad","Chile","China","Colombia","Comoros",
"Congo","Costa Rica","Croatia","Cuba","Cyprus","Czech Republic","Denmark",
"Djibouti","Dominica","Dominican Republic","Ecuador","Egypt","El Salvador",
"Equatorial Guinea","Eritrea","Estonia","Eswatini","Ethiopia","Fiji",
"Finland","France","Gabon","Gambia","Georgia","Germany","Ghana","Greece",
"Grenada","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Honduras",
"Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel",
"Italy","Jamaica","Japan","Jordan","Kazakhstan","Kenya","Kiribati",
"Korea North","Korea South","Kuwait","Kyrgyzstan","Laos","Latvia",
"Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania",
"Luxembourg","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta",
"Marshall Islands","Mauritania","Mauritius","Mexico","Micronesia",
"Moldova","Monaco","Mongolia","Montenegro","Morocco","Mozambique",
"Myanmar","Namibia","Nauru","Nepal","Netherlands","New Zealand",
"Nicaragua","Niger","Nigeria","North Macedonia","Norway","Oman",
"Pakistan","Palau","Panama","Papua New Guinea","Paraguay","Peru",
"Philippines","Poland","Portugal","Qatar","Romania","Russia","Rwanda",
"Saint Kitts and Nevis","Saint Lucia","Saint Vincent and the Grenadines",
"Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Senegal",
"Serbia","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia",
"Solomon Islands","Somalia","South Africa","Spain","Sri Lanka","Sudan",
"Suriname","Sweden","Switzerland","Syria","Taiwan","Tajikistan",
"Tanzania","Thailand","Togo","Tonga","Trinidad and Tobago","Tunisia",
"Turkey","Turkmenistan","Tuvalu","Uganda","Ukraine","United Arab Emirates",
"United Kingdom","United States","Uruguay","Uzbekistan","Vanuatu",
"Vatican City","Venezuela","Vietnam","Yemen","Zambia","Zimbabwe"
];

$current_country = $country ?: "India";

echo "<option value=''>Select Country</option>";

foreach($countries as $c){
    $selected = ($current_country == $c) ? "selected" : "";
    echo "<option value=\"$c\" $selected>$c</option>";
}
?>

</select>
</div>

<div class="col-12">
<label class="form-label">Address</label>
<textarea name="address" class="form-control" rows="3"><?= $address ?></textarea>
</div>

<div class="col-12 mt-3">
<button class="btn btn-success">
<?= $id ? "Update Client" : "Save Client" ?>
</button>
<a href="clients.php" class="btn btn-secondary">Back</a>
</div>

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

<?php include("partials/footer.php"); ?>