

<?php $__env->startSection('title', 'নতুন সীট যোগ করুন'); ?>

<?php $__env->startSection('content'); ?>
    <div class="min-h-screen bg-gray-50 p-4 sm:p-6">
        <div class="max-w-4xl mx-auto">

            <div class="mb-6 flex items-center justify-between">
                <div>
                    <h1 class="text-2xl font-bold text-gray-900">নতুন সীট যোগ করুন</h1>
                    <p class="text-sm text-gray-600">সীটের তথ্য পূরণ করুন</p>
                </div>
                <a href="<?php echo e(route('seats.index')); ?>" class="px-4 py-2 border rounded-lg hover:bg-gray-50">
                    সীট তালিকা
                </a>
            </div>

            <?php if($errors->any()): ?>
                <div class="mb-4 p-3 rounded-lg bg-red-50 border border-red-200 text-red-800">
                    <ul class="list-disc pl-5 text-sm">
                        <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <li><?php echo e($error); ?></li>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </ul>
                </div>
            <?php endif; ?>

            <div class="bg-white border rounded-xl p-6">
                <form action="<?php echo e(route('seats.store')); ?>" method="POST" class="space-y-6">
                    <?php echo csrf_field(); ?>

                    
                    <div>
                        <h2 class="text-lg font-semibold mb-3">সীট তথ্য</h2>

                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="text-sm font-medium">সীট নাম (বাংলা) *</label>
                                <input name="name_bn" value="<?php echo e(old('name_bn')); ?>" required
                                    class="w-full px-4 py-2 border rounded-lg">
                            </div>

                            <div>
                                <label class="text-sm font-medium">সীট নাম (English)</label>
                                <input name="name_en" value="<?php echo e(old('name_en')); ?>"
                                    class="w-full px-4 py-2 border rounded-lg">
                            </div>

                            <div>
                                <label class="text-sm font-medium">সীট কোড</label>
                                <input name="seat_code" value="<?php echo e(old('seat_code')); ?>" placeholder="DHA-08"
                                    class="w-full px-4 py-2 border rounded-lg">
                            </div>

                            <div>
                                <label class="text-sm font-medium">সীটের ধরন *</label>
                                <select name="seat_type" required class="w-full px-4 py-2 border rounded-lg bg-white">
                                    <option value="">নির্বাচন করুন</option>
                                    <option value="parliament" <?php echo e(old('seat_type') == 'parliament' ? 'selected' : ''); ?>>জাতীয় সংসদ
                                    </option>
                                    <option value="local" <?php echo e(old('seat_type') == 'local' ? 'selected' : ''); ?>>স্থানীয়</option>
                                    <option value="city_corp" <?php echo e(old('seat_type') == 'city_corp' ? 'selected' : ''); ?>>সিটি
                                        কর্পোরেশন</option>
                                    <option value="pouroshova" <?php echo e(old('seat_type') == 'pouroshova' ? 'selected' : ''); ?>>পৌরসভা
                                    </option>
                                    <option value="union" <?php echo e(old('seat_type') == 'union' ? 'selected' : ''); ?>>ইউনিয়ন</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    
                    <div>
                        <h2 class="text-lg font-semibold mb-3">লোকেশন</h2>

                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <label class="text-sm font-medium">জেলা *</label>
                                <select name="district_id" id="district_id" required
                                    class="w-full px-4 py-2 border rounded-lg bg-white">
                                    <option value="">নির্বাচন করুন</option>
                                    <?php $__currentLoopData = $districts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($d->id); ?>"
                                            <?php echo e(old('district_id') == $d->id ? 'selected' : ''); ?>>
                                            <?php echo e($d->name_bn); ?>

                                        </option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <div>
                                <label class="text-sm font-medium">উপজেলা</label>
                                <select name="upazila_id" id="upazila_id"
                                    class="w-full px-4 py-2 border rounded-lg bg-white">
                                    <option value="">জেলা নির্বাচন করুন</option>
                                </select>
                            </div>

                            <div>
                                <label class="text-sm font-medium">সিটি</label>
                                <select name="city_id" id="city_id" class="w-full px-4 py-2 border rounded-lg bg-white">
                                    <option value="">জেলা নির্বাচন করুন</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    
                    <div>
                        <h2 class="text-lg font-semibold mb-3">ভোটার সংখ্যা</h2>

                        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                            <div>
                                <p>পুরুষ</p>
                                <input type="number" id="male" name="male_voters" min="0"
                                    value="<?php echo e(old('male_voters', 0)); ?>" placeholder="পুরুষ"
                                    class="px-4 py-2 border rounded-lg">
                            </div>
                            <div>
                                <p>নারী</p>

                                <input type="number" id="female" name="female_voters" min="0"
                                    value="<?php echo e(old('female_voters', 0)); ?>" placeholder="নারী"
                                    class="px-4 py-2 border rounded-lg">
                            </div>

                            <div>
                                <p>অন্যান্য</p>

                                <input type="number" id="other" name="other_voters" min="0"
                                    value="<?php echo e(old('other_voters', 0)); ?>" placeholder="অন্যান্য"
                                    class="px-4 py-2 border rounded-lg">

                            </div>

                            <div>
                                <p>মোট</p>
                                <input type="number" id="total" name="total_voters" readonly
                                class="px-4 py-2 border rounded-lg bg-gray-100" placeholder="মোট">
                            </div>


                            
                        </div>
                    </div>

                    <div>
                        <label class="text-sm font-medium">বিবরণ</label>
                        <textarea name="description" rows="3" class="w-full px-4 py-2 border rounded-lg"><?php echo e(old('description')); ?></textarea>
                    </div>

                    <div>
                        <label class="text-sm font-medium">স্ট্যাটাস</label>
                        <select name="status" class="w-56 px-4 py-2 border rounded-lg bg-white">
                            <option value="active">সক্রিয়</option>
                            <option value="inactive">নিষ্ক্রিয়</option>
                        </select>
                    </div>

                    <div class="flex justify-end gap-3 pt-4 border-t">
                        <a href="<?php echo e(route('seats.index')); ?>" class="px-5 py-2 border rounded-lg">বাতিল</a>
                        <button class="px-5 py-2 bg-green-600 text-white rounded-lg">
                            সীট তৈরি করুন
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        /* ================= Auto total voters ================= */
        function calcTotal() {
            const m = parseInt(document.getElementById('male').value) || 0;
            const f = parseInt(document.getElementById('female').value) || 0;
            const o = parseInt(document.getElementById('other').value) || 0;
            document.getElementById('total').value = m + f + o;
        }
        ['male', 'female', 'other'].forEach(id => {
            document.getElementById(id).addEventListener('input', calcTotal);
        });
        calcTotal();

        /* ================= SAFE JSON FETCH (fixes a[ ... ]) ================= */
        async function fetchJsonSafe(url) {
            const res = await fetch(url, {
                headers: {
                    'Accept': 'application/json'
                }
            });
            const text = await res.text();
            const start = text.search(/[\[{]/);
            if (start === -1) throw text;
            return JSON.parse(text.slice(start));
        }

        /* ================= Dependent dropdowns ================= */
        const districtEl = document.getElementById('district_id');
        const upazilaEl = document.getElementById('upazila_id');
        const cityEl = document.getElementById('city_id');

        async function loadUpazilas(districtId) {
            if (!districtId) return;
            upazilaEl.innerHTML = '<option>লোড হচ্ছে...</option>';
            const data = await fetchJsonSafe(`/api/districts/${districtId}/upazilas`);
            upazilaEl.innerHTML = '<option value="">নির্বাচন করুন</option>';
            data.forEach(u => {
                upazilaEl.innerHTML += `<option value="${u.id}">${u.name_bn}</option>`;
            });
        }

        async function loadCities(districtId) {
            if (!districtId) return;
            cityEl.innerHTML = '<option>লোড হচ্ছে...</option>';
            const data = await fetchJsonSafe(`/api/districts/${districtId}/cities`);
            cityEl.innerHTML = '<option value="">নির্বাচন করুন</option>';
            data.forEach(c => {
                cityEl.innerHTML += `<option value="${c.id}">${c.name_bn}</option>`;
            });
        }

        districtEl.addEventListener('change', () => {
            loadUpazilas(districtEl.value);
            loadCities(districtEl.value);
        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layout.main', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Projects\election\resources\views/seats/create.blade.php ENDPATH**/ ?>