<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once __DIR__ . "/../config/database.php";

$error = "";

$supplier_name = "";
$contact_person = "";
$phone = "";
$email = "";
$address = "";
$rating = 5;
$outstanding = "0.00";

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

    $supplier_name  = trim($_POST['supplier_name']);
    $contact_person = trim($_POST['contact_person']);
    $phone          = trim($_POST['phone']);
    $email          = trim($_POST['email']);
    $address        = trim($_POST['address']);
    $rating         = (int)$_POST['rating'];
    $outstanding    = trim($_POST['outstanding']);

    if ($supplier_name == "") {

        $error = "Supplier name is required.";

    } else {

        $check = mysqli_query(
            $conn,
            "SELECT id
            FROM suppliers
            WHERE supplier_name='" . mysqli_real_escape_string($conn, $supplier_name) . "'"
        );

        if (mysqli_num_rows($check) > 0) {

            $error = "Supplier already exists.";

        } else {

            $supplier_name  = mysqli_real_escape_string($conn, $supplier_name);
            $contact_person = mysqli_real_escape_string($conn, $contact_person);
            $phone          = mysqli_real_escape_string($conn, $phone);
            $email          = mysqli_real_escape_string($conn, $email);
            $address        = mysqli_real_escape_string($conn, $address);
            $rating         = max(1, min(5, $rating));
            $outstanding    = ($outstanding == "") ? "0.00" : $outstanding;

            $insert = mysqli_query(
                $conn,
                "INSERT INTO suppliers
                (
                    supplier_name,
                    contact_person,
                    phone,
                    email,
                    address,
                    rating,
                    outstanding
                )
                VALUES
                (
                    '$supplier_name',
                    '$contact_person',
                    '$phone',
                    '$email',
                    '$address',
                    '$rating',
                    '$outstanding'
                )"
            );

            if ($insert) {

                header("Location: /beauty_pos/suppliers/index.php?success=added");
                exit;

            } else {

                $error = "Unable to save supplier.";

            }
        }
    }
}
?>

<?php include_once __DIR__ . "/../includes/header.php"; ?>
<?php include_once __DIR__ . "/../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once __DIR__ . "/../includes/topbar.php"; ?>

<div class="container-fluid py-4">

    <div class="d-flex justify-content-between align-items-center flex-wrap mb-4">

        <h3 class="fw-bold mb-0">
            Add Supplier
        </h3>

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

            <i class="fas fa-arrow-left me-2"></i>
            Back

        </a>

    </div>

    <?php if($error!=""){ ?>

        <div class="alert alert-danger alert-dismissible fade show">

            <?php echo $error; ?>

            <button
                class="btn-close"
                data-bs-dismiss="alert">
            </button>

        </div>

    <?php } ?>

    <div class="card rounded-4 shadow border-0">

        <div class="card-body">

            <form method="POST">

                <div class="row g-3">

                    <div class="col-xl-6 col-lg-6 col-md-6 col-12">

                        <label class="form-label">
                            Supplier Name
                        </label>

                        <input
                            type="text"
                            name="supplier_name"
                            class="form-control"
                            maxlength="150"
                            required
                            value="<?php echo htmlspecialchars($supplier_name); ?>">

                    </div>

                    <div class="col-xl-6 col-lg-6 col-md-6 col-12">

                        <label class="form-label">
                            Contact Person
                        </label>

                        <input
                            type="text"
                            name="contact_person"
                            class="form-control"
                            maxlength="150"
                            value="<?php echo htmlspecialchars($contact_person); ?>">

                    </div>

                    <div class="col-xl-6 col-lg-6 col-md-6 col-12">

                        <label class="form-label">
                            Phone
                        </label>

                        <input
                            type="text"
                            name="phone"
                            class="form-control"
                            maxlength="20"
                            value="<?php echo htmlspecialchars($phone); ?>">

                    </div>

                    <div class="col-xl-6 col-lg-6 col-md-6 col-12">

                        <label class="form-label">
                            Email
                        </label>

                        <input
                            type="email"
                            name="email"
                            class="form-control"
                            maxlength="150"
                            value="<?php echo htmlspecialchars($email); ?>">

                    </div>
                                        <div class="col-12">

                        <label class="form-label">
                            Address
                        </label>

                        <textarea
                            name="address"
                            class="form-control"
                            rows="4"><?php echo htmlspecialchars($address); ?></textarea>

                    </div>

                    <div class="col-xl-6 col-lg-6 col-md-6 col-12">

                        <label class="form-label">
                            Rating
                        </label>

                        <select
                            name="rating"
                            class="form-select">

                            <?php
                            for($i=1;$i<=5;$i++){
                            ?>

                            <option
                                value="<?php echo $i; ?>"
                                <?php if($rating==$i) echo "selected"; ?>>

                                <?php echo $i; ?> Star<?php if($i>1) echo "s"; ?>

                            </option>

                            <?php
                            }
                            ?>

                        </select>

                    </div>

                    <div class="col-xl-6 col-lg-6 col-md-6 col-12">

                        <label class="form-label">
                            Outstanding Amount
                        </label>

                        <input
                            type="number"
                            step="0.01"
                            min="0"
                            name="outstanding"
                            class="form-control"
                            value="<?php echo htmlspecialchars($outstanding); ?>">

                    </div>

                    <div class="col-12">

                        <div class="d-flex flex-wrap gap-2 mt-3">

                            <button
                                type="submit"
                                class="btn btn-primary">

                                <i class="fas fa-save me-2"></i>
                                Save Supplier

                            </button>

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

                                Cancel

                            </a>

                        </div>

                    </div>

                </div>

            </form>

        </div>

    </div>

</div>

<?php include_once __DIR__ . "/../includes/footer.php"; ?>

</div>