@extends('admin.layouts.app')

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/affiliates.title')"
        :subtitle="__('admin/affiliates.subtitle')"
        icon="fas fa-handshake"
        :buttons="[
            ['label' => __('admin/affiliates.payouts_title'), 'route' => route('admin.affiliates.payouts'), 'icon' => 'fas fa-money-bill-wave', 'color' => 'outline-secondary'],
            ['label' => __('admin/affiliates.reports_title'), 'route' => route('admin.affiliates.reports'), 'icon' => 'fas fa-chart-bar', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.card :padding="false">
        <x-admin.data-table id="affiliates-table" :records-url="route('api.admin.affiliates.records')">
            <th style="width: 60px">{{ __('admin/affiliates.column_id') }}</th>
            <th>{{ __('admin/affiliates.column_client') }}</th>
            <th>{{ __('admin/affiliates.column_referral_code') }}</th>
            <th class="text-center">{{ __('admin/affiliates.column_status') }}</th>
            <th class="text-end">{{ __('admin/affiliates.column_total_earned') }}</th>
            <th class="text-end">{{ __('admin/affiliates.column_pending_balance') }}</th>
            <th class="text-center">{{ __('admin/affiliates.column_conversions') }}</th>
            <th>{{ __('admin/affiliates.column_created_at') }}</th>
            <th class="text-end pe-4" style="width: 140px">{{ __('admin/affiliates.column_actions') }}</th>
        </x-admin.data-table>
    </x-admin.card>
</div>

<x-admin.modal id="rejectModal" :title="__('admin/affiliates.reject_modal_title')">
    <input type="hidden" id="rejectAffiliateId">
    <div class="mb-3">
        <label for="rejectReason" class="form-label">{{ __('admin/affiliates.reject_modal_reason') }}</label>
        <textarea class="form-control" id="rejectReason" rows="3" placeholder="{{ __('admin/affiliates.reject_modal_reason_placeholder') }}"></textarea>
    </div>
    <x-slot:footer>
        <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">{{ __('admin/affiliates.reject_modal_cancel') }}</button>
        <button type="button" class="btn btn-danger btn-sm" id="confirmReject">{{ __('admin/affiliates.reject_modal_submit') }}</button>
    </x-slot:footer>
</x-admin.modal>
@endsection

@push('scripts')
<script>
    window.affiliateConfig = {
        routes: {
            records: '{{ route("api.admin.affiliates.records") }}',
            approve: '{{ route("api.admin.affiliates.approve", ":id") }}',
            reject: '{{ route("api.admin.affiliates.reject", ":id") }}',
            suspend: '{{ route("api.admin.affiliates.suspend", ":id") }}',
            activate: '{{ route("api.admin.affiliates.activate", ":id") }}',
            show: '{{ route("admin.affiliates.show", ":id") }}'
        },
        translations: {
            status_pending: '{{ __("admin/affiliates.status_pending") }}',
            status_active: '{{ __("admin/affiliates.status_active") }}',
            status_rejected: '{{ __("admin/affiliates.status_rejected") }}',
            status_suspended: '{{ __("admin/affiliates.status_suspended") }}',
            button_view: '{{ __("admin/affiliates.button_view") }}',
            button_approve: '{{ __("admin/affiliates.button_approve") }}',
            button_reject: '{{ __("admin/affiliates.button_reject") }}',
            button_suspend: '{{ __("admin/affiliates.button_suspend") }}',
            button_activate: '{{ __("admin/affiliates.button_activate") }}',
            messages_confirm_approve: '{{ __("admin/affiliates.messages_confirm_approve") }}',
            messages_confirm_suspend: '{{ __("admin/affiliates.messages_confirm_suspend") }}',
            messages_confirm_activate: '{{ __("admin/affiliates.messages_confirm_activate") }}',
            filter_all: '{{ __("admin/affiliates.filter_all") }}',
            filter_status: '{{ __("admin/affiliates.filter_status") }}',
            search_placeholder: '{{ __("admin/common.search_placeholder") }}'
        }
    };

    window.permissions = {
        affiliates: {
            view: {{ json_encode(auth()->user()->can('view affiliates')) }},
            manage: {{ json_encode(auth()->user()->can('manage affiliates')) }}
        }
    };
</script>
@vite('resources/js/admin/affiliates/index.js')
@endpush
