@extends('merchant_admin.layouts.app')

@section('title', '商家商品导入历史')
@section('page_title', '商家商品导入历史')

@section('content')
<div class="card">
    <p class="muted muted-tight">这里集中查看当前商家范围内的商品导入历史，便于复盘成功率、失败批次与 failure CSV 保留情况。</p>

    @php
        $exportHistoryQuery = http_build_query(array_filter([
            'import_result_status' => $importHistoryFilters['result_status'] ?? 'all',
            'import_time_range' => $importHistoryFilters['time_range'] ?? 'all',
            'start_date' => $importHistoryFilters['start_date'] ?? '',
            'end_date' => $importHistoryFilters['end_date'] ?? '',
            'import_sort' => $importHistoryFilters['sort'] ?? 'latest',
        ], fn ($value, $key) => match ($key) {
            'start_date', 'end_date' => $value !== null && $value !== '',
            default => $value !== null && $value !== '' && $value !== 'all' && $value !== 'latest',
        }, ARRAY_FILTER_USE_BOTH));
    @endphp
    <form method="get" action="/merchant-admin/products/import-histories" class="mb-12">
        <div class="actions">
            <select name="import_result_status">
                <option value="all" @selected(($importHistoryFilters['result_status'] ?? 'all') === 'all')>全部导入结果</option>
                <option value="success_only" @selected(($importHistoryFilters['result_status'] ?? 'all') === 'success_only')>仅看成功导入</option>
                <option value="has_failures" @selected(($importHistoryFilters['result_status'] ?? 'all') === 'has_failures')>仅看含失败导入</option>
            </select>
            <select name="import_time_range">
                <option value="all" @selected(($importHistoryFilters['time_range'] ?? 'all') === 'all')>全部时间</option>
                <option value="today" @selected(($importHistoryFilters['time_range'] ?? 'all') === 'today')>今天</option>
                <option value="last_7_days" @selected(($importHistoryFilters['time_range'] ?? 'all') === 'last_7_days')>近7天</option>
                <option value="custom" @selected(($importHistoryFilters['time_range'] ?? 'all') === 'custom')>自定义区间</option>
            </select>
            <input type="date" name="start_date" value="{{ $importHistoryFilters['start_date'] ?? '' }}">
            <span class="muted">至</span>
            <input type="date" name="end_date" value="{{ $importHistoryFilters['end_date'] ?? '' }}">
            <select name="import_sort">
                @foreach(($importHistoryFilterOptions['sorts'] ?? []) as $sortValue => $sortLabel)
                    <option value="{{ $sortValue }}" @selected(($importHistoryFilters['sort'] ?? 'latest') === $sortValue)>{{ $sortLabel }}</option>
                @endforeach
            </select>
            <button type="submit">筛选导入历史</button>
            <a href="{{ '/merchant-admin/products/import-histories/export' . ($exportHistoryQuery ? ('?' . $exportHistoryQuery) : '') }}">导出当前筛选 CSV</a>
            <a href="/merchant-admin/products/import-histories">清空筛选</a>
            <a href="/merchant-admin/products">返回商品页</a>
        </div>
    </form>

    @if(!empty($importHistoryFilters['date_errors']))
        <div class="info-warning">
            @foreach($importHistoryFilters['date_errors'] as $dateError)
                <div>{{ $dateError }}</div>
            @endforeach
        </div>
    @endif

    <div class="grid-4 mb-12">
        <div class="stat-box"><div class="muted">累计导入批次</div><strong class="num-sm">{{ $importHistoryStats['total_imports'] ?? 0 }}</strong></div>
        <div class="stat-box"><div class="muted">累计成功商品</div><strong class="num-sm">{{ $importHistoryStats['total_success'] ?? 0 }}</strong></div>
        <div class="stat-box"><div class="muted">累计失败商品</div><strong class="num-sm">{{ $importHistoryStats['total_failed'] ?? 0 }}</strong></div>
        <div class="stat-box"><div class="muted">含失败批次</div><strong class="num-sm">{{ $importHistoryStats['warning_imports'] ?? 0 }}</strong></div>
    </div>

    <table>
        <thead><tr><th>ID</th><th>导入时间</th><th>上传文件</th><th>结果</th><th>成功</th><th>失败</th><th>操作者</th><th>失败明细</th></tr></thead>
        <tbody>
        @forelse($importHistories as $history)
            <tr>
                <td>{{ $history->id }}</td>
                <td>{{ $history->imported_at?->format('Y-m-d H:i:s') }}</td>
                <td>{{ $history->file_name }}</td>
                <td>
                    @if(($history->failed_count ?? 0) > 0)
                        <span class="result-warning">部分失败</span>
                    @else
                        <span class="result-success">成功</span>
                    @endif
                </td>
                <td>{{ $history->success_count }}</td>
                <td>{{ $history->failed_count }}</td>
                <td>{{ $history->admin?->name ?? '-' }}</td>
                <td>
                    @if($history->failure_file && ($history->failure_file_available ?? false))
                        <a href="/merchant-admin/products/import-failures/{{ $history->failure_file }}">下载 failure CSV</a>
                    @elseif($history->failure_file)
                        <span class="muted">文件已过保留期</span>
                    @else
                        <span class="muted">无</span>
                    @endif
                </td>
            </tr>
        @empty
            <tr><td colspan="8" class="muted">暂无导入记录</td></tr>
        @endforelse
        </tbody>
    </table>

    <div class="pagination-wrap">{{ $importHistories->links() }}</div>
</div>
@endsection
