@extends('layouts.admin.app')

@section('title', translate('Create Bangla QR Settlement'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon">
                    <i class="tio-receipt-outlined"></i>
                </span>
                <span>{{ translate('Create Bangla QR Settlement') }}</span>
            </h1>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.showroom.bangla-qr.settlements.create') }}" method="get">
                    <div class="row g-3">
                        <div class="col-sm-6 col-md-3">
                            <input type="search" name="search" value="{{ request('search') }}" class="form-control" placeholder="{{ translate('Search transaction') }}">
                        </div>
                        <div class="col-sm-6 col-md-2">
                            <input type="date" name="from" value="{{ request('from') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-2">
                            <input type="date" name="to" value="{{ request('to') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-1">
                            <button class="btn btn--primary btn-block" type="submit">{{ translate('Go') }}</button>
                        </div>
                        <div class="col-sm-6 col-md-1">
                            <a href="{{ route('admin.showroom.bangla-qr.settlements.create') }}" class="btn btn--reset btn-block">{{ translate('Reset') }}</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <form action="{{ route('admin.showroom.bangla-qr.settlements.store') }}" method="post">
            @csrf
            <div class="card mb-3">
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-sm-6 col-md-3">
                            <label class="form-label">{{ translate('Settlement Date') }}</label>
                            <input type="date" name="settlement_date" value="{{ old('settlement_date', now()->toDateString()) }}" class="form-control" required>
                        </div>
                        <div class="col-sm-6 col-md-3">
                            <label class="form-label">{{ translate('Bank Reference') }}</label>
                            <input type="text" name="bank_reference" value="{{ old('bank_reference') }}" class="form-control" maxlength="160">
                        </div>
                        <div class="col-sm-6 col-md-3">
                            <label class="form-label">{{ translate('Gateway/Bank Charge') }}</label>
                            <input type="number" step="0.00000001" min="0" name="charge_amount" value="{{ old('charge_amount', 0) }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-3 d-flex align-items-end">
                            <button type="submit" class="btn btn--primary btn-block">{{ translate('Post Settlement') }}</button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="row g-3 mb-3">
                <div class="col-sm-6 col-lg-3">
                    <div class="card h-100">
                        <div class="card-body">
                            <h6>{{ translate('Unsettled Transactions') }}</h6>
                            <h3>{{ $summary['count'] }}</h3>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-lg-3">
                    <div class="card h-100">
                        <div class="card-body">
                            <h6>{{ translate('Listed Amount') }}</h6>
                            <h3>{{ \App\CentralLogics\Helpers::format_currency($summary['amount']) }}</h3>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header border-0 d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">{{ translate('Paid Verified Bangla QR Transactions') }}</h5>
                    <label class="mb-0">
                        <input type="checkbox" class="js-select-all">
                        {{ translate('Select visible') }}
                    </label>
                </div>
                <div class="table-responsive">
                    <table class="table table-borderless table-thead-bordered mb-0">
                        <thead class="thead-light">
                        <tr>
                            <th>{{ translate('Select') }}</th>
                            <th>{{ translate('Transaction') }}</th>
                            <th>{{ translate('Paid At') }}</th>
                            <th>{{ translate('Provider') }}</th>
                            <th>{{ translate('Provider Txn') }}</th>
                            <th>{{ translate('QR Reference') }}</th>
                            <th class="text-right">{{ translate('Amount') }}</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($transactions as $transaction)
                            <tr>
                                <td>
                                    <input type="checkbox" name="payment_transaction_ids[]" value="{{ $transaction->id }}" class="js-settlement-row">
                                </td>
                                <td>#{{ $transaction->id }} / {{ \Illuminate\Support\Str::limit($transaction->transaction_id, 18) }}</td>
                                <td>{{ $transaction->paid_at ? \Carbon\Carbon::parse($transaction->paid_at)->format('d M Y h:i A') : '-' }}</td>
                                <td>{{ ucfirst(str_replace('_', ' ', $transaction->provider_name ?: '-')) }}</td>
                                <td>{{ $transaction->provider_transaction_id ?: '-' }}</td>
                                <td>{{ $transaction->qr_reference ?: '-' }}</td>
                                <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($transaction->amount) }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center py-4">{{ translate('No unsettled paid Bangla QR transaction found') }}</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="card-footer border-0">
                    {!! $transactions->links() !!}
                </div>
            </div>
        </form>
    </div>
@endsection

@push('script_2')
    <script>
        "use strict";
        $('.js-select-all').on('change', function () {
            $('.js-settlement-row').prop('checked', $(this).is(':checked'));
        });
    </script>
@endpush
