<?php $editing = !empty($vendor); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6"><a href="?page=vendors" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" 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></a><h1 class="text-2xl font-bold text-gray-900"><?= $editing ? 'Edit Vendor' : 'Add Vendor' ?></h1></div>
    <form method="POST" action="?page=vendors&action=<?= $editing ? 'update&id='.$vendor['id'] : 'store' ?>" class="space-y-6"><?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Company Name <span class="text-red-500">*</span></label><input type="text" name="company_name" value="<?= htmlspecialchars(old('company_name', $vendor['company_name'] ?? '')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Contact Name</label><input type="text" name="contact_name" value="<?= htmlspecialchars(old('contact_name', $vendor['contact_name'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Email <span class="text-red-500">*</span></label><input type="email" name="email" value="<?= htmlspecialchars(old('email', $vendor['email'] ?? '')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Phone</label><input type="text" name="phone" value="<?= htmlspecialchars(old('phone', $vendor['phone'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Service Category</label>
                    <select name="service_category" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach (['plumbing','electrical','hvac','appliance','pest_control','general','painting','flooring','cleaning','landscaping','roofing','other'] as $c): ?><option value="<?= $c ?>" <?= old('service_category', $vendor['service_category'] ?? '') === $c ? 'selected' : '' ?>><?= ucwords(str_replace('_',' ',$c)) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">W-9 Status</label>
                    <select name="w9_status" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['pending'=>'Pending','received'=>'Received','expired'=>'Expired'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('w9_status', $vendor['w9_status'] ?? 'pending') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Insurance Expiry</label><input type="date" name="insurance_expiry" value="<?= old('insurance_expiry', $vendor['insurance_expiry'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Rating (1-5)</label><input type="number" name="rating" value="<?= old('rating', $vendor['rating'] ?? '') ?>" min="1" max="5" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Address</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div class="md:col-span-2"><input type="text" name="address" value="<?= htmlspecialchars(old('address', $vendor['address'] ?? '')) ?>" placeholder="Street Address" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><input type="text" name="city" value="<?= htmlspecialchars(old('city', $vendor['city'] ?? '')) ?>" placeholder="City" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div class="grid grid-cols-2 gap-4"><input type="text" name="state" value="<?= htmlspecialchars(old('state', $vendor['state'] ?? '')) ?>" placeholder="State" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><input type="text" name="zip" value="<?= htmlspecialchars(old('zip', $vendor['zip'] ?? '')) ?>" placeholder="ZIP" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
            </div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6"><label class="block text-sm font-medium text-gray-700 mb-1">Notes</label><textarea name="notes" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><?= htmlspecialchars(old('notes', $vendor['notes'] ?? '')) ?></textarea></div>
        <div class="flex justify-end gap-3"><a href="?page=vendors" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"><?= $editing ? 'Update' : 'Add Vendor' ?></button></div>
    </form>
</div>
