@extends('layouts.app')

@section('title','Data Transaksi')

@section('content')

<div class="container">

    <div class="d-flex justify-content-between align-items-center mb-4">

        <h2>📄 Data Transaksi</h2>

        <a href="{{ route('transaksi.create') }}"
            class="btn btn-primary">

            + Tambah Transaksi

        </a>

    </div>

    <div class="card-dark mb-3 p-3">
        <form method="GET" action="{{ route('transaksi.index') }}" class="d-flex gap-2 align-items-center flex-wrap">
            <input name="search" type="text" class="form-control-dark" placeholder="Cari nama pembeli atau event..." value="{{ request('search') }}" style="min-width:240px" data-live-search="true">
            <select name="status" class="form-control-dark">
                <option value="">Semua Status</option>
                <option value="Pending" {{ request('status')=='Pending' ? 'selected' : '' }}>Pending</option>
                <option value="Confirmed" {{ request('status')=='Confirmed' ? 'selected' : '' }}>Confirmed</option>
                <option value="Rejected" {{ request('status')=='Rejected' ? 'selected' : '' }}>Rejected</option>
            </select>
            <select name="date_filter" class="form-control-dark">
                <option value="">Semua Tanggal</option>
                <option value="today" {{ request('date_filter') == 'today' ? 'selected' : '' }}>Hari Ini</option>
                <option value="this_week" {{ request('date_filter') == 'this_week' ? 'selected' : '' }}>Minggu Ini</option>
                <option value="this_month" {{ request('date_filter') == 'this_month' ? 'selected' : '' }}>Bulan Ini</option>
                <option value="custom" {{ request('date_filter') == 'custom' ? 'selected' : '' }}>Rentang Tanggal</option>
            </select>
            @if(request('date_filter') == 'custom')
                <input type="date" name="date_from" class="form-control-dark" value="{{ request('date_from') }}">
                <input type="date" name="date_to" class="form-control-dark" value="{{ request('date_to') }}">
            @endif
            <select name="perpage" class="form-control-dark">
                <option value="10" {{ request('perpage',10) == 10 ? 'selected' : '' }}>10</option>
                <option value="25" {{ request('perpage') == 25 ? 'selected' : '' }}>25</option>
                <option value="50" {{ request('perpage') == 50 ? 'selected' : '' }}>50</option>
            </select>
            <div class="ms-auto d-flex gap-2">
                <button class="btn-primary-glow">Filter</button>
                <a href="{{ route('transaksi.index') }}" class="btn btn-outline-light">Reset</a>
            </div>
        </form>
    </div>

    @if(session('success'))

        <div class="alert alert-success">

            {{ session('success') }}

        </div>

    @endif

    @if(session('error'))

        <div class="alert alert-danger">

            {{ session('error') }}

        </div>

    @endif

    <div class="card shadow">

        <div class="card-body">

            <div class="table-responsive">

                <table class="table table-bordered table-hover align-middle">

                    <thead class="table-dark">

                        <tr>

                            <th>No</th>

                            <th>Nama Pembeli</th>

                            <th>Event</th>

                            <th>Jenis Tiket</th>

                            <th>Jumlah</th>

                            <th>Total</th>

                            <th>Status</th>

                            <th width="260">Aksi</th>

                        </tr>

                    </thead>

                    <tbody>

                    @forelse($transaksis as $no => $transaksi)

                        <tr>

                            <td>

                                {{ ($transaksis->firstItem()-1)+$no }}

                            </td>

                            <td>

                                {{ !empty($transaksi->nama_pembeli) ? $transaksi->nama_pembeli : '-' }}

                            </td>

                            <td>

                                {{ $transaksi->tiket->event->nama_event ?? '-' }}

                            </td>

                            <td>

                                {{ $transaksi->tiket->jenis ?? '-' }}

                            </td>

                            <td>

                                {{ $transaksi->jumlah }}

                            </td>

                            <td>

                                Rp {{ number_format($transaksi->total_harga,0,',','.') }}

                            </td>

                            <td>

                                @if($transaksi->status=="Pending")

                                    <span class="badge bg-warning text-dark">

                                        Pending

                                    </span>

                                @elseif($transaksi->status=="Confirmed")

                                    <span class="badge bg-success">

                                        Confirmed

                                    </span>

                                @elseif($transaksi->status=="Rejected")

                                    <span class="badge bg-danger">

                                        Rejected

                                    </span>

                                @else

                                    <span class="badge bg-secondary">

                                        {{ $transaksi->status }}

                                    </span>

                                @endif

                            </td>

                            <td>

                                <div class="d-flex flex-wrap gap-1">

                                    <a
                                        href="{{ route('transaksi.show',$transaksi->id) }}"
                                        class="btn btn-info btn-sm">

                                        Detail

                                    </a>

                                    <a
                                        href="{{ route('transaksi.edit',$transaksi->id) }}"
                                        class="btn btn-warning btn-sm">

                                        Edit

                                    </a>

                                    @if($transaksi->status=="Pending")

                                        <form
                                            action="{{ route('transaksi.konfirmasi',$transaksi->id) }}"
                                            method="POST">

                                            @csrf

                                            @method('PATCH')

                                            <input
                                                type="hidden"
                                                name="status"
                                                value="Confirmed">

                                            <button
                                                class="btn btn-success btn-sm">

                                                Confirm

                                            </button>

                                        </form>

                                        <form
                                            action="{{ route('transaksi.konfirmasi',$transaksi->id) }}"
                                            method="POST">

                                            @csrf

                                            @method('PATCH')

                                            <input
                                                type="hidden"
                                                name="status"
                                                value="Rejected">

                                            <button
                                                class="btn btn-danger btn-sm">

                                                Reject

                                            </button>

                                        </form>

                                    @endif

                                    <form
                                        action="{{ route('transaksi.destroy',$transaksi->id) }}"
                                        method="POST"
                                        onsubmit="return confirm('Hapus transaksi?')">

                                        @csrf

                                        @method('DELETE')

                                        <button
                                            class="btn btn-secondary btn-sm">

                                            Hapus

                                        </button>

                                    </form>

                                </div>

                            </td>

                        </tr>

                    @empty

                        <tr>

                            <td colspan="8" class="text-center">

                                Belum ada transaksi.

                            </td>

                        </tr>

                    @endforelse

                    </tbody>

                </table>

            </div>

            <div class="mt-3">

                {{ $transaksis->appends(request()->except('page'))->links() }}

            </div>

        </div>

    </div>

</div>

@endsection