@extends('layouts.admin.app')

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

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

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

        <div class="card">
            <div class="card-header border-0">
                <div class="search--button-wrapper">
                    <h4 class="card-title">{{ translate('Courier Account Setup') }} <span class="badge badge-soft-secondary">{{ $couriers->total() }}</span></h4>
                    <form class="search-form min--240">
                        <div class="input-group input--group">
                            <input type="search" name="search" value="{{ request('search') }}" class="form-control" placeholder="{{ translate('Search courier') }}">
                            <button class="btn btn--secondary"><i class="tio-search"></i></button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('Courier') }}</th>
                        <th>{{ translate('Provider/API') }}</th>
                        <th>{{ translate('Receivable Account') }}</th>
                        <th>{{ translate('Expense Accounts') }}</th>
                        <th class="text-right">{{ translate('Opening') }}</th>
                        <th class="text-right">{{ translate('Current Balance') }}</th>
                        <th>{{ translate('Status') }}</th>
                        <th class="text-center">{{ translate('Action') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($couriers as $courier)
                        @php
                            $openingBalance = (float) ($courier->opening_balance ?? 0);
                            $openingDirection = $openingBalance < 0 ? 'payable' : 'receivable';
                            $openingLabel = $openingDirection === 'payable' ? translate('Payable to courier') : translate('Receivable from courier');
                        @endphp
                        <tr>
                            <td>
                                <strong>{{ $courier->name }}</strong>
                                <div class="fs-12 text-muted">{{ $courier->provider_key }}</div>
                            </td>
                            <td>{{ $courier->api_type ?? translate('N/A') }}</td>
                            <td>{{ $courier->receivable_account_name }}</td>
                            <td>
                                <div>{{ $courier->charge_expense_account_name }}</div>
                                <div class="fs-12 text-muted">{{ $courier->return_charge_expense_account_name }} / {{ $courier->cod_charge_expense_account_name }}</div>
                            </td>
                            <td class="text-right">
                                {{ \App\CentralLogics\Helpers::format_currency($courier->opening_balance) }}
                                <div class="fs-12 text-muted">{{ $openingLabel }}</div>
                                @if($courier->opening_balance_synced_at)
                                    <div class="fs-12 text-success">{{ translate('Synced') }}: {{ $courier->opening_balance_synced_at?->format('d M Y') }}</div>
                                @endif
                            </td>
                            <td class="text-right font-weight-bold">{{ \App\CentralLogics\Helpers::format_currency($courier->current_balance) }}</td>
                            <td>
                                <span class="badge {{ $courier->is_active ? 'badge-soft-success' : 'badge-soft-secondary' }}">
                                    {{ $courier->is_active ? translate('Active') : translate('Inactive') }}
                                </span>
                            </td>
                            <td class="text-center">
                                <button class="btn btn-sm btn-outline-primary" data-toggle="collapse" data-target="#courier-edit-{{ $courier->id }}">
                                    <i class="tio-edit"></i>
                                </button>
                            </td>
                        </tr>
                        <tr class="collapse" id="courier-edit-{{ $courier->id }}">
                            <td colspan="8">
                                <form action="{{ route('admin.courier.accounts.update', $courier) }}" method="post">
                                    @csrf
                                    <div class="row g-3">
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Courier Name') }}</label>
                                            <input type="text" name="name" value="{{ $courier->name }}" class="form-control" required>
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('API Type') }}</label>
                                            <input type="text" name="api_type" value="{{ $courier->api_type }}" class="form-control">
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Receivable Account') }}</label>
                                            <input type="text" name="receivable_account_name" value="{{ $courier->receivable_account_name }}" class="form-control" required>
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Default Settlement Account') }}</label>
                                            <input type="text" name="default_settlement_account_name" value="{{ $courier->default_settlement_account_name }}" class="form-control" required>
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Courier Charge Expense') }}</label>
                                            <input type="text" name="charge_expense_account_name" value="{{ $courier->charge_expense_account_name }}" class="form-control" required>
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Return Charge Expense') }}</label>
                                            <input type="text" name="return_charge_expense_account_name" value="{{ $courier->return_charge_expense_account_name }}" class="form-control" required>
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('COD Charge Expense') }}</label>
                                            <input type="text" name="cod_charge_expense_account_name" value="{{ $courier->cod_charge_expense_account_name }}" class="form-control" required>
                                        </div>
                                        <div class="col-md-2">
                                            <label class="input-label">{{ translate('Opening Balance') }}</label>
                                            <input type="number" step="0.01" min="0" name="opening_balance" value="{{ abs((float) $courier->opening_balance) }}" class="form-control">
                                        </div>
                                        <div class="col-md-2">
                                            <label class="input-label">{{ translate('Opening Type') }}</label>
                                            <select name="opening_balance_direction" class="form-control">
                                                <option value="receivable" {{ $openingDirection === 'receivable' ? 'selected' : '' }}>{{ translate('Courier will pay me') }}</option>
                                                <option value="payable" {{ $openingDirection === 'payable' ? 'selected' : '' }}>{{ translate('I will pay courier') }}</option>
                                            </select>
                                        </div>
                                        <div class="col-md-2">
                                            <label class="input-label">{{ translate('Opening Date') }}</label>
                                            <input type="date" name="opening_balance_date" value="{{ $courier->opening_balance_date?->format('Y-m-d') }}" class="form-control">
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Opening Reference') }}</label>
                                            <input type="text" name="opening_balance_reference" value="{{ $courier->opening_balance_reference }}" class="form-control" placeholder="{{ translate('Old statement / note ref') }}">
                                        </div>
                                        <div class="col-md-3">
                                            <label class="input-label">{{ translate('Opening Note') }}</label>
                                            <input type="text" name="opening_balance_note" value="{{ $courier->opening_balance_note }}" class="form-control">
                                        </div>
                                        <div class="col-md-1 d-flex align-items-end">
                                            <label class="form-check form--check mb-2">
                                                <input type="checkbox" name="is_active" value="1" class="form-check-input" {{ $courier->is_active ? 'checked' : '' }}>
                                                <span class="form-check-label">{{ translate('Active') }}</span>
                                            </label>
                                        </div>
                                        <div class="col-12 text-right">
                                            <button class="btn btn--primary">{{ translate('Update Courier Account') }}</button>
                                        </div>
                                    </div>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="text-center py-4">{{ translate('No courier account found') }}</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($couriers->hasPages())
                <div class="card-footer">{!! $couriers->links() !!}</div>
            @endif
        </div>
    </div>
@endsection
