@extends('layouts.admin.app')

@section('title', translate('Courier Reconciliation'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon"><i class="tio-sync"></i></span>
                <span>{{ translate('Courier Reconciliation') }}</span>
            </h1>
        </div>

        @include('admin-views.courier.partials._nav')

        <div class="card mb-3">
            <div class="card-header border-0">
                <h4 class="card-title">{{ translate('Import Courier Statement CSV') }}</h4>
            </div>
            <div class="card-body">
                <form method="post" action="{{ route('admin.courier.reconciliation.import') }}" enctype="multipart/form-data">
                    @csrf
                    <div class="row g-3">
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Courier') }}</label>
                            <select name="courier_id" class="form-control" required>
                                <option value="">{{ translate('Select Courier') }}</option>
                                @foreach($couriers as $courier)
                                    <option value="{{ $courier->id }}">{{ $courier->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="input-label">{{ translate('Import Date') }}</label>
                            <input type="date" name="import_date" value="{{ now()->toDateString() }}" class="form-control" required>
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('Reference No') }}</label>
                            <input type="text" name="reference_no" class="form-control">
                        </div>
                        <div class="col-md-3">
                            <label class="input-label">{{ translate('CSV File') }}</label>
                            <input type="file" name="statement_file" class="form-control" accept=".csv,.txt" required>
                        </div>
                        <div class="col-md-1 d-flex align-items-end">
                            <button class="btn btn--primary btn-block">{{ translate('Import') }}</button>
                        </div>
                    </div>
                    <div class="fs-12 text-muted mt-2">
                        {{ translate('Supported columns include parcel_id, consignment_id, tracking_id, invoice, cod_amount, courier_charge, settled_amount.') }}
                    </div>
                </form>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form method="get">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <select name="courier_id" class="form-control">
                                <option value="">{{ translate('All Couriers') }}</option>
                                @foreach($couriers as $courier)
                                    <option value="{{ $courier->id }}" {{ (string) request('courier_id') === (string) $courier->id ? 'selected' : '' }}>{{ $courier->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2"><button class="btn btn--primary">{{ translate('Filter') }}</button></div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('Date') }}</th>
                        <th>{{ translate('Courier') }}</th>
                        <th>{{ translate('Reference') }}</th>
                        <th class="text-right">{{ translate('Rows') }}</th>
                        <th class="text-right">{{ translate('Matched') }}</th>
                        <th class="text-right">{{ translate('Mismatch') }}</th>
                        <th class="text-right">{{ translate('Missing') }}</th>
                        <th class="text-right">{{ translate('Duplicate') }}</th>
                        <th>{{ translate('Status') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($imports as $import)
                        <tr>
                            <td>{{ $import->import_date?->format('d M Y') }}</td>
                            <td>{{ $import->courier?->name }}</td>
                            <td>
                                {{ $import->reference_no ?? translate('N/A') }}
                                <div class="fs-12 text-muted">{{ $import->file_name }}</div>
                            </td>
                            <td class="text-right">{{ $import->total_rows }}</td>
                            <td class="text-right text-success">{{ $import->matched_rows }}</td>
                            <td class="text-right text-warning">{{ $import->mismatch_rows }}</td>
                            <td class="text-right text-danger">{{ $import->missing_rows }}</td>
                            <td class="text-right">{{ $import->duplicate_rows }}</td>
                            <td><span class="badge badge-soft-info">{{ translate($import->status) }}</span></td>
                        </tr>
                    @empty
                        <tr><td colspan="9" class="text-center py-4">{{ translate('No reconciliation import found') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($imports->hasPages())
                <div class="card-footer">{!! $imports->links() !!}</div>
            @endif
        </div>
    </div>
@endsection
