@extends('admin.layouts.layout')
@section('title', 'کدهای تخفیف')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1">کدهای تخفیف</h4>
            <p class="text-muted mb-0 small">مدیریت کدهای تخفیف فروشگاه</p>
        </div>
        <a href="{{ route('admin.discount-codes.create') }}" class="btn btn-primary">
            <i class="ti ti-plus me-1"></i>کد جدید
        </a>
    </div>

    @if(session('success'))
    <div class="alert alert-success alert-dismissible mb-4" role="alert">
        {{ session('success') }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    @endif

    <div class="card">
        <div class="card-body p-0">
            @if($codes->isEmpty())
            <div class="text-center py-5 text-muted">
                <i class="ti ti-tag-off ti-lg mb-2 d-block"></i>
                هیچ کد تخفیفی تعریف نشده است.
            </div>
            @else
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>کد</th>
                            <th>تخفیف</th>
                            <th>محدوده</th>
                            <th>استفاده</th>
                            <th>اعتبار</th>
                            <th>وضعیت</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($codes as $code)
                        <tr>
                            <td>
                                <div class="d-flex align-items-center gap-2">
                                    <code class="dc-code-badge">{{ $code->code }}</code>
                                    <button type="button" class="btn btn-icon btn-xs btn-outline-secondary copy-code-btn"
                                            data-code="{{ $code->code }}" title="کپی">
                                        <i class="ti ti-copy ti-xs"></i>
                                    </button>
                                </div>
                            </td>
                            <td>
                                @if($code->type === 'percent')
                                <span class="badge bg-label-info">{{ $code->value }}٪</span>
                                @else
                                <span class="badge bg-label-primary">{{ number_format($code->value) }} تومان</span>
                                @endif
                                @if($code->min_order)
                                <div class="text-muted small mt-1">حداقل: {{ number_format($code->min_order) }} تومان</div>
                                @endif
                            </td>
                            <td>
                                <span class="font-13">{{ $code->scopeLabel() }}</span>
                                @if($code->scope === 'user' && $code->user)
                                <div class="text-muted small">{{ $code->user->first_name }} {{ $code->user->last_name }}</div>
                                @endif
                                @if($code->scope === 'products')
                                <div class="text-muted small">{{ $code->products()->count() }} محصول</div>
                                @endif
                            </td>
                            <td>
                                <span class="fw-medium">{{ number_format($code->used_count) }}</span>
                                @if($code->max_uses)
                                <span class="text-muted"> / {{ number_format($code->max_uses) }}</span>
                                @endif
                            </td>
                            <td>
                                @if($code->starts_at || $code->expires_at)
                                <div class="font-13">
                                    @if($code->starts_at)
                                    <div class="text-muted">از: <span class="font-monospace">{{ $code->starts_at }}</span></div>
                                    @endif
                                    @if($code->expires_at)
                                    <div class="text-muted">تا: <span class="font-monospace {{ $code->expires_at < \App\Models\DiscountCode::todayJalali() ? 'text-danger' : '' }}">{{ $code->expires_at }}</span></div>
                                    @endif
                                </div>
                                @else
                                <span class="text-muted small">نامحدود</span>
                                @endif
                            </td>
                            <td>
                                <form method="POST" action="{{ route('admin.discount-codes.toggle', $code) }}">
                                    @csrf
                                    <button type="submit" class="badge border-0 {{ $code->is_active ? 'bg-label-success' : 'bg-label-secondary' }}"
                                            title="کلیک برای تغییر">
                                        {{ $code->is_active ? 'فعال' : 'غیرفعال' }}
                                    </button>
                                </form>
                            </td>
                            <td>
                                <div class="d-flex gap-1">
                                    <a href="{{ route('admin.discount-codes.edit', $code) }}"
                                       class="btn btn-icon btn-sm btn-outline-primary" title="ویرایش">
                                        <i class="ti ti-edit ti-xs"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.discount-codes.destroy', $code) }}"
                                          onsubmit="return confirm('آیا از حذف این کد تخفیف اطمینان دارید؟')">
                                        @csrf @method('DELETE')
                                        <button type="submit" class="btn btn-icon btn-sm btn-outline-danger" title="حذف">
                                            <i class="ti ti-trash ti-xs"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @endif
        </div>
    </div>

</div>
@endsection

@section('customjs')
<script>
$(function () {
    $(document).on('click', '.copy-code-btn', function () {
        var code = $(this).data('code');
        navigator.clipboard.writeText(code).then(() => {
            var $btn = $(this);
            $btn.html('<i class="ti ti-check ti-xs text-success"></i>');
            setTimeout(() => $btn.html('<i class="ti ti-copy ti-xs"></i>'), 1500);
        });
    });
});
</script>

<style>
.dc-code-badge {
    background: var(--bs-secondary-bg);
    color: var(--bs-emphasis-color);
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 0.85rem;
    letter-spacing: 0.05em;
}
</style>
@endsection
