<?php $__env->startSection('css'); ?>
    <link rel="stylesheet" href="<?php echo e(asset('assets/web/css/lib/toastr.css')); ?>" />
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
<div class="side-title">
    <h3><?php echo e($head ?? ''); ?></h3>
</div>

<div class="border-item">
    <form class="form-row send_notification_form" method="post" action="<?php echo e($action); ?>" enctype="multipart/form-data">
        <?php if($usersNotification ?? ''  && $usersNotification->id): ?>
            <?php echo method_field('PUT'); ?>
        <?php endif; ?>
        <?php echo csrf_field(); ?>
        <div class="form-group col-lg-12">
            <label><?php echo e(meta('send_to', getCurrentLocale()) ?? __('usersNotifications.send_to')); ?></label>
            <select name="send_to" class="form-control <?php echo e($errors->has('send_to') ? 'is-invalid' : ''); ?>">
                <option value=""><?php echo e(meta('send_to', getCurrentLocale()) ?? __('usersNotifications.send_to')); ?></option>
                <option <?php echo e(($role == 'all_user') ? 'selected' : ''); ?> value="all_user"><?php echo e(__('usersNotifications.user_name')); ?></option>


            </select>
        </div>
        <div class="form-group col-lg-12 <?php echo e((!$role) ? 'd-none' : ''); ?>  user_container">
            <label><?php echo e($role == 'all_user' ? __('usersNotifications.user') : __('usersNotifications.provider')); ?></label>
            <select name="user_id" class="form-control">
                <option value="<?php echo e($role); ?>"><?php echo e(__('usersNotifications.all' ?? '')); ?></option>
                <?php if(count($users)): ?>
                    <?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option <?php echo e(($usersNotification->user_id ?? null) == $user->id ? 'selected' : ''); ?> value="<?php echo e($user->id); ?>"><?php echo e($user->name); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <?php endif; ?>
            </select>
        </div>


        <div class="form-group col-lg-12">
            <label><?php echo e(meta('title', getCurrentLocale()) ?? __('usersNotifications.title')); ?> </label>
            <input type="text" class="form-control <?php echo e($errors->has('title') ? 'is-invalid' : ''); ?>" name="title" placeholder="<?php echo e(meta('title', getCurrentLocale()) ?? __('usersNotifications.title')); ?>" value='<?php echo e(old("title", $usersNotification->title ?? null)); ?>'/>
        </div>

        <div class="form-group col-lg-12">
            <label><?php echo e(meta('message', getCurrentLocale()) ?? __('usersNotifications.message')); ?></label>
            <textarea name="message" class="form-control editor <?php echo e($errors->has('message') ? 'is-invalid' : ''); ?>" placeholder="<?php echo e(meta('message', getCurrentLocale()) ?? __('usersNotifications.message')); ?>"><?php echo old("message", $usersNotification->message ?? null); ?></textarea>
        </div>

        <div class="text-center mt-5 col-12">
            <button class="butn send_notification_button"><span><?php echo e(__('dashboard.save')); ?></span></button>
        </div>
    </form>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('inner_js'); ?>
    <!-- Editor JS files -->


    <script>

        

        let getUsersRoute = "<?php echo e(route('usersNotifications.getUsers')); ?>"

    </script>


    <script src="<?php echo e(asset('assets/web/js/lib/toastr.min.js')); ?>"></script>
    <script>
        
    </script>

    <script>

        let selectedOption = $('select[name="send_to"] option:selected').val();
        if (selectedOption === '') {
            $('.user_container').addClass('d-none');
            $('.user_container select').val('');
        }

        if (selectedOption === 'all_user' || selectedOption === 'all_provider'){
            $('.user_container').removeClass('d-none');
        }

        $('select[name="send_to"]').change(async function () {
            if ($(this).val() === '') {
                $('.user_container').addClass('d-none');
                $('.user_container select').val('');
            }
            if ($(this).val() === 'all_user' ||$(this).val() === 'all_provider'){
                let data = {role: $(this).val()},
                    url = getUsersRoute,
                    method = 'get';

                const response=await ajax({
                    url,
                    data,
                    dataType: 'json',
                    method
                });

                if (response.errors){
                    toasterError(response.errors);
                }

                $('.user_container').removeClass('d-none');
                let userOptions = $('select[name="user_id"]');

                if (response.role === 'all_user'){
                    userOptions.html(`<option value="all_user"><?php echo e(__('usersNotifications.all')); ?></option>`);
                }
                if (response.role === 'all_provider'){
                    userOptions.html(`<option value="all_provider"><?php echo e(__('usersNotifications.all')); ?></option>`);
                }

                $('.user_container label').html(response.label);

                if (response.users){
                    for (let i = 0; i < response.users.length; i++){
                        userOptions.append(`<option value="`+response.users[i].id+`">`+response.users[i].name+`</option>`)
                    }
                }
            }
        });

        $('.send_notification_button').click(async function (e) {
            e.preventDefault();

            $(this).prop('disabled', true);

            let form = $('.send_notification_form'),
                data = new FormData(form[0]),
                url = form.attr('action'),
                method = form.attr('method');

            const response=await ajax({
                url,
                data,
                dataType: 'json',
                method
            });

            if (response.message){
                $(this).prop('disabled', false);
                $('input[name="title"], textarea[name="message"]').val('');
                toasterSuccess(response.message);
            }

            if (response.errors){
                $(this).prop('disabled', false);
                toasterError(response.errors);
            }


        });


    </script>

<?php $__env->stopSection(); ?>




<?php echo $__env->make('admin.layouts.container', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\wamp64\www\legal\resources\views/admin/users_notifications/form.blade.php ENDPATH**/ ?>