answer_to_admin_chat.blade.php 2.21 KB
<div class="hide">
    <div id="answer_to_admin_chat_modal" class="modal-dialog">
        <div class="modal-dialog-title">
            <h2>Ответить</h2>
        </div>
        <div class="modal-dialog-body">
            <textarea class="textarea" name="text" placeholder="" required></textarea>
        </div>
        <div class="modal-dialog-footer">
            <button type="button" class="button answer_to_admin_chat_modal">Отправить</button>
            <button type="button" class="button button_light" onclick="$.fancybox.close();">Закрыть</button>
        </div>
    </div>
</div>

<script>
    $(function (){
        $('.admin-chat-answer').on('click', function () {
            var button = $(this);
            var modal = $('#answer_to_admin_chat_modal');
            modal.data('to-user-id', button.data('to-user-id'));
            modal.data('message-id', button.data('message-id'));
        });

        $('.answer_to_admin_chat_modal').click(function(){
            spinStart($(this));
            var wrap = $(this).closest('#answer_to_admin_chat_modal');
            console.log(wrap);
            var to_user_id = wrap.data('to-user-id');
            var reply_message_id = wrap.data('message-id');
            var textarea = wrap.find('[name="text"]');
            var text = textarea.val()
            textarea.removeClass('border-red');
            if (!text.trim()){
                textarea.addClass('border-red');
                return;
            }

            $.ajax({
                type: "POST",
                url: "{{ route('employer.send_message')  }}",
                data: {
                    to_user_id: to_user_id,
                    reply_message_id: reply_message_id,
                    text: text
                },
                dataType: 'json',
                headers: {
                    'X-CSRF-TOKEN': $('[name="_token"]').val()
                },
                success: function(res){
                    if (res && res.success && res.url_redirect){
                        window.location.replace(res.url_redirect);
                    } else{
                        location.reload();
                    }
                }
            });
        });
    });
</script>