@extends('site_admin.layouts.app')

@section('title', '站点商品')
@section('page_title', '站点商品')

@section('content')
<div class="card card-spaced">
    <p class="muted mt-0">当前页面展示站点范围内的商品列表，已补最小筛选与导出入口，便于站点侧先做基础运营查看。</p>
    @if(!empty($filters['validation_errors'] ?? []))
        <div class="error-box mt-12">
            <strong>筛选条件有误：</strong>
            <ul class="ml-18">
                @foreach(($filters['validation_errors'] ?? []) as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    @php
        $productExportQuery = http_build_query(array_filter([
            'keyword' => $filters['keyword'] ?? '',
            'status' => $filters['status'] ?? '',
            'category_id' => $filters['category_id'] ?? '',
            'min_price' => $filters['min_price'] ?? '',
            'max_price' => $filters['max_price'] ?? '',
            'min_stock' => $filters['min_stock'] ?? '',
            'max_stock' => $filters['max_stock'] ?? '',
            'sort' => $filters['sort'] ?? 'latest',
        ], fn ($value) => $value !== null && $value !== ''));
    @endphp
    <h3>筛选条件</h3>
    <form method="get" action="/site-admin/products">
        <div class="grid-4 form-grid">
            <input type="text" name="keyword" placeholder="关键词：标题 / SKU / slug" value="{{ $filters['keyword'] ?? '' }}">
            <select name="status">
                <option value="">全部状态</option>
                @foreach(($filterOptions['statuses'] ?? []) as $status)
                    <option value="{{ $status }}" @selected(($filters['status'] ?? '') === $status)>{{ ($statusLabels[$status] ?? $status) }}</option>
                @endforeach
            </select>
            <select name="category_id">
                <option value="">全部分类</option>
                @foreach($categories as $category)
                    <option value="{{ $category->id }}" @selected((string) ($filters['category_id'] ?? '') === (string) $category->id)>{{ $category->name }}</option>
                @endforeach
            </select>
            <select name="sort">
                @foreach(($filterOptions['sortOptions'] ?? []) as $value => $label)
                    <option value="{{ $value }}" @selected(($filters['sort'] ?? 'latest') === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <input type="number" step="0.01" min="0" name="min_price" placeholder="最低价格" value="{{ $filters['min_price'] ?? '' }}">
            <input type="number" step="0.01" min="0" name="max_price" placeholder="最高价格" value="{{ $filters['max_price'] ?? '' }}">
            <input type="number" min="0" name="min_stock" placeholder="最低库存" value="{{ $filters['min_stock'] ?? '' }}">
            <input type="number" min="0" name="max_stock" placeholder="最高库存" value="{{ $filters['max_stock'] ?? '' }}">
        </div>
        <div class="actions mt-12">
            <button type="submit">应用筛选</button>
            <a href="/site-admin/products">重置</a>
            <a href="{{ '/site-admin/products/export' . ($productExportQuery ? ('?' . $productExportQuery) : '') }}">导出当前筛选结果 CSV</a>
        </div>
    </form>
</div>

<div class="card card-spaced">
    <h3 class="mt-0">当前筛选摘要</h3>
    <div class="grid-3">
        @foreach(($activeFilterSummary ?? []) as $label => $value)
            <div class="summary-box">
                <div class="muted">{{ $label }}</div>
                <strong class="text-md">{{ $value }}</strong>
            </div>
        @endforeach
    </div>
</div>

<div class="card card-spaced">
    <h3 class="mt-0">商品运营汇总</h3>
    <div class="grid-4">
        <div class="stat-box">
            <div class="muted">商品总数</div>
            <strong class="num-md">{{ $summaryStats['total_products'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">总库存</div>
            <strong class="num-md">{{ $summaryStats['total_stock'] ?? 0 }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">总货值</div>
            <strong class="num-md">¥{{ number_format($summaryStats['total_stock_value'] ?? 0, 2) }}</strong>
        </div>
        <div class="stat-box">
            <div class="muted">平均售价</div>
            <strong class="num-md">¥{{ number_format($summaryStats['average_price'] ?? 0, 2) }}</strong>
        </div>
    </div>
</div>

<div class="card card-dark card-spaced">
    <h3 class="mt-0">运营关注项</h3>
    <div class="grid-3">
        <div class="focus-box">
            <div class="muted">商品盘面</div>
            <strong class="text-md">{{ $operationsFocus['headline'] ?? '当前站点商品运营信息已就绪。' }}</strong>
            <div class="muted mt-8">建议动作</div>
            <div class="inline-links mt-6">
                @foreach(($operationsFocus['actions'] ?? []) as $action)
                    <a href="{{ $action['url'] }}">{{ $action['label'] }}</a>
                @endforeach
            </div>
        </div>
        <div class="focus-box">
            <div class="muted">当前信号</div>
            <div class="grid-3 mt-8">
                @foreach(($operationsFocus['signals'] ?? []) as $label => $value)
                    <div class="summary-box">
                        <div class="muted">{{ $label }}</div>
                        <strong class="num-sm">{{ $value }}</strong>
                    </div>
                @endforeach
            </div>
        </div>
        <div class="focus-box">
            <div class="muted">工作台导航</div>
            <div class="inline-links mt-8">
                @foreach(($operationsFocus['workbench'] ?? []) as $label => $url)
                    <a href="{{ $url }}">{{ $label }}</a>
                @endforeach
            </div>
        </div>
    </div>
</div>

<div class="card card-dark card-spaced">
    <h3 class="mt-0">商品状态统计</h3>
    @php
        $productBaseQuery = [
            'keyword' => ($filters['keyword'] ?? '') ?: null,
            'category_id' => ($filters['category_id'] ?? '') ?: null,
            'min_price' => ($filters['min_price'] ?? '') ?: null,
            'max_price' => ($filters['max_price'] ?? '') ?: null,
            'min_stock' => ($filters['min_stock'] ?? '') ?: null,
            'max_stock' => ($filters['max_stock'] ?? '') ?: null,
            'sort' => ($filters['sort'] ?? 'latest') !== 'latest' ? ($filters['sort'] ?? 'latest') : null,
        ];
    @endphp
    <div class="grid-4">
        <a class="status-card {{ ($filters['status'] ?? '') === '' ? 'status-card-active' : '' }}" href="{{ '/site-admin/products?' . http_build_query(array_filter($productBaseQuery, fn ($value) => $value !== null && $value !== '')) }}">
            <div class="muted">全部</div>
            <strong class="num-md">{{ $statusStats['all'] ?? 0 }}</strong>
        </a>
        @foreach(($filterOptions['statuses'] ?? []) as $status)
            <a class="status-card {{ ($filters['status'] ?? '') === $status ? 'status-card-active' : '' }}" href="{{ '/site-admin/products?' . http_build_query(array_filter(array_merge($productBaseQuery, ['status' => $status]), fn ($value) => $value !== null && $value !== '')) }}">
                <div class="muted">{{ ($statusLabels[$status] ?? $status) }}</div>
                <strong class="num-md">{{ $statusStats[$status] ?? 0 }}</strong>
            </a>
        @endforeach
    </div>
</div>

<div class="card">
    <h3>商品列表</h3>
    <table>
        <thead><tr><th>ID</th><th>标题</th><th>SKU</th><th>分类</th><th>价格</th><th>库存</th><th>状态</th></tr></thead>
        <tbody>
        @forelse($products as $product)
            <tr>
                <td>{{ $product->id }}</td>
                <td>{{ $product->title }}</td>
                <td>{{ $product->sku }}</td>
                <td>{{ $product->category?->name ?? '-' }}</td>
                <td>{{ number_format((float) $product->price, 2) }}</td>
                <td>{{ $product->stock }}</td>
                <td>{{ $statusLabels[$product->status] ?? $product->status }}</td>
            </tr>
        @empty
            <tr><td colspan="7" class="muted">暂无商品</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
<div class="mt-16">{{ $products->links() }}</div>
@endsection
