<?php

$pageTitle =
    'Quản lý sản phẩm - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'products';


$successMessage =
    $_SESSION['flash_success']
    ?? null;

$errorMessage =
    $_SESSION['flash_error']
    ?? null;


unset(
    $_SESSION['flash_success'],
    $_SESSION['flash_error']
);


require BASE_PATH
    . '/app/views/layouts/header.php';

require BASE_PATH
    . '/app/views/layouts/navbar.php';

?>

<main class="owner-area">

    <div class="container-fluid">

        <div class="row g-4">

            <div class="col-xl-2 col-lg-3">

                <?php require BASE_PATH
                    . '/app/views/layouts/owner-sidebar.php';
                ?>

            </div>


            <div class="col-xl-10 col-lg-9">

                <div class="owner-page-header">

                    <div>

                        <h1>
                            Sản phẩm
                        </h1>

                        <p>

                            <?= number_format(
                                $totalProducts,
                                0,
                                ',',
                                '.'
                            ) ?>

                            sản phẩm.

                        </p>

                    </div>


                    <a
                        href="<?= BASE_URL ?>/?url=owner/product/create"
                        class="btn btn-primary"
                    >

                        <i class="bi bi-plus"></i>

                        Thêm sản phẩm

                    </a>

                </div>


                <?php if ($successMessage): ?>

                    <div class="alert alert-success">

                        <?= htmlspecialchars(
                            $successMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <?php if ($errorMessage): ?>

                    <div class="alert alert-danger">

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <!-- FILTER -->

                <section class="owner-card mb-4">

                    <form
                        method="GET"
                        class="row g-3"
                    >

                        <input
                            type="hidden"
                            name="url"
                            value="owner/products"
                        >


                        <div class="col-md-6">

                            <input
                                type="text"
                                name="search"
                                class="form-control"
                                placeholder="Tên, SKU hoặc thương hiệu..."
                                value="<?= htmlspecialchars(
                                    $search
                                ) ?>"
                            >

                        </div>


                        <div class="col-md-3">

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

                                <option value="">
                                    Tất cả trạng thái
                                </option>

                                <option
                                    value="active"
                                    <?= $status === 'active'
                                        ? 'selected'
                                        : ''
                                    ?>
                                >
                                    Đang bán
                                </option>

                                <option
                                    value="out_of_stock"
                                    <?= $status === 'out_of_stock'
                                        ? 'selected'
                                        : ''
                                    ?>
                                >
                                    Hết hàng
                                </option>

                                <option
                                    value="hidden"
                                    <?= $status === 'hidden'
                                        ? 'selected'
                                        : ''
                                    ?>
                                >
                                    Đã ẩn
                                </option>

                            </select>

                        </div>


                        <div class="col-md-3">

                            <button
                                class="btn btn-dark w-100"
                            >
                                Tìm kiếm
                            </button>

                        </div>

                    </form>

                </section>


                <!-- TABLE -->

                <section class="owner-card">

                    <div class="table-responsive">

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

                            <thead>

                                <tr>

                                    <th>Sản phẩm</th>
                                    <th>SKU</th>
                                    <th>Danh mục</th>
                                    <th>Giá</th>
                                    <th>Tồn kho</th>
                                    <th>Đã bán</th>
                                    <th>Trạng thái</th>
                                    <th></th>

                                </tr>

                            </thead>


                            <tbody>

                                <?php foreach (
                                    $products
                                    as $product
                                ): ?>

                                    <tr>

                                        <td>

                                            <div class="owner-product-cell">

                                                <div class="owner-product-image">

                                                    <?php if (
                                                        !empty(
                                                            $product[
                                                                'featured_image'
                                                            ]
                                                        )
                                                    ): ?>

                                                        <img
                                                            src="<?= BASE_URL ?>/<?= htmlspecialchars(
                                                                ltrim(
                                                                    $product[
                                                                        'featured_image'
                                                                    ],
                                                                    '/'
                                                                )
                                                            ) ?>"
                                                            alt=""
                                                        >

                                                    <?php else: ?>

                                                        🐾

                                                    <?php endif; ?>

                                                </div>


                                                <div>

                                                    <strong>

                                                        <?= htmlspecialchars(
                                                            $product['name']
                                                        ) ?>

                                                    </strong>


                                                    <?php if (
                                                        (int)
                                                        $product[
                                                            'is_featured'
                                                        ] === 1
                                                    ): ?>

                                                        <small
                                                            class="text-warning"
                                                        >
                                                            ★ Nổi bật
                                                        </small>

                                                    <?php endif; ?>

                                                </div>

                                            </div>

                                        </td>


                                        <td>

                                            <?= htmlspecialchars(
                                                $product['sku']
                                                ?? '-'
                                            ) ?>

                                        </td>


                                        <td>

                                            <?= htmlspecialchars(
                                                $product[
                                                    'category_name'
                                                ]
                                            ) ?>

                                        </td>


                                        <td>

                                            <?php if (
                                                $product[
                                                    'sale_price'
                                                ] !== null
                                            ): ?>

                                                <strong
                                                    class="text-danger"
                                                >

                                                    <?= number_format(
                                                        (float)
                                                        $product[
                                                            'sale_price'
                                                        ],
                                                        0,
                                                        ',',
                                                        '.'
                                                    ) ?>đ

                                                </strong>

                                            <?php else: ?>

                                                <?= number_format(
                                                    (float)
                                                    $product['price'],
                                                    0,
                                                    ',',
                                                    '.'
                                                ) ?>đ

                                            <?php endif; ?>

                                        </td>


                                        <td>

                                            <strong
                                                class="<?= (int)
                                                    $product[
                                                        'stock_quantity'
                                                    ] <= 5
                                                    ? 'text-danger'
                                                    : ''
                                                ?>"
                                            >

                                                <?= (int)
                                                    $product[
                                                        'stock_quantity'
                                                    ]
                                                ?>

                                            </strong>

                                        </td>


                                        <td>

                                            <?= (int)
                                                $product[
                                                    'sold_quantity'
                                                ]
                                            ?>

                                        </td>


                                        <td>

                                            <?php

                                            echo match (
                                                $product['status']
                                            ) {

                                                'active' =>
                                                    '<span class="badge bg-success">Đang bán</span>',

                                                'out_of_stock' =>
                                                    '<span class="badge bg-warning text-dark">Hết hàng</span>',

                                                default =>
                                                    '<span class="badge bg-secondary">Đã ẩn</span>'
                                            };

                                            ?>

                                        </td>


                                        <td>

                                            <div class="dropdown">

                                                <button
                                                    class="btn btn-sm btn-light"
                                                    data-bs-toggle="dropdown"
                                                >
                                                    ⋮
                                                </button>


                                                <ul
                                                    class="dropdown-menu dropdown-menu-end"
                                                >

                                                    <li>

                                                        <a
                                                            class="dropdown-item"
                                                            href="<?= BASE_URL ?>/?url=owner/product/edit&id=<?= (int)
                                                                $product['id']
                                                            ?>"
                                                        >
                                                            Sửa
                                                        </a>

                                                    </li>


                                                    <li>

                                                        <a
                                                            class="dropdown-item"
                                                            target="_blank"
                                                            href="<?= BASE_URL ?>/?url=product&slug=<?= urlencode(
                                                                $product['slug']
                                                            ) ?>"
                                                        >
                                                            Xem ngoài Shop
                                                        </a>

                                                    </li>


                                                    <?php if (
                                                        $product['status']
                                                        !== 'hidden'
                                                    ): ?>

                                                        <li>
                                                            <hr class="dropdown-divider">
                                                        </li>

                                                        <li>

                                                            <form
                                                                method="POST"
                                                                action="<?= BASE_URL ?>/?url=owner/product/hide"
                                                            >

                                                                <input
                                                                    type="hidden"
                                                                    name="_token"
                                                                    value="<?= htmlspecialchars(
                                                                        $_SESSION[
                                                                            'csrf_token'
                                                                        ]
                                                                    ) ?>"
                                                                >

                                                                <input
                                                                    type="hidden"
                                                                    name="product_id"
                                                                    value="<?= (int)
                                                                        $product['id']
                                                                    ?>"
                                                                >

                                                                <button
                                                                    class="dropdown-item text-danger"
                                                                    type="submit"
                                                                >
                                                                    Ẩn sản phẩm
                                                                </button>

                                                            </form>

                                                        </li>

                                                    <?php endif; ?>

                                                </ul>

                                            </div>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>


                                <?php if (
                                    empty($products)
                                ): ?>

                                    <tr>

                                        <td
                                            colspan="8"
                                            class="text-center text-muted py-5"
                                        >
                                            Không tìm thấy sản phẩm.
                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </section>

            </div>

        </div>

    </div>

</main>


<?php

require BASE_PATH
    . '/app/views/layouts/footer.php';

?>