<?php

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

$user = sparrow_require_backyard_login();
$pdo = sparrow_db();
$settings = sparrow_get_settings($pdo, [
    'default_shirt_type',
    'standard_retail_price',
    'plus_retail_price',
]);

$default_shirt_type = trim((string) ($settings['default_shirt_type'] ?? ''));
$standard_retail_price = (float) ($settings['standard_retail_price'] ?? 15.00);
$plus_retail_price = (float) ($settings['plus_retail_price'] ?? 18.00);

if ($default_shirt_type === '') {
    $default_shirt_type = 'Gildan Light Cotton Shirt';
}

$notice = '';
$error = '';

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

    try {
        if ($action === 'save_pricing') {
            sparrow_save_pricing_rows_from_post($_POST);
            $notice = 'Pricing table saved.';
        }
		
		if ($action === 'bundle_pricing') {
    $count = sparrow_bundle_pricing_rows_in_database();
    $notice = 'Pricing rows bundled. Your table now has ' . $count . ' rows.';
}

        if ($action === 'delete_pricing_row') {
            sparrow_delete_pricing_row((int) ($_POST['pricing_id'] ?? 0));
            $notice = 'Pricing row deleted.';
        }

        if ($action === 'import_pricing_matrix') {
            $matrix_text = trim((string) ($_POST['matrix_csv_text'] ?? ''));

            $count = sparrow_import_pricing_matrix_from_text(
                $matrix_text,
                trim((string) ($_POST['matrix_item_type'] ?? $default_shirt_type)),
                (float) ($_POST['matrix_standard_retail'] ?? $standard_retail_price),
                (float) ($_POST['matrix_plus_retail'] ?? $plus_retail_price),
                trim((string) ($_POST['matrix_notes'] ?? 'Imported from pricing spreadsheet')),
                !empty($_POST['matrix_replace_existing'])
            );

            $notice = 'Imported ' . $count . ' pricing rows.';
        }
    } catch (Throwable $e) {
        $error = $e->getMessage();
    }
}


$rows = sparrow_get_pricing_rows();

sparrow_backyard_header('Pricing / Cost Table');
?>

<section class="card">
    <h1>Pricing / Cost Table</h1>

    <p class="muted">
        Use this page to keep shirt, transfer, specialty, and material costs in one place.
    </p>

    <?php if ($notice): ?>
        <div class="notice-success"><?php echo sparrow_escape($notice); ?></div>
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="notice-error"><?php echo sparrow_escape($error); ?></div>
    <?php endif; ?>
	
	<?php if (isset($_GET['bundled'])): ?>
    <div class="notice-success">
        Pricing rows bundled. Your table now has <?php echo (int) $_GET['bundled']; ?> rows.
    </div>
<?php endif; ?>

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

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

		<section class="card">
    <h2>Pricing Tools</h2>

    <p class="muted">
        Bundle rows with the same item, color, cost, retail price, and notes. This will condense matching sizes into ranges like XS-XL.
    </p>

    <form method="post" action="<?php echo sparrow_escape(sparrow_url('/backyard/pricing-bundle.php')); ?>">
        <button
            type="submit"
            class="button button-secondary"
            onclick="return confirm('Bundle rows with the same item, color, cost, retail price, and notes?');"
        >
            Bundle Same Color/Price Rows
        </button>
    </form>
</section>

<section class="card pricing-table-card">
    <div class="pricing-toolbar">
        <div>
            <label for="pricing_search">Search pricing table:</label>
            <input
                type="text"
                id="pricing_search"
                placeholder="Search color, size, item type, notes..."
            >
        </div>

        <div>
            <label for="pricing_page_size">Show:</label>
            <select id="pricing_page_size">
                <option value="10" selected>10 rows</option>
                <option value="25">25 rows</option>
                <option value="50">50 rows</option>
                <option value="100">100 rows</option>
                <option value="all">All rows</option>
            </select>
        </div>

        <span id="pricing_row_count" class="muted"></span>
        <div id="pricing_pagination" class="pricing-pagination"></div>
    </div>

    <form method="post">
        <input type="hidden" name="action" value="save_pricing">

        <div class="table-wrap">
            <table id="pricing_table" class="pricing-table">
                <thead>
                    <tr>
                        <th>Item Type</th>
                        <th>Size / Range</th>
                        <th>Color / Style</th>
                        <th>Unit Cost</th>
                        <th>Retail Price</th>
                        <th>Est. Profit</th>
                        <th>Notes</th>
                        <th>Actions</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($rows as $row): ?>
                        <?php
                        $unit_cost = (float) ($row['unit_cost'] ?? 0);
                        $retail_price = $row['retail_price'] ?? '';
                        $profit = '';

                        if ($retail_price !== null && $retail_price !== '') {
                            $profit_value = (float) $retail_price - $unit_cost;
                            $profit = '$' . number_format($profit_value, 2);
                        }
                        ?>
                        <tr>
                            <td data-label="Item Type">
                                <input type="hidden" name="pricing_id[]" value="<?php echo (int) $row['id']; ?>">
                                <input type="text" name="pricing_item_type[]" value="<?php echo sparrow_escape($row['item_type'] ?? ''); ?>">
                            </td>

                            <td data-label="Size / Range">
                                <input type="text" name="pricing_size_range[]" value="<?php echo sparrow_escape($row['size_range'] ?? ''); ?>">
                            </td>

                            <td data-label="Color / Style">
                                <input type="text" name="pricing_color_style[]" value="<?php echo sparrow_escape($row['color_style'] ?? ''); ?>">
                            </td>

                            <td data-label="Unit Cost">
                                <input type="number" step="0.01" name="pricing_unit_cost[]" value="<?php echo sparrow_escape(number_format($unit_cost, 2, '.', '')); ?>">
                            </td>

                            <td data-label="Retail Price">
                                <input type="number" step="0.01" name="pricing_retail_price[]" value="<?php echo sparrow_escape($retail_price === null ? '' : number_format((float) $retail_price, 2, '.', '')); ?>">
                            </td>

                            <td data-label="Est. Profit" class="pricing-profit">
                                <?php echo sparrow_escape($profit); ?>
                            </td>

                            <td data-label="Notes">
                                <input type="text" name="pricing_notes[]" value="<?php echo sparrow_escape($row['notes'] ?? ''); ?>">
                            </td>

                            <td data-label="Actions">
                                <button
                                    type="submit"
                                    name="delete_pricing_id"
                                    value="<?php echo (int) $row['id']; ?>"
                                    class="button button-danger"
                                    formaction="<?php echo sparrow_escape(sparrow_url('/backyard/pricing-delete.php')); ?>"
                                    onclick="return confirm('Delete this pricing row?');"
                                >
                                    Delete
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div class="form-actions">
            <button type="button" id="add_pricing_row" class="button button-secondary">
                + Add New Row
            </button>

            <button type="submit" class="button">
                Save Pricing Table
            </button>
        </div>
    </form>
</section>

<script type="text/plain" data-obsolete-pricing-script="true">
document.addEventListener('DOMContentLoaded', function () {
    const table = document.getElementById('pricing_table');
    const searchInput = document.getElementById('pricing_search');
    const pageSizeSelect = document.getElementById('pricing_page_size');
    const rowCount = document.getElementById('pricing_row_count');
    const pagination = document.getElementById('pricing_pagination');
    const addButton = document.getElementById('add_pricing_row');

    if (!table) {
        return;
    }

    const tbody = table.querySelector('tbody');
    let currentPage = 1;

    function getRows() {
        return Array.from(tbody.querySelectorAll('tr'));
    }

    function getRowText(row) {
        let text = row.innerText.toLowerCase();

        row.querySelectorAll('input, select, textarea').forEach(function (field) {
            text += ' ' + String(field.value || '').toLowerCase();
        });

        return text;
    }

    function getMatchingRows() {
        const search = searchInput ? searchInput.value.toLowerCase().trim() : '';

        return getRows().filter(function (row) {
            return search === '' || getRowText(row).includes(search);
        });
    }

    function makePageButton(label, page, disabled, active) {
        const button = document.createElement('button');
        button.type = 'button';
        button.textContent = label;
        button.className = 'button pricing-page-btn';

        if (active) {
            button.classList.add('is-active');
        }

        button.disabled = disabled;

        if (!disabled) {
            button.addEventListener('click', function () {
                currentPage = page;
                updateView();
            });
        }

        return button;
    }

    function renderPagination(totalPages, pageSizeValue) {
        pagination.innerHTML = '';

        if (pageSizeValue === 'all' || totalPages <= 1) {
            return;
        }

        pagination.appendChild(makePageButton('Prev', Math.max(1, currentPage - 1), currentPage === 1, false));

        for (let page = 1; page <= totalPages; page++) {
            pagination.appendChild(makePageButton(String(page), page, false, currentPage === page));
        }

        pagination.appendChild(makePageButton('Next', Math.min(totalPages, currentPage + 1), currentPage === totalPages, false));
    }

    function updateView() {
        const rows = getRows();
        const matchingRows = getMatchingRows();
        const pageSizeValue = pageSizeSelect ? pageSizeSelect.value : '10';

        rows.forEach(row => row.classList.add('pricing-row-hidden'));

        let pageSize = matchingRows.length;
        let totalPages = 1;

        if (pageSizeValue !== 'all') {
            pageSize = parseInt(pageSizeValue, 10) || 10;
            totalPages = Math.max(1, Math.ceil(matchingRows.length / pageSize));
        }

        if (currentPage > totalPages) {
            currentPage = totalPages;
        }

        const start = pageSizeValue === 'all' ? 0 : (currentPage - 1) * pageSize;
        const end = pageSizeValue === 'all' ? matchingRows.length : start + pageSize;

        matchingRows.slice(start, end).forEach(row => row.classList.remove('pricing-row-hidden'));

        if (rowCount) {
            const showingStart = matchingRows.length === 0 ? 0 : start + 1;
            const showingEnd = Math.min(end, matchingRows.length);

            rowCount.textContent = pageSizeValue === 'all'
                ? 'Showing all ' + matchingRows.length + ' matching rows'
                : 'Showing ' + showingStart + '-' + showingEnd + ' of ' + matchingRows.length + ' matching rows';
        }

        renderPagination(totalPages, pageSizeValue);
    }

    if (searchInput) {
        searchInput.addEventListener('input', function () {
            currentPage = 1;
            updateView();
        });
    }

    if (pageSizeSelect) {
        pageSizeSelect.addEventListener('change', function () {
            currentPage = 1;
            updateView();
        });
    }

    if (addButton) {
        addButton.addEventListener('click', function () {
            const row = document.createElement('tr');

            row.innerHTML = `
                <td data-label="Item Type">
                    <input type="hidden" name="pricing_id[]" value="0">
                    <input type="text" name="pricing_item_type[]" value="" placeholder="Gildan Light Cotton Shirt">
                </td>
                <td data-label="Size / Range">
                    <input type="text" name="pricing_size_range[]" value="" placeholder="XS-XL">
                </td>
                <td data-label="Color / Style">
                    <input type="text" name="pricing_color_style[]" value="" placeholder="Ash">
                </td>
                <td data-label="Unit Cost">
                    <input type="number" step="0.01" name="pricing_unit_cost[]" value="" placeholder="0.00">
                </td>
                <td data-label="Retail Price">
                    <input type="number" step="0.01" name="pricing_retail_price[]" value="" placeholder="0.00">
                </td>
                <td data-label="Est. Profit" class="pricing-profit"></td>
                <td data-label="Notes">
                    <input type="text" name="pricing_notes[]" value="" placeholder="Notes">
                </td>
                <td data-label="Actions">
                    New row
                </td>
            `;

            tbody.insertBefore(row, tbody.firstChild);

            if (searchInput) {
                searchInput.value = '';
            }

            if (pageSizeSelect) {
                pageSizeSelect.value = '10';
            }

            currentPage = 1;
            updateView();
            row.scrollIntoView({ behavior: 'smooth', block: 'center' });
        });
    }

    updateView();
});

function sparrow_pricing_normalize_size_label(string $size): string
{
    $size = strtoupper(trim($size));
    $size = str_replace(['(', ')'], '', $size);
    $size = preg_replace('/\s+/', ' ', $size);

    $map = [
        'X-SMALL' => 'XS',
        'EXTRA SMALL' => 'XS',
        'EXTRA-SMALL' => 'XS',
        'XS' => 'XS',

        'SMALL' => 'S',
        'S' => 'S',

        'MEDIUM' => 'M',
        'M' => 'M',

        'LARGE' => 'L',
        'L' => 'L',

        'X-LARGE' => 'XL',
        'XLARGE' => 'XL',
        'EXTRA LARGE' => 'XL',
        'EXTRA-LARGE' => 'XL',
        'XL' => 'XL',

        '2X' => '2XL',
        '2XL' => '2XL',
        'XXL' => '2XL',
        '2X LARGE' => '2XL',
        '2X-LARGE' => '2XL',
        'XX-LARGE' => '2XL',
        '2XLARGE' => '2XL',

        '3X' => '3XL',
        '3XL' => '3XL',
        '3X LARGE' => '3XL',
        '3X-LARGE' => '3XL',
        '3XLARGE' => '3XL',

        '4X' => '4XL',
        '4XL' => '4XL',
        '4X LARGE' => '4XL',
        '4X-LARGE' => '4XL',
        '4XLARGE' => '4XL',

        '5X' => '5XL',
        '5XL' => '5XL',
        '5X LARGE' => '5XL',
        '5X-LARGE' => '5XL',
        '5XLARGE' => '5XL',
    ];

    return $map[$size] ?? $size;
}

function sparrow_pricing_size_order_value(string $size): int
{
    $size = sparrow_pricing_normalize_size_label($size);

    $order = [
        'XS' => 1,
        'S' => 2,
        'M' => 3,
        'L' => 4,
        'XL' => 5,
        '2XL' => 6,
        '3XL' => 7,
        '4XL' => 8,
        '5XL' => 9,
    ];

    return $order[$size] ?? 999;
}

function sparrow_pricing_format_size_range(array $sizes): string
{
    $sizes = array_values(array_unique(array_filter(array_map(function ($size) {
        return sparrow_pricing_normalize_size_label((string) $size);
    }, $sizes))));

    usort($sizes, function ($a, $b) {
        return sparrow_pricing_size_order_value($a) <=> sparrow_pricing_size_order_value($b);
    });

    $standard_sizes = ['XS', 'S', 'M', 'L', 'XL'];

    if ($sizes === $standard_sizes) {
        return 'XS-XL';
    }

    if (count($sizes) === 1) {
        return $sizes[0];
    }

    return reset($sizes) . '-' . end($sizes);
}

function sparrow_bundle_pricing_rows_in_database(): int
{
    $pdo = sparrow_db();

    $rows = sparrow_get_pricing_rows();

    if (!$rows) {
        return 0;
    }

    $groups = [];

    foreach ($rows as $row) {
        $item_type = trim((string) ($row['item_type'] ?? ''));
        $size_range = trim((string) ($row['size_range'] ?? ''));
        $color_style = trim((string) ($row['color_style'] ?? ''));
        $unit_cost = number_format((float) ($row['unit_cost'] ?? 0), 2, '.', '');
        $retail_price = ($row['retail_price'] ?? '') === null ? '' : number_format((float) ($row['retail_price'] ?? 0), 2, '.', '');
        $notes = trim((string) ($row['notes'] ?? ''));

        if ($item_type === '' && $size_range === '' && $color_style === '') {
            continue;
        }

        $group_key = md5($item_type . '|' . $color_style . '|' . $unit_cost . '|' . $retail_price . '|' . $notes);

        if (!isset($groups[$group_key])) {
            $groups[$group_key] = [
                'item_type' => $item_type,
                'sizes' => [],
                'color_style' => $color_style,
                'unit_cost' => $unit_cost,
                'retail_price' => $retail_price,
                'notes' => $notes,
            ];
        }

        $groups[$group_key]['sizes'][] = $size_range;
    }

    $pdo->beginTransaction();

    try {
        $pdo->exec("DELETE FROM pricing_items");

        $stmt = $pdo->prepare("
            INSERT INTO pricing_items
            (
                item_type,
                size_range,
                color_style,
                unit_cost,
                retail_price,
                notes
            )
            VALUES
            (
                :item_type,
                :size_range,
                :color_style,
                :unit_cost,
                :retail_price,
                :notes
            )
        ");

        foreach ($groups as $group) {
            $stmt->execute([
                ':item_type' => $group['item_type'],
                ':size_range' => sparrow_pricing_format_size_range($group['sizes']),
                ':color_style' => $group['color_style'],
                ':unit_cost' => $group['unit_cost'],
                ':retail_price' => $group['retail_price'] === '' ? null : $group['retail_price'],
                ':notes' => $group['notes'],
            ]);
        }

        $pdo->commit();

        return count($groups);
    } catch (Throwable $e) {
        $pdo->rollBack();
        throw $e;
    }
}
</script>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const table = document.getElementById('pricing_table');
    const searchInput = document.getElementById('pricing_search');
    const pageSizeSelect = document.getElementById('pricing_page_size');
    const rowCount = document.getElementById('pricing_row_count');
    const pagination = document.getElementById('pricing_pagination');
    const addButton = document.getElementById('add_pricing_row');
    const defaultItemType = <?php echo json_encode($default_shirt_type, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); ?>;
    const defaultRetailPrice = <?php echo json_encode(number_format($standard_retail_price, 2, '.', '')); ?>;

    if (!table) {
        return;
    }

    const tbody = table.querySelector('tbody');

    if (!tbody) {
        return;
    }

    let currentPage = 1;

    function getRows() {
        return Array.from(tbody.querySelectorAll('tr'));
    }

    function getRowText(row) {
        let text = row.innerText.toLowerCase();

        row.querySelectorAll('input, textarea, select').forEach(function (field) {
            text += ' ' + String(field.value || '').toLowerCase();
        });

        return text;
    }

    function getMatchingRows() {
        const searchValue = searchInput ? searchInput.value.toLowerCase().trim() : '';
        const rows = getRows();

        return rows.filter(function (row) {
            const rowText = getRowText(row);
            return searchValue === '' || rowText.includes(searchValue);
        });
    }

    function makePageButton(label, page, disabled, isActive) {
        const button = document.createElement('button');

        button.type = 'button';
        button.textContent = label;
        button.className = 'button pricing-page-btn';

        if (isActive) {
            button.classList.add('is-active');
        }

        button.disabled = !!disabled;

        if (!disabled) {
            button.addEventListener('click', function () {
                currentPage = page;
                updatePricingTableView();
            });
        }

        return button;
    }

    function renderPagination(totalPages, pageSizeValue) {
        if (!pagination) {
            return;
        }

        pagination.innerHTML = '';

        if (pageSizeValue === 'all' || totalPages <= 1) {
            return;
        }

        pagination.appendChild(
            makePageButton('Prev', Math.max(1, currentPage - 1), currentPage === 1, false)
        );

        const maxButtons = 5;
        let startPage = Math.max(1, currentPage - 2);
        let endPage = Math.min(totalPages, startPage + maxButtons - 1);

        if (endPage - startPage < maxButtons - 1) {
            startPage = Math.max(1, endPage - maxButtons + 1);
        }

        if (startPage > 1) {
            pagination.appendChild(makePageButton('1', 1, false, currentPage === 1));

            if (startPage > 2) {
                const dots = document.createElement('span');
                dots.textContent = '...';
                dots.className = 'pricing-pagination-dots';
                pagination.appendChild(dots);
            }
        }

        for (let page = startPage; page <= endPage; page++) {
            pagination.appendChild(
                makePageButton(String(page), page, false, currentPage === page)
            );
        }

        if (endPage < totalPages) {
            if (endPage < totalPages - 1) {
                const dots = document.createElement('span');
                dots.textContent = '...';
                dots.className = 'pricing-pagination-dots';
                pagination.appendChild(dots);
            }

            pagination.appendChild(
                makePageButton(String(totalPages), totalPages, false, currentPage === totalPages)
            );
        }

        pagination.appendChild(
            makePageButton('Next', Math.min(totalPages, currentPage + 1), currentPage === totalPages, false)
        );
    }

    function updatePricingTableView() {
        const rows = getRows();
        const matchingRows = getMatchingRows();
        const pageSizeValue = pageSizeSelect ? pageSizeSelect.value : '10';

        rows.forEach(function (row) {
            row.classList.add('pricing-row-hidden');
        });

        let pageSize = matchingRows.length;
        let totalPages = 1;

        if (pageSizeValue !== 'all') {
            pageSize = parseInt(pageSizeValue, 10);

            if (!pageSize || pageSize < 1) {
                pageSize = 10;
            }

            totalPages = Math.max(1, Math.ceil(matchingRows.length / pageSize));
        }

        if (currentPage > totalPages) {
            currentPage = totalPages;
        }

        if (currentPage < 1) {
            currentPage = 1;
        }

        let startIndex = 0;
        let endIndex = matchingRows.length;

        if (pageSizeValue !== 'all') {
            startIndex = (currentPage - 1) * pageSize;
            endIndex = startIndex + pageSize;
        }

        matchingRows.slice(startIndex, endIndex).forEach(function (row) {
            row.classList.remove('pricing-row-hidden');
        });

        if (rowCount) {
            const showingStart = matchingRows.length === 0 ? 0 : startIndex + 1;
            const showingEnd = Math.min(endIndex, matchingRows.length);

            if (pageSizeValue === 'all') {
                rowCount.textContent = 'Showing all ' + matchingRows.length + ' matching rows';
            } else {
                rowCount.textContent = 'Showing ' + showingStart + '-' + showingEnd + ' of ' + matchingRows.length + ' matching rows';
            }
        }

        renderPagination(totalPages, pageSizeValue);
    }

    if (searchInput) {
        searchInput.addEventListener('input', function () {
            currentPage = 1;
            updatePricingTableView();
        });
    }

    if (pageSizeSelect) {
        pageSizeSelect.addEventListener('change', function () {
            currentPage = 1;
            updatePricingTableView();
        });
    }

    if (addButton) {
        addButton.addEventListener('click', function () {
            const row = document.createElement('tr');

            row.innerHTML = `
                <td data-label="Item Type">
                    <input type="hidden" name="pricing_id[]" value="0">
                    <input type="text" name="pricing_item_type[]" value="" placeholder="Gildan Light Cotton Shirt">
                </td>

                <td data-label="Size / Range">
                    <input type="text" name="pricing_size_range[]" value="" placeholder="XS-XL">
                </td>

                <td data-label="Color / Style">
                    <input type="text" name="pricing_color_style[]" value="" placeholder="Ash">
                </td>

                <td data-label="Unit Cost">
                    <input type="number" step="0.01" name="pricing_unit_cost[]" value="" placeholder="0.00">
                </td>

                <td data-label="Retail Price">
                    <input type="number" step="0.01" name="pricing_retail_price[]" value="" placeholder="0.00">
                </td>

                <td data-label="Est. Profit" class="pricing-profit"></td>

                <td data-label="Notes">
                    <input type="text" name="pricing_notes[]" value="" placeholder="Notes">
                </td>

                <td data-label="Actions">
                    New row
                </td>
            `;

            const itemTypeInput = row.querySelector('[name="pricing_item_type[]"]');
            const retailPriceInput = row.querySelector('[name="pricing_retail_price[]"]');

            if (itemTypeInput) {
                itemTypeInput.value = defaultItemType;
            }

            if (retailPriceInput) {
                retailPriceInput.value = defaultRetailPrice;
            }

            tbody.insertBefore(row, tbody.firstChild);

            if (searchInput) {
                searchInput.value = '';
            }

            if (pageSizeSelect) {
                pageSizeSelect.value = '10';
            }

            currentPage = 1;
            updatePricingTableView();

            row.classList.remove('pricing-row-hidden');
            row.scrollIntoView({ behavior: 'smooth', block: 'center' });

            const firstInput = row.querySelector('input[type="text"]');

            if (firstInput) {
                firstInput.focus();
            }
        });
    }

    updatePricingTableView();
});
</script>

<?php
sparrow_backyard_footer();
