<?php
session_start();

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

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

$success = "";
$error = "";

if (isset($_GET['success'])) {

    switch ($_GET['success']) {

        case "added":
            $success = "Supplier added successfully.";
            break;

        case "updated":
            $success = "Supplier updated successfully.";
            break;

        case "deleted":
            $success = "Supplier deleted successfully.";
            break;
    }
}

if (isset($_GET['error'])) {

    switch ($_GET['error']) {

        case "duplicate":
            $error = "Supplier already exists.";
            break;

        case "delete":
            $error = "Unable to delete supplier.";
            break;

        default:
            $error = "Something went wrong.";
            break;
    }
}

$search = "";

if (isset($_GET['search'])) {
    $search = trim($_GET['search']);
}

$limit = 10;

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

if ($page < 1) {
    $page = 1;
}

$offset = ($page - 1) * $limit;

$where = "";

if ($search != "") {

    $search_value = mysqli_real_escape_string($conn, $search);

    $where = " WHERE
        supplier_name LIKE '%$search_value%'
        OR contact_person LIKE '%$search_value%'
        OR phone LIKE '%$search_value%'
        OR email LIKE '%$search_value%'";
}

$count_query = mysqli_query(
    $conn,
    "SELECT COUNT(*) AS total FROM suppliers $where"
);

$count_row = mysqli_fetch_assoc($count_query);

$total_records = $count_row['total'];

$total_pages = ceil($total_records / $limit);

$query = mysqli_query(
    $conn,
    "SELECT *
    FROM suppliers
    $where
    ORDER BY id DESC
    LIMIT $offset,$limit"
);
?>

<?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 gap-3 mb-4">

        <div>
            <h3 class="fw-bold mb-1">Suppliers</h3>
            <p class="text-muted mb-0">
                Manage supplier records
            </p>
        </div>

        <a href="/beauty_pos/suppliers/add.php"
           class="btn btn-primary rounded-3">

            <i class="fas fa-plus me-2"></i>
            Add Supplier

        </a>

    </div>

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

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

            <?php echo $success; ?>

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

        </div>

    <?php } ?>

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

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

            <?php echo $error; ?>

            <button
                type="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="GET">

                <div class="row g-3 align-items-center mb-4">

                    <div class="col-xl-4 col-lg-5 col-md-8 col-12">

                        <input
                            type="text"
                            name="search"
                            class="form-control"
                            placeholder="Search Supplier..."
                            value="<?php echo htmlspecialchars($search); ?>">

                    </div>

                    <div class="col-auto">

                        <button
                            class="btn btn-primary">

                            <i class="fas fa-search me-1"></i>
                            Search

                        </button>

                    </div>

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

                    <div class="col-auto">

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

                        Reset

                        </a>

                    </div>

                    <?php } ?>

                </div>

            </form>

            <div class="table-responsive">

                <table class="table table-hover align-middle">

                    <thead class="table-light">

                        <tr>

                            <th width="25%">Supplier</th>

                            <th width="25%">Contact</th>

                            <th width="12%">Rating</th>

                            <th width="15%">Outstanding</th>

                            <th width="15%">Created</th>

                            <th width="8%" class="text-center">
                                Actions
                            </th>

                        </tr>

                    </thead>

                    <tbody>

<?php

if(mysqli_num_rows($query)>0){

while($row=mysqli_fetch_assoc($query)){

?>
                        <tr>

                            <td>

                                <div class="fw-semibold">
                                    <?php echo htmlspecialchars($row['supplier_name']); ?>
                                </div>

                            </td>

                            <td>

                                <?php if(!empty($row['contact_person'])){ ?>

                                    <div class="fw-semibold">
                                        <?php echo htmlspecialchars($row['contact_person']); ?>
                                    </div>

                                <?php } ?>

                                <?php if(!empty($row['phone'])){ ?>

                                    <div class="text-muted small">
                                        <?php echo htmlspecialchars($row['phone']); ?>
                                    </div>

                                <?php } ?>

                                <?php if(!empty($row['email'])){ ?>

                                    <div class="text-muted small">
                                        <?php echo htmlspecialchars($row['email']); ?>
                                    </div>

                                <?php } ?>

                            </td>

                            <td>

                                <?php

                                $rating = (int)$row['rating'];

                                if($rating < 0){
                                    $rating = 0;
                                }

                                if($rating > 5){
                                    $rating = 5;
                                }

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

                                    if($i <= $rating){

                                        echo '<i class="fas fa-star text-warning me-1"></i>';

                                    }else{

                                        echo '<i class="far fa-star text-warning me-1"></i>';

                                    }

                                }

                                ?>

                            </td>

                            <td>

                                <?php

                                if((float)$row['outstanding'] <= 0){

                                    ?>

                                    <span class="badge bg-success rounded-pill px-3 py-2">
                                        Clear
                                    </span>

                                    <?php

                                }else{

                                    ?>

                                    <span class="badge bg-danger rounded-pill px-3 py-2">

                                        ₹<?php echo number_format($row['outstanding'],2); ?>

                                    </span>

                                    <?php

                                }

                                ?>

                            </td>

                            <td>

                                <?php

                                echo date(
                                    "d M Y",
                                    strtotime($row['created_at'])
                                );

                                ?>

                            </td>

                            <td class="text-center">

                                <a
                                href="/beauty_pos/suppliers/edit.php?id=<?php echo $row['id']; ?>"
                                class="btn btn-sm btn-warning mb-1">

                                    <i class="fas fa-edit"></i>

                                </a>

                                <a
                                href="/beauty_pos/suppliers/delete.php?id=<?php echo $row['id']; ?>"
                                class="btn btn-sm btn-danger mb-1"
                                onclick="return confirm('Delete this supplier?');">

                                    <i class="fas fa-trash"></i>

                                </a>

                            </td>

                        </tr>

<?php

}

}else{

?>

<tr>

    <td colspan="6" class="text-center py-5 text-muted">

        No suppliers found.

    </td>

</tr>

<?php

}

?>

                    </tbody>

                </table>

            </div>

<?php

if($total_pages > 1){

?>

<nav class="mt-4">

<ul class="pagination justify-content-end flex-wrap">

<?php

if($page > 1){

?>

<li class="page-item">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $page-1; ?>">

Previous

</a>

</li>

<?php

}

for($i=1;$i<=$total_pages;$i++){

?>

<li class="page-item <?php if($i==$page) echo 'active'; ?>">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $i; ?>">

<?php echo $i; ?>

</a>

</li>

<?php

}

if($page < $total_pages){

?>

<li class="page-item">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $page+1; ?>">

Next

</a>

</li>

<?php

}

?>

</ul>

</nav>

<?php

}

?>

        </div>

    </div>

</div>

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

</div>