<?php $__env->startSection('title', 'CC'); ?>
<?php $__env->startPush('css'); ?>
    <link rel="stylesheet" href="<?php echo e(asset('/assets/css/datepicker.css')); ?>"/>
    <style>
        .is-invalid {
            border-color: var(--color-primary) !important;
        }
    </style>
<?php $__env->stopPush(); ?>
<?php $__env->startSection('content'); ?>
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="addAdmissionPage">
                    <div class="">
                        <h4 class="mb-1">Create CM</h4>
                        <nav style="--bs-breadcrumb-divider: '|';"  aria-label="breadcrumb" >
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="<?php echo e(route('cm.index')); ?>" class="text-primary">Community Mobiliser</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Create</li>
                            </ol>
                        </nav>
                    </div>


                    <form id="enquireForm" method="POST" action="<?php echo e(isset($user) ? route('cm.update',['id' => $user->community_id]) : route('cm.store')); ?>" enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>
                        <div class="mt-4">
                            <p class="color-primary small-text">CM info</p>
                            <div class="card myCard form-main-div">
                                <div class="row g-3">
                                    <div class="col-lg-6">
                                        <label for="name">Name<span class="required-sign ">*</span></label>
                                        <input type="text" class="form-control" id="name" name="name" value="<?php echo e($user->member_name ?? ''); ?>" placeholder="Name" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="name">Father Name<span class="required-sign ">*</span></label>
                                        <input type="text" class="form-control" id="father_name" name="father_name" value="<?php echo e($user->father_name ?? ''); ?>" placeholder="Father Name" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="phone-number">Phone Number<span class="required-sign ">*</span></label>
                                        <input type="text" class="form-control" name="phone_number" id="phone-number" value="<?php echo e($user->member_phone ?? ''); ?>" placeholder="Phone Number" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">Email</label>
                                        <input type="text" class="form-control" id="email" name="email" value="<?php echo e($user->member_email ?? ''); ?>" placeholder="Email ID" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">Member ID</label>
                                        <input type="text" class="form-control" id="member_sh_id" name="member_sh_id" value="<?php echo e($user->member_sh_id ?? ''); ?>" placeholder="Member ID" >
                                    </div>

                                    <div class="col-lg-6">
                                        <label for="email">DOB</label>
                                        <input type="text" class="form-control" id="dob" name="dob" value="<?php echo e($user->dob ?? ''); ?>" placeholder="DOB" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">DOJ</label>
                                        <input type="text" class="form-control" id="doj" name="doj" value="<?php echo e($user->doj ?? ''); ?>" placeholder="DOJ" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">Designation</label>
                                        <input type="text" class="form-control" id="designation" name="designation" value="<?php echo e($user->designation ?? ''); ?>" placeholder="Designation" >
                                    </div>




                                    <div class="col-lg-6">
                                        <label for="district">District<span class="required-sign ">*</span></label>
                                        <select name="district" class="form-select" id="district">
                                            <option value="" >--choose--</option>
                                            <?php $__currentLoopData = $districts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $district): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($district->id); ?>"
                                                    <?php echo e(isset($user) && $user->district_id == $district->id ? 'selected' : ''); ?>>
                                                    <?php echo e($district->district_name); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="source">Block <span class="required-sign ">*</span></label>
                                        <select name="block" class="form-select" id="block">
                                            <option value="" >--choose--</option>
                                            <?php $__currentLoopData = $blocks; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $block): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($block->block_id); ?>"
                                                    <?php echo e(isset($user) && $user->block_id == $block->block_id ? 'selected' : ''); ?>>
                                                    <?php echo e($block->block_name); ?>

                                                </option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">Panchayat</label>
                                        <input type="text" class="form-control" id="panchayat" name="panchayat" value="<?php echo e($user->panchayat ?? ''); ?>" placeholder="Panchayat" >
                                    </div>

                                    <div class="col-lg-6">
                                        <label for="email">Address</label>
                                        <input type="text" class="form-control" id="address" name="address" value="<?php echo e($user->address ?? ''); ?>" placeholder="Address" >
                                    </div>

                                    <div class="col-lg-6">
                                        <label for="email">Jivika Name</label>
                                        <input type="text" class="form-control" id="jivika" name="jivika" value="<?php echo e($user->jivika_name ?? ''); ?>" placeholder="Jivika Name" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">SHG ID</label>
                                        <input type="text" class="form-control" id="shg_id" name="shg_id" value="<?php echo e($user->shg_id ?? ''); ?>" placeholder="SHG ID" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="email">Photo</label>
                                        <input type="file" class="form-control" id="photo" name="photo" >
                                    </div>
                                    <div class="col-lg-6">
                                        <label for="status">Status</label>
                                        <select name="status" class="form-select" id="status">
                                            <?php $__currentLoopData = config('constant.STATUS'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $val): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <option value="<?php echo e($key); ?>" <?php echo e(isset($user) && $user->status == $key ? 'selected' :''); ?>><?php echo e($val); ?></option>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="mt-4">
                                <div>
                                    <button class="btn btn-secondary px-4">Save</button>
                                </div>
                            </div>

                        </div>
                    </form>
                </div>
            </div>
        </div>

        <?php $__env->startPush('scripts'); ?>
            <script src="<?php echo e(asset('/assets/js/datepicker/datepicker.js')); ?>"></script>
            <script src="<?php echo e(asset('/assets/js/datepicker/datepicker.en.js')); ?>"></script>
            <script>
                $(function () {
                    $("#doj").datepicker()
                    $("#dob").datepicker()
                })
                $(function () {
                    $('#district').on('change', function () {
                        var district = $(this).val();
                        let url = '<?php echo e(route('get.blocks.by.district', ['id' => '__id'])); ?>'.replace('__id', district);

                        $.get(url, function (data) {
                            var floorSelect = $('#block');
                            floorSelect.empty().append('<option value="">Select Block</option>');

                            data.forEach(function (floor) {
                                console.log(floor,"sdfghjk")
                                floorSelect.append('<option value="' + floor.block_id + '">' + floor.block_name + '</option>');
                            });
                        });
                    });
                })
                $(document).ready(function() {
                    $('#enquireForm').on('submit', function(e) {
                        e.preventDefault();
                        if (!validateBuildingForm()) {
                            return;
                        }
                        let form = $(this);
                        let url = form.attr('action');
                        let formData = new FormData(this);

                        $.ajax({
                            type: 'POST',
                            url: url,
                            data: formData,
                            contentType: false,
                            processData: false,
                            beforeSend: function () {
                                // optional: show loader or disable button
                            },
                            success: function(response) {
                                if (response.status =='success') {
                                    notification('success',response.message,'success',2000)
                                } else {
                                    notification('error',"Something went wrong.",'danger',2000)
                                }
                                setTimeout(function(){
                                    location.reload()
                                },2000)

                            },
                            error: function(xhr) {
                                let errors = xhr.responseJSON?.errors;
                                if (errors) {
                                    let errorMessages = Object.values(errors).flat().join('\n');
                                    notification('error',"Something went wrong.",'danger',2000)
                                } else {
                                    notification('error',"Something went wrong.",'danger',2000)
                                }
                            }
                        });
                    });
                });

                function validateBuildingForm() {
                    let error = false;
                    let errorMessages = [];

                    const fields = {
                        name: {
                            required: true,
                            message: "Name is required",
                            id: 'name'
                        },
                        phoneNumber: {
                            required: true,
                            message: "Phone Number is required",
                            id: 'phone-number'
                        },
                        district: {
                            required: true,
                            message: "District is required",
                            id: 'district'
                        },
                        block: {
                            required: true,
                            message: "Block is required",
                            id: 'block'
                        },
                        status: {
                            required: true,
                            message: "Status is required",
                            id: 'status'
                        },
                    };

                    for (const key in fields) {
                        const field = fields[key];
                        const el = document.getElementById(field.id);

                        if (el) {
                            const value = el.value.trim();
                            if (field.required && !value) {
                                error = true;
                                notification('',field.message,'danger',2000)
                                errorMessages.push(field.message);
                                el.classList.add('is-invalid');
                            } else {
                                el.classList.remove('is-invalid');
                            }
                        }
                    }

                    if (error) {
                        return false;
                    }

                    return true;
                }

            </script>
    <?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /var/www/html/nari/resources/views/admin/cm.blade.php ENDPATH**/ ?>