<?php

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../app/helpers.php';
require_once __DIR__ . '/../app/shop-products.php';

$user = sparrow_require_backyard_login();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    $product_id = (int) ($_POST['product_id'] ?? 0);

    if ($product_id > 0 && $action === 'hide_product') {
        sparrow_set_shop_product_active($product_id, false);

        header('Location: ' . sparrow_url('/backyard/products.php?notice=hidden'));
        exit;
    }

    if ($product_id > 0 && $action === 'show_product') {
        sparrow_set_shop_product_active($product_id, true);

        header('Location: ' . sparrow_url('/backyard/products.php?notice=shown'));
        exit;
    }
}

$products = sparrow_get_shop_products(true);

$page_css = <<<'CSS'
.products-table-wrap {
    overflow-x: auto;
}

.products-table {
    width: 100%;
    border-collapse: collapse;
}

.products-table th,
.products-table td {
    padding: 9px 8px;
    border-bottom: 1px solid var(--sparrow-border, #ead2df);
    text-align: left;
    vertical-align: middle;
    font-size: 13px;
}

.products-table th {
    color: var(--sparrow-purple-dark, #7d3f68);
    background: #fdf0f7;
    font-weight: bold;
    white-space: nowrap;
}

.product-thumb,
.products-table img.product-thumb,
.product-thumb-placeholder {
    width: 52px !important;
    height: 52px !important;
    max-width: 52px !important;
    max-height: 52px !important;
    min-width: 52px;
    min-height: 52px;
    border-radius: 10px;
    border: 1px solid var(--sparrow-border, #ead2df);
    object-fit: cover;
    background: #fdf0f7;
    display: block;
}

.products-table td:first-child {
    width: 64px;
}

.product-thumb-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #8a7080;
    font-size: 11px;
    text-align: center;
    padding: 6px;
}

.product-name-link {
    color: var(--sparrow-purple-dark, #7d3f68);
    font-weight: bold;
    text-decoration: none;
}

.product-name-link:hover {
    text-decoration: underline;
}

.product-small {
    color: var(--sparrow-muted, #8a7080);
    font-size: 12px;
    margin-top: 3px;
}

.product-pill-row {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.product-pill {
    display: inline-flex;
    align-items: center;
    padding: 4px 8px;
    border-radius: 999px;
    border: 1px solid var(--sparrow-border, #ead2df);
    background: #fff;
    color: var(--sparrow-purple-dark, #7d3f68);
    font-size: 12px;
    font-weight: bold;
    white-space: nowrap;
}

.product-pill.is-hidden {
    background: #f4e7ec;
    color: #9b5c75;
}

.product-pill.is-featured {
    background: #fdf0f7;
}

.product-tags {
    max-width: 240px;
    color: var(--sparrow-muted, #8a7080);
    font-size: 12px;
    line-height: 1.35;
}

.products-top-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

@media (max-width: 850px) {
    .products-table th,
    .products-table td {
        font-size: 12px;
        padding: 8px 7px;
    }

    .product-thumb,
    .product-thumb-placeholder {
        width: 48px;
        height: 48px;
    }
}

.products-table td img {
    width: 52px !important;
    height: 52px !important;
    max-width: 52px !important;
    max-height: 52px !important;
    object-fit: cover !important;
}
CSS;

sparrow_backyard_header('Products');
?>

<?php if (($_GET['notice'] ?? '') === 'deleted'): ?>
    <div class="notice-success">
        Product deleted.
    </div>
<?php endif; ?>

<style>
.products-table-wrap {
    overflow-x: auto;
}

.products-table {
    width: 100%;
    border-collapse: collapse;
}

.products-table th,
.products-table td {
    padding: 9px 8px;
    border-bottom: 1px solid var(--sparrow-border, #ead2df);
    text-align: left;
    vertical-align: middle;
    font-size: 13px;
}

.products-table th {
    color: var(--sparrow-purple-dark, #7d3f68);
    background: #fdf0f7;
    font-weight: bold;
    white-space: nowrap;
}

.products-table td.product-image-cell {
    width: 70px;
}

.products-table td.product-image-cell img.product-thumb,
.product-thumb {
    width: 52px !important;
    height: 52px !important;
    max-width: 52px !important;
    max-height: 52px !important;
    min-width: 52px !important;
    min-height: 52px !important;
    border-radius: 10px;
    border: 1px solid var(--sparrow-border, #ead2df);
    object-fit: cover !important;
    background: #fdf0f7;
    display: block;
}

.product-thumb-placeholder {
    width: 52px;
    height: 52px;
    border-radius: 10px;
    border: 1px solid var(--sparrow-border, #ead2df);
    background: #fdf0f7;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #8a7080;
    font-size: 10px;
    text-align: center;
    padding: 5px;
}

.product-name-link {
    color: var(--sparrow-purple-dark, #7d3f68);
    font-weight: bold;
    text-decoration: none;
}

.product-name-link:hover {
    text-decoration: underline;
}

.product-small {
    color: var(--sparrow-muted, #8a7080);
    font-size: 12px;
    margin-top: 3px;
}

.product-pill-row {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.product-pill {
    display: inline-flex;
    align-items: center;
    padding: 4px 8px;
    border-radius: 999px;
    border: 1px solid var(--sparrow-border, #ead2df);
    background: #fff;
    color: var(--sparrow-purple-dark, #7d3f68);
    font-size: 12px;
    font-weight: bold;
    white-space: nowrap;
}

.product-pill.is-hidden {
    background: #f4e7ec;
    color: #9b5c75;
}

.product-pill.is-featured {
    background: #fdf0f7;
}

.product-tags {
    max-width: 240px;
    color: var(--sparrow-muted, #8a7080);
    font-size: 12px;
    line-height: 1.35;
}
</style>

<?php if (($_GET['notice'] ?? '') === 'hidden'): ?>
    <div class="notice-success">
        Product hidden from the public shop.
    </div>
<?php endif; ?>

<?php if (($_GET['notice'] ?? '') === 'shown'): ?>
    <div class="notice-success">
        Product is showing in the public shop.
    </div>
<?php endif; ?>

<section class="card">
    <div class="page-heading-row">
        <div>
            <h1>Products</h1>
            <p class="muted">
                Manage premade products for the public shop.
            </p>
        </div>

        <div class="heading-actions products-top-actions">
            <a
                class="button button-secondary"
                href="<?php echo sparrow_escape(sparrow_url('/shop.php')); ?>"
                target="_blank"
            >
                View Shop
            </a>

            <a
                class="button"
                href="<?php echo sparrow_escape(sparrow_url('/backyard/product-new.php')); ?>"
            >
                Add Product
            </a>
        </div>
    </div>
</section>

<section class="card">
    <h2>Product List</h2>

    <?php if (!$products): ?>
        <p class="muted">No products yet.</p>
    <?php else: ?>
        <div class="products-table-wrap">
            <table class="products-table">
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Name</th>
                        <th>Stock</th>
                        <th>Price</th>
                        <th>Category</th>
                        <th>Tags</th>
                        <th>Date</th>
                        <th>Status</th>
						<th>Actions</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($products as $product): ?>
                        <?php
                            $image_src = '';

                            if (!empty($product['image_path'])) {
                                $image_src = sparrow_url($product['image_path']);
                            } elseif (!empty($product['source_image_urls'])) {
                                $image_urls = explode(',', (string) $product['source_image_urls']);
                                $image_src = trim($image_urls[0] ?? '');
                            }

                            $date_text = '';

                            if (!empty($product['created_at'])) {
                                $date_text = date('M j, Y', strtotime($product['created_at']));
                            }
                        ?>

                        <tr>
                            <td class="product-image-cell">
								<?php if ($image_src !== ''): ?>
                                    <img
                                        class="product-thumb"
                                        src="<?php echo sparrow_escape($image_src); ?>"
                                        alt="<?php echo sparrow_escape($product['product_name']); ?>"
                                    >
                                <?php else: ?>
                                    <div class="product-thumb-placeholder">
                                        No image
                                    </div>
                                <?php endif; ?>
                            </td>

                            <td>
                                <a
                                    class="product-name-link"
                                    href="<?php echo sparrow_escape(sparrow_url('/backyard/product.php?id=' . (int) $product['id'])); ?>"
                                >
                                    <?php echo sparrow_escape($product['product_name']); ?>
                                </a>
                            </td>

                            <td>
                                <?php echo (int) ($product['quantity_available'] ?? 0); ?>
                            </td>

                            <td>
                                $<?php echo sparrow_escape(number_format((float) ($product['price'] ?? 0), 2)); ?>
                            </td>

                            <td>
                                <?php echo sparrow_escape($product['category'] ?? ''); ?>
                            </td>

                            <td>
                                <div class="product-tags">
                                    <?php echo sparrow_escape($product['meta_keywords'] ?? ''); ?>
                                </div>
                            </td>

                            <td>
                                <?php echo sparrow_escape($date_text); ?>
                            </td>

                            <td>
                                <div class="product-pill-row">
                                    <?php if (!empty($product['is_active'])): ?>
                                        <span class="product-pill">Showing</span>
                                    <?php else: ?>
                                        <span class="product-pill is-hidden">Hidden/Sold</span>
                                    <?php endif; ?>

                                    <?php if (!empty($product['is_featured'])): ?>
                                        <span class="product-pill is-featured">Featured</span>
                                    <?php endif; ?>
                                </div>
                            </td>
							
							<td>
    <?php if (!empty($product['is_active'])): ?>
        <form
            method="post"
            onsubmit="return confirm('Hide this product from the public shop?');"
        >
            <input type="hidden" name="action" value="hide_product">
            <input type="hidden" name="product_id" value="<?php echo (int) $product['id']; ?>">

            <button type="submit" class="button button-secondary">
                Mark Sold
            </button>
        </form>
    <?php else: ?>
        <form method="post">
            <input type="hidden" name="action" value="show_product">
            <input type="hidden" name="product_id" value="<?php echo (int) $product['id']; ?>">

            <button type="submit" class="button button-secondary">
                Show
            </button>
        </form>
    <?php endif; ?>
</td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>

<?php sparrow_backyard_footer(); ?>