
<?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 == 'user') ? 'selected' : ''); ?> value="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="all_user"><?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 rows="5" 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 === 'user'){
            $('.user_container').removeClass('d-none');
        }

        $('select[name="send_to"]').change(async function () {
            let userContainer = $('.user_container'),
                userOptions = $('select[name="user_id"]');

            if ($(this).val() === '') {
                userContainer.addClass('d-none');
                $('.user_container select').val('');
                userOptions.html(`<option value="all_user"><?php echo e(__('usersNotifications.all')); ?></option>`)
            }
            if ($(this).val() === 'user'){
                let data = {role: $(this).val()},
                    url = getUsersRoute,
                    method = 'get';

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

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

                userContainer.removeClass('d-none');

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

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

                if (response.users.length){
                    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 /home/balyomhost/public_html/legal/resources/views/admin/users_notifications/form.blade.php ENDPATH**/ ?>