<?php

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';

$user = sparrow_require_backyard_login();

$import_notice = $_SESSION['sparrow_import_notice'] ?? '';
unset($_SESSION['sparrow_import_notice']);

sparrow_backyard_header('Imports');
?>

<?php if ($import_notice): ?>
    <section class="card">
        <p style="color:green; font-weight:bold; margin:0;">
            <?= sparrow_escape($import_notice) ?>
        </p>
    </section>
<?php endif; ?>

<?php if (isset($_GET['pricing_imported'])): ?>
    <div class="notice-success">
        Imported <?php echo (int) $_GET['pricing_imported']; ?> pricing rows.
    </div>
<?php endif; ?>

<?php if (isset($_GET['pricing_error'])): ?>
    <div class="notice-error">
        Pricing import failed: <?php echo sparrow_escape($_GET['pricing_error']); ?>
    </div>
<?php endif; ?>

<?php if (isset($_GET['wp_merge_imported'])): ?>
    <div class="notice-success">
        WordPress merge import finished:
        <?php echo (int) ($_GET['created'] ?? 0); ?> created,
        <?php echo (int) ($_GET['updated'] ?? 0); ?> updated,
        <?php echo (int) ($_GET['skipped'] ?? 0); ?> skipped.
    </div>
<?php endif; ?>

<?php if (isset($_GET['wp_merge_error'])): ?>
    <div class="notice-error">
        WordPress merge import failed:
        <?php echo sparrow_escape($_GET['wp_merge_error']); ?>
    </div>
<?php endif; ?>

<?php if (isset($_GET['shop_products_imported'])): ?>
    <div class="notice-success">
        WooCommerce shop product import finished:
        <?php echo (int) ($_GET['created'] ?? 0); ?> created,
        <?php echo (int) ($_GET['updated'] ?? 0); ?> updated,
        <?php echo (int) ($_GET['skipped'] ?? 0); ?> skipped.
    </div>
<?php endif; ?>

<?php if (isset($_GET['shop_products_error'])): ?>
    <div class="notice-error">
        WooCommerce shop product import failed:
        <?php echo sparrow_escape($_GET['shop_products_error']); ?>
    </div>
<?php endif; ?>

<?php if (isset($_GET['supplies_imported'])): ?>
    <div class="notice-success">
        WordPress supplies import finished:
        <?php echo (int) ($_GET['created'] ?? 0); ?> created,
        <?php echo (int) ($_GET['updated'] ?? 0); ?> updated,
        <?php echo (int) ($_GET['skipped'] ?? 0); ?> skipped.
    </div>
<?php endif; ?>

<?php if (isset($_GET['supplies_error'])): ?>
    <div class="notice-error">
        WordPress supplies import failed:
        <?php echo sparrow_escape($_GET['supplies_error']); ?>
    </div>
<?php endif; ?>

<style>
.import-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 14px;
    margin-top: 18px;
}

.import-card {
    border: 1px solid #e8cfd8;
    border-radius: 14px;
    padding: 16px;
    background: #fff;
}

.import-card h2 {
    margin-top: 0;
    margin-bottom: 8px;
}

.import-card p {
    margin-bottom: 14px;
}

.import-card-disabled {
    opacity: 0.65;
}

.import-button-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.import-coming-soon {
    display: inline-block;
    padding: 6px 10px;
    border-radius: 999px;
    background: #f4e7ec;
    color: #9b5c75;
    font-size: 12px;
    font-weight: bold;
}
</style>

<section class="card">
    <h1>Imports</h1>

    <p class="muted">
        Use this page to bring old Sparrow &amp; Seed data into Backyard. Each import type will open its own preview page before anything is saved.
    </p>

    <div class="import-grid">

        <div class="import-card">
    <h2>Orders</h2>

    <p class="muted">
        Import old order entries from a Forminator CSV export.
    </p>

    <form method="post" action="import-orders.php" enctype="multipart/form-data">
        <div class="form-row">
            <label for="orders_csv">Orders CSV File</label>
            <input type="file" id="orders_csv" name="orders_csv" accept=".csv,text/csv" required>
        </div>

        <div class="import-button-row" style="margin-top:10px;">
            <button type="submit" class="button">
                Upload &amp; Preview Orders CSV
            </button>
        </div>
    </form>
</div>

        <div class="import-card">
    <h2>Import Pricing / Shirt Cost</h2>

    <p class="muted">
        Upload the shirt price lookup CSV. Column A should be colors, and the top row should be sizes.
    </p>

    <form
        method="post"
        action="<?php echo sparrow_escape(sparrow_url('/backyard/import-pricing.php')); ?>"
        enctype="multipart/form-data"
    >
	
        <div class="form-section">
            <label for="pricing_csv">Pricing CSV File</label>
            <input
                type="file"
                id="pricing_csv"
                name="pricing_csv"
                accept=".csv,.txt"
                required
            >
        </div>

        <label style="margin-top: 12px;">
            <input
                type="checkbox"
                name="matrix_replace_existing"
                value="1"
                style="width:auto;"
            >
            Replace current pricing table with this import
        </label>

        <div class="form-actions">
            <button type="submit" class="button">
                Import Pricing CSV
            </button>
        </div>
    </form>
	</div>
	
	<div class="import-card">
    <h2>Temporary WordPress Order Merge Import</h2>

    <p class="muted">
        Upload the Backyard Merge CSV exported from the WordPress plugin. This will merge real totals, paid amounts, blessings, discounts, statuses, and notes into existing Backyard orders.
    </p>

    <form
        method="post"
        action="<?php echo sparrow_escape(sparrow_url('/backyard/import-wp-merge.php')); ?>"
        enctype="multipart/form-data"
    >
        <div class="form-section">
            <label for="wp_merge_csv">WordPress Backyard Merge CSV</label>
            <input
                type="file"
                id="wp_merge_csv"
                name="wp_merge_csv"
                accept=".csv,.txt"
                required
            >
        </div>

        <div class="form-actions">
            <button type="submit" class="button">
                Import WordPress Merge CSV
            </button>
        </div>
    </form>
</div>

        <div class="import-card">
    <h2>Supplier Receipts</h2>

    <p class="muted">
        Upload the Supplier Splitter JSON export from the old
        WordPress site. You will preview everything before
        anything is imported.
    </p>

    <form
        method="post"
        action="<?= sparrow_escape(
            sparrow_url(
                '/backyard/import-supplier-receipts.php'
            )
        ) ?>"
        enctype="multipart/form-data"
    >
        <div class="form-section">
            <label for="supplier_json">
                WordPress Supplier Splitter Export
            </label>

            <input
                type="file"
                id="supplier_json"
                name="supplier_json"
                accept=".json,.txt,application/json,text/plain"
                required
            >
        </div>

        <div class="form-actions">
            <button type="submit" class="button">
                Upload &amp; Preview Supplier Receipts
            </button>
        </div>
    </form>
</div>

<div class="import-card">
    <h2>WooCommerce Shop Products</h2>

    <p class="muted">
        Upload a WooCommerce product CSV export. This will import premade shop items,
        prices, categories, descriptions, featured status, image URLs, and SEO information.
    </p>

    <form
        method="post"
        action="<?php echo sparrow_escape(sparrow_url('/backyard/import-shop-products.php')); ?>"
        enctype="multipart/form-data"
    >
        <div class="form-section">
            <label for="shop_products_csv">WooCommerce Products CSV</label>
            <input
                type="file"
                id="shop_products_csv"
                name="shop_products_csv"
                accept=".csv,.txt"
                required
            >
        </div>

        <label style="margin-top: 12px;">
            <input
                type="checkbox"
                name="update_existing"
                value="1"
                checked
                style="width:auto;"
            >
            Update existing products when WooCommerce ID or product name matches
        </label>

        <div class="form-actions">
            <button type="submit" class="button">
                Upload &amp; Preview Shop Products
            </button>
        </div>
    </form>
</div>

<div class="import-card">
            <h2>WordPress Supplies</h2>

            <p class="muted">
                Upload the supplies CSV exported from the old
                WordPress site. You will preview every supply
                before anything is saved.
            </p>

            <form
                method="post"
                action="<?php echo sparrow_escape(
                    sparrow_url('/backyard/import-supplies.php')
                ); ?>"
                enctype="multipart/form-data"
            >
                <div class="form-section">
                    <label for="supplies_csv">
                        WordPress Supplies CSV
                    </label>

                    <input
                        type="file"
                        id="supplies_csv"
                        name="supplies_csv"
                        accept=".csv,.txt,text/csv"
                        required
                    >
                </div>

                <div class="form-actions">
                    <button type="submit" class="button">
                        Upload &amp; Preview Supplies
                    </button>
                </div>
            </form>
        </div>

    </div>
</section>

<?php sparrow_backyard_footer(); ?>