<?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'];

$message = "";

/* ===============================
SAVE 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("

INSERT INTO suppliers
(company_id,supplier_code,supplier_name,supplier_type,vat_number,contact_person,email,phone,address,city,country,status)

VALUES (?,?,?,?,?,?,?,?,?,?,?,?)

");

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

$message = "Supplier added successfully.";

}

?>

<div class="main-content">

<div class="page-header">
<h2>Add 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" required>
</div>

<div class="form-group">
<label>Supplier Name</label>
<input type="text" name="supplier_name" required>
</div>

</div>


<div class="form-row">

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

<option value="water">Water</option>
<option value="electricity">Electricity</option>
<option value="general">General</option>

</select>
</div>

<div class="form-group">
<label>VAT Number</label>
<input type="text" name="vat_number">
</div>

</div>


<div class="form-row">

<div class="form-group">
<label>Contact Person</label>
<input type="text" name="contact_person">
</div>

<div class="form-group">
<label>Email</label>
<input type="email" name="email">
</div>

</div>


<div class="form-row">

<div class="form-group">
<label>Phone</label>
<input type="text" name="phone">
</div>

<div class="form-group">
<label>City</label>
<input type="text" name="city">
</div>

</div>


<div class="form-group">
<label>Address</label>
<textarea name="address"></textarea>
</div>


<div class="form-row">

<div class="form-group">
<label>Country</label>
<input type="text" name="country">
</div>

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

<option value="active">Active</option>
<option value="inactive">Inactive</option>

</select>
</div>

</div>


<div class="form-actions">

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

<a href="index.php" class="btn btn-secondary">
Cancel
</a>

</div>

</form>

</div>

</div>

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