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

$id = $_GET['id'] ?? '';
$name = $org_name = $email = $phone = $country = $pin_zip = $address = $tax_id = $status = 
"";

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

    $name = $data['name'];
    $org_name = $data['org_name'];
    $email = $data['email'];
    $phone = $data['phone'];
    $country = $data['country'];
    $pin_zip = $data['pin_zip'];
    $address = $data['address'];
    $tax_id = $data['tax_id'];
    $status = $data['status'];
}
?>

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

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

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

<div class="row g-3">

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

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

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

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

<div class="col-md-6">
<label>Tax / GST (Optional)</label>
<input type="text" name="tax_id" class="form-control" value="<?= $tax_id ?>">
</div>

<div class="col-md-6">
<label>Status</label>
<select name="status" class="form-select">
<option value="under_review" <?= $status=="under_review"?'selected':'' ?>>Under 
Review</option>
<option value="active" <?= $status=="active"?'selected':'' ?>>Active</option>
<option value="inactive" <?= $status=="inactive"?'selected':'' ?>>Inactive</option>
<option value="rejected" <?= $status=="rejected"?'selected':'' ?>>Rejected</option>
</select>
</div>

<div class="col-md-6">
<label>Country</label>
<select name="country" class="form-select" required>
<option value="<?= $country ?>"><?= $country ?></option>

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

</select>
</div>

<div class="col-md-6">
<label>Pin / Zip</label>
<input type="text" name="pin_zip" class="form-control" value="<?= $pin_zip ?>" required>
</div>

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

<div class="col-12">
<label>Password (Leave blank to keep unchanged)</label>
<input type="password" name="password" class="form-control">
</div>

<div class="col-12">
<button class="btn btn-success">
<?= $id ? "Update Associate" : "Create Associate" ?>
</button>
<a href="associates.php" class="btn btn-secondary">Back</a>
</div>

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

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