@extends('admin.layouts.app')

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

@section('content')
@php
    $exportHistoryQuery = http_build_query(array_filter([
        'merchant_id' => $importHistoryFilters['merchant_id'] ?? '',
        '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

<div class="page-header mb-20" data-page="admin.products.import_histories">
    <div class="page-header-main">
        <div>
            <div class="page-header-title">商品导入历史</div>
            <div class="page-header-subtitle">集中复盘总台视角的商品导入结果、失败明细与文件状态（面向运营）。</div>
        </div>
        <div class="page-header-actions">
            <a href="{{ '/admin/products/import-histories/export' . ($exportHistoryQuery ? ('?' . $exportHistoryQuery) : '') }}" class="btn btn-secondary btn-sm">导出 CSV</a>
            <a href="/admin/products/import-histories" class="btn btn-secondary btn-sm">清空筛选</a>
        </div>
    </div>
</div>

<details class="card mb-20 collapsible filters-card" data-role="collapsible" data-storage-key="admin.products.import_histories.filters" open>
    <summary class="collapsible-summary filters-summary">
        <div class="flex-between items-center">
            <div>
                <h3 class="mb-0">筛选条件</h3>
                <div class="muted muted-xs">按商家/结果/时间范围筛选导入批次，便于定位失败文件与问题批次。</div>
            </div>
            <span class="muted muted-xs">点击收起/展开</span>
        </div>
    </summary>

    <div class="collapsible-body filters-body">
        <form method="get" action="/admin/products/import-histories" class="filters-grid">
            <div class="actions gap-10">
                <select name="merchant_id">
                    <option value="">全部商家</option>
                    @foreach(($historyMerchants ?? collect()) as $merchant)
                        <option value="{{ $merchant->id }}" @selected(($importHistoryFilters['merchant_id'] ?? '') === (string) $merchant->id)>{{ $merchant->id }} / {{ $merchant->name }}</option>
                    @endforeach
                </select>
                <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" class="btn btn-sm">筛选导入历史</button>
            </div>
        </form>

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

<div class="card mb-20">
    <div class="grid-4">
        <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>
</div>

<div class="card list-card">
    <div class="list-card-header">
        <div>
            <h3 class="list-card-title">导入记录</h3>
        </div>
    </div>
    <div class="list-card-body">
        <table class="list-card-table">
            <thead><tr><th>ID</th><th>导入时间</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->merchant?->name ?? '平台批量导入' }}</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="/admin/products/import-failures/{{ $history->failure_file }}" class="btn btn-secondary btn-sm">下载 failure CSV</a>
                        @elseif($history->failure_file)
                            <span class="muted">文件已过保留期</span>
                        @else
                            <span class="muted">无</span>
                        @endif
                    </td>
                </tr>
            @empty
                <tr><td colspan="9" class="muted table-empty">暂无导入记录</td></tr>
            @endforelse
            </tbody>
        </table>

        {{ $importHistories->links('pagination.admin') }}
    </div>
</div>
@endsection
