@extends('layouts.admin')

@section('title', $isEdit ? 'Edit Pelanggan' : 'Tambah Pelanggan')

@section('content')
<div class="max-w-3xl mx-auto">
    <a href="{{ route('admin.customers.index') }}" class="inline-flex items-center gap-1.5 text-sm text-gray-500 hover:text-gray-800 mb-5 transition">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
        </svg>
        Kembali ke daftar pelanggan
    </a>

    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
        <div class="bg-brand-700 px-6 py-5">
            <h1 class="text-white font-bold text-lg">{{ $isEdit ? 'Edit Pelanggan' : 'Tambah Pelanggan Baru' }}</h1>
            <p class="text-brand-200 text-sm mt-0.5">{{ $isEdit ? 'Perbarui data master pelanggan.' : 'Form ini dapat digunakan admin atau kasir untuk registrasi pelanggan baru.' }}</p>
        </div>

        <form method="POST" action="{{ $isEdit ? route('admin.customers.update', $customer) : route('admin.customers.store') }}" class="p-6 space-y-5">
            @csrf
            @if($isEdit)
            @method('PATCH')
            @endif

            <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-1.5">Nama Pelanggan <span class="text-red-500">*</span></label>
                    <input type="text" name="name" value="{{ old('name', $customer->name) }}"
                           class="w-full rounded-xl border px-4 py-2.5 text-sm {{ $errors->has('name') ? 'border-red-400 bg-red-50' : 'border-gray-200' }} focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    @error('name')<p class="mt-1 text-xs text-red-500">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-1.5">WhatsApp</label>
                    <input type="text" name="whatsapp" value="{{ old('whatsapp', $customer->whatsapp) }}"
                           class="w-full rounded-xl border px-4 py-2.5 text-sm {{ $errors->has('whatsapp') ? 'border-red-400 bg-red-50' : 'border-gray-200' }} focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    @error('whatsapp')<p class="mt-1 text-xs text-red-500">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-1.5">Email</label>
                    <input type="email" name="email" value="{{ old('email', $customer->email) }}"
                           class="w-full rounded-xl border px-4 py-2.5 text-sm {{ $errors->has('email') ? 'border-red-400 bg-red-50' : 'border-gray-200' }} focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    @error('email')<p class="mt-1 text-xs text-red-500">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-semibold text-gray-700 mb-1.5">NPWP / Tax Number</label>
                    <input type="text" name="tax_number" value="{{ old('tax_number', $customer->tax_number) }}"
                           class="w-full rounded-xl border px-4 py-2.5 text-sm {{ $errors->has('tax_number') ? 'border-red-400 bg-red-50' : 'border-gray-200' }} focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    @error('tax_number')<p class="mt-1 text-xs text-red-500">{{ $message }}</p>@enderror
                </div>
            </div>

            <div>
                <label class="block text-sm font-semibold text-gray-700 mb-1.5">Alamat</label>
                <textarea name="address" rows="4"
                          class="w-full rounded-xl border px-4 py-2.5 text-sm {{ $errors->has('address') ? 'border-red-400 bg-red-50' : 'border-gray-200' }} focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">{{ old('address', $customer->address) }}</textarea>
                @error('address')<p class="mt-1 text-xs text-red-500">{{ $message }}</p>@enderror
            </div>

            @if($isEdit && auth()->user()?->isAdmin())
            <label class="inline-flex items-center gap-2 text-sm font-medium text-gray-700">
                <input type="checkbox" name="is_active" value="1" @checked(old('is_active', $customer->is_active)) class="rounded border-gray-300 text-brand-600 focus:ring-brand-500">
                Pelanggan aktif
            </label>
            @endif

            <div class="flex items-center justify-end gap-2 pt-2">
                <a href="{{ route('admin.customers.index') }}" class="px-4 py-2.5 rounded-xl border border-gray-300 text-sm font-medium text-gray-700 hover:bg-gray-50 transition">Batal</a>
                <button type="submit" class="px-4 py-2.5 rounded-xl bg-brand-600 hover:bg-brand-700 text-white text-sm font-semibold transition">
                    {{ $isEdit ? 'Simpan Perubahan' : 'Simpan Pelanggan' }}
                </button>
            </div>
        </form>
    </div>
</div>
@endsection