@extends('layouts.admin', ['title' => ($coupon->exists ? 'تعديل كوبون' : 'كوبون جديد').' | RaseedNow'])

@section('content')
    <section class="lc-page-shell">
        <div class="section-heading">
            <div>
                <span>Coupon</span>
                <h1>{{ $coupon->exists ? 'تعديل كوبون' : 'كوبون جديد' }}</h1>
            </div>
            <a class="secondary-button" href="{{ route('admin.coupons.index') }}">كل الكوبونات</a>
        </div>

        @if(session('status'))
            <div class="mt-4 rounded border border-green-200 bg-green-50 p-3 text-sm text-green-700">{{ session('status') }}</div>
        @endif
        @if($errors->any())
            <div class="mt-4 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-700">{{ $errors->first() }}</div>
        @endif

        <form class="surface mt-6 grid gap-4 p-5" method="POST" action="{{ $coupon->exists ? route('admin.coupons.update', $coupon) : route('admin.coupons.store') }}">
            @csrf
            @if($coupon->exists)
                @method('PATCH')
            @endif

            <div class="grid gap-4 md:grid-cols-3">
                <label class="form-field">
                    <span>الكود</span>
                    <input name="code" value="{{ old('code', $coupon->code) }}" required placeholder="WELCOME10">
                </label>
                <label class="form-field md:col-span-2">
                    <span>الاسم العربي</span>
                    <input name="name_ar" value="{{ old('name_ar', $coupon->name_ar) }}" required>
                </label>
            </div>

            <div class="grid gap-4 md:grid-cols-4">
                <label class="form-field">
                    <span>النوع</span>
                    <select name="type">
                        <option value="fixed" @selected(old('type', $coupon->type) === 'fixed')>مبلغ ثابت</option>
                        <option value="percent" @selected(old('type', $coupon->type) === 'percent')>نسبة مئوية</option>
                    </select>
                </label>
                <label class="form-field">
                    <span>قيمة ثابتة بالقروش</span>
                    <input name="value_minor" type="number" min="0" value="{{ old('value_minor', $coupon->value_minor) }}">
                </label>
                <label class="form-field">
                    <span>نسبة الخصم</span>
                    <input name="percent_off" type="number" min="0" max="100" value="{{ old('percent_off', $coupon->percent_off) }}">
                </label>
                <label class="form-field">
                    <span>أقصى خصم بالقروش</span>
                    <input name="max_discount_minor" type="number" min="0" value="{{ old('max_discount_minor', $coupon->max_discount_minor) }}">
                </label>
            </div>

            <div class="grid gap-4 md:grid-cols-4">
                <label class="form-field">
                    <span>حد أدنى بالقروش</span>
                    <input name="min_subtotal_minor" type="number" min="0" value="{{ old('min_subtotal_minor', $coupon->min_subtotal_minor) }}">
                </label>
                <label class="form-field">
                    <span>عدد الاستخدامات</span>
                    <input name="usage_limit" type="number" min="1" value="{{ old('usage_limit', $coupon->usage_limit) }}">
                </label>
                <label class="form-field">
                    <span>يبدأ في</span>
                    <input name="starts_at" type="datetime-local" value="{{ old('starts_at', $coupon->starts_at?->format('Y-m-d\TH:i')) }}">
                </label>
                <label class="form-field">
                    <span>ينتهي في</span>
                    <input name="ends_at" type="datetime-local" value="{{ old('ends_at', $coupon->ends_at?->format('Y-m-d\TH:i')) }}">
                </label>
            </div>

            <div class="flex flex-wrap gap-6 rounded-2xl bg-[#f7f8fb] p-4">
                <label class="inline-flex items-center gap-2 text-sm font-black text-[#17264d]">
                    <input name="is_active" value="1" type="checkbox" @checked(old('is_active', $coupon->is_active))>
                    مفعل
                </label>
                <label class="inline-flex items-center gap-2 text-sm font-black text-[#17264d]">
                    <input name="once_per_customer" value="1" type="checkbox" @checked(old('once_per_customer', $coupon->once_per_customer))>
                    مرة لكل عميل
                </label>
            </div>

            <button class="primary-button w-full md:w-fit" type="submit">حفظ الكوبون</button>
        </form>
    </section>
@endsection
