<div class="ticket-details-sidebar">
    <div class="ticket-details-sidebar-close">
        <i class="las la-times"></i>
    </div>
    <p class="ticket-details-sidebar-title"><?php echo app('translator')->get('Details'); ?></p>
    <div class="accordion">
        <div class="accordion ticket-action-accordion" id="ticket-sidebar-accordion">
            <div class="accordion-item">
                <h2 class="accordion-header">
                    <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#ticket-info" aria-expanded="false" aria-controls="ticket-info">
                        <span class="title">
                            <span class="icon">
                                <?php echo $__env->make('admin.partials.svg.info', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            </span>
                            <?php echo app('translator')->get('Ticket Info'); ?>
                        </span>
                    </button>
                </h2>
                <div id="ticket-info" class="accordion-collapse collapse" data-bs-parent="#ticket-sidebar-accordion">
                    <div class="accordion-body">
                        <div class="ticket-sidebar-info">
                            <div class="ticket-sidebar-info-item">
                                <span class="label"><?php echo app('translator')->get('Ticket ID'); ?>:</span>
                                <span class="value">
                                    <?php echo e($ticket->ticket_number); ?>

                                    <button class="copy-id copyBtn" data-ticket="<?php echo e($ticket->ticket_number); ?>">
                                        <i class="fa fa-copy"></i>
                                    </button>
                                </span>
                            </div>
                            <div class="ticket-sidebar-info-item">
                                <span class="label"><?php echo app('translator')->get('Created'); ?>:</span>
                                <span class="value">
                                    <?php echo e(showDateTime($ticket->created_at, 'd M Y')); ?>

                                </span>
                            </div>
                            <div class="ticket-sidebar-info-item">
                                <span class="label"><?php echo app('translator')->get('Customer Rating'); ?>:</span>
                                <span class="value">
                                    <?php if($ticket->customer_happiness): ?>
                                        <?php echo e(__($ticket->customer_happiness)); ?>

                                    <?php else: ?>
                                        <?php echo app('translator')->get('Not rated'); ?>
                                    <?php endif; ?>
                                </span>
                            </div>
                            <div class="ticket-sidebar-info-item">
                                <span class="label"><?php echo app('translator')->get('Last message'); ?>:</span>
                                <span class="value">
                                    <?php echo e(diffForHumans($ticket->replies->first()?->created_at)); ?>

                                </span>
                            </div>

                            <div class="ticket-sidebar-info-item">
                                <label for="department" class="form--label"><?php echo app('translator')->get('Department'); ?></label>
                                <select name="department" class="select2 style-two">
                                    <?php $__currentLoopData = $globalValues['departments']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $department): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($department->id); ?>"><?php echo e(__($department->name)); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                            <div class="ticket-sidebar-info-item">
                                <label for="admin" class="form--label"><?php echo app('translator')->get('Agent'); ?></label>
                                <select name="admin" class="select2 style-two">
                                    <option value="0"><?php echo app('translator')->get('Unassign'); ?></option>
                                    <?php $__currentLoopData = $globalValues['admins']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $admin): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($admin->id); ?>"><?php echo e($admin->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <div class="ticket-sidebar-info-item">
                                <label for="priority" class="form--label"><?php echo app('translator')->get('Priority'); ?></label>
                                <select name="priority" class="select2 style-two">
                                    <?php $__currentLoopData = $globalValues['priorities']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $priority): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($priority->id); ?>"><?php echo e(__($priority->name)); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <div class="ticket-sidebar-info-item">
                                <label for="status" class="form--label"><?php echo app('translator')->get('Status'); ?></label>
                                <select name="status" class="select2 style-two">
                                    <?php $__currentLoopData = $globalValues['statuses']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($status->id); ?>"><?php echo e(__($status->name)); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>

                            <div class="ticket-sidebar-info-item">
                                <label for="source" class="form--label"><?php echo app('translator')->get('Tags'); ?></label>
                                <div class="select2-group">
                                    <span class="icon">
                                        <?php echo $__env->make('admin.partials.svg.tag', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </span>

                                    <select name="tags[]" class="select2 style-two select2-multiple" multiple>
                                        <?php $__currentLoopData = $globalValues['tags']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tag): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($tag->id); ?>" <?php if($ticket->tags->contains($tag->id)): ?> selected <?php endif; ?>><?php echo e($tag->name); ?>

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

            <?php if($ticket->department_data): ?>
                <div class="accordion-item">
                    <h2 class="accordion-header">
                        <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#department-data" aria-expanded="false" aria-controls="department-data">
                            <span class="title">
                                <span class="icon">
                                    <?php echo $__env->make('admin.partials.svg.department', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                </span>
                                <?php echo app('translator')->get('Department Data'); ?>
                            </span>
                        </button>
                    </h2>
                    <div id="department-data" class="accordion-collapse collapse" data-bs-parent="#ticket-sidebar-accordion">
                        <div class="accordion-body">
                            <?php $__currentLoopData = $ticket->department_data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $deptData): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <div class="ticket-sidebar-info-item">
                                    <span class="label"><?php echo e(__($deptData->name)); ?>:</span>
                                    <span class="value">
                                        <?php if($deptData->type == 'checkbox'): ?>
                                            <?php echo e(implode(',', $deptData->value)); ?>

                                        <?php elseif($deptData->type == 'file'): ?>
                                            <?php if($deptData->value): ?>
                                                <a href="<?php echo e(route('admin.download.attachment', encrypt(getFilePath('verify') . '/' . $deptData->value))); ?>"><i class="fa-regular fa-file"></i> <?php echo app('translator')->get('Attachment'); ?> </a>
                                            <?php else: ?>
                                                <?php echo app('translator')->get('No File'); ?>
                                            <?php endif; ?>
                                        <?php else: ?>
                                            <?php echo e(__($deptData->value)); ?>

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

            <div class="accordion-item">
                <h2 class="accordion-header">
                    <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#customer-info" aria-expanded="false" aria-controls="customer-info">
                        <span class="title">
                            <span class="icon">
                                <?php echo $__env->make('admin.partials.svg.user', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            </span>
                            <?php echo app('translator')->get('Customer Info'); ?>
                        </span>
                    </button>
                </h2>
                <div id="customer-info" class="accordion-collapse collapse" data-bs-parent="#ticket-sidebar-accordion">
                    <div class="accordion-body">
                        <div class="ticket-sidebar-info-item">
                            <span class="label"><?php echo app('translator')->get('Name'); ?>:</span>
                            <span class="value"><?php echo e($ticket->user->fullname); ?></span>
                        </div>
                        <div class="ticket-sidebar-info-item">
                            <span class="label"><?php echo app('translator')->get('Email'); ?>:</span>
                            <span class="value"><?php echo e($ticket->user->email); ?></span>
                        </div>
                        <div class="ticket-sidebar-info-item">
                            <span class="label"><?php echo app('translator')->get('Mobile'); ?>:</span>
                            <span class="value"><?php echo e($ticket->user->mobile ?? '-'); ?></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="accordion-item">
                <h2 class="accordion-header">
                    <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#folder-action" aria-expanded="false" aria-controls="folder-action">
                        <span class="title">
                            <span class="icon">
                                <?php echo $__env->make('admin.partials.svg.folder', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            </span>
                            <?php echo app('translator')->get('Folders'); ?>
                        </span>
                    </button>
                </h2>
                <div id="folder-action" class="accordion-collapse collapse" data-bs-parent="#ticket-sidebar-accordion">
                    <div class="accordion-body">
                        <div class="ticket-sidebar-info">
                            <div class="ticket-sidebar-info-action">
                                <label for="primary" class="content">
                                    <span class="text--body">
                                        <?php echo $__env->make('admin.partials.svg.ticket', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </span>
                                    <?php echo app('translator')->get('Move to Primary'); ?>
                                </label>
                                <span class="action-checkbox">
                                    <div class="form--check">
                                        <input type="radio" name="folder" value="<?php echo e(Status::TICKET_PRIMARY); ?>" id="primary" class="form-check-input" <?php if($ticket->folder == Status::TICKET_PRIMARY): echo 'checked'; endif; ?> />
                                    </div>
                                </span>
                            </div>
                            <div class="ticket-sidebar-info-action">
                                <label for="draft" class="content">
                                    <span class="text--body">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-hand-icon lucide-hand">
                                            <path d="M18 11V6a2 2 0 0 0-2-2a2 2 0 0 0-2 2" />
                                            <path d="M14 10V4a2 2 0 0 0-2-2a2 2 0 0 0-2 2v2" />
                                            <path d="M10 10.5V6a2 2 0 0 0-2-2a2 2 0 0 0-2 2v8" />
                                            <path d="M18 8a2 2 0 1 1 4 0v6a8 8 0 0 1-8 8h-2c-2.8 0-4.5-.86-5.99-2.34l-3.6-3.6a2 2 0 0 1 2.83-2.82L7 15" />
                                        </svg>
                                    </span>
                                    <?php echo app('translator')->get('Move to Draft'); ?>
                                </label>
                                <span class="action-checkbox">
                                    <div class="form--check">
                                        <input type="radio" name="folder" value="<?php echo e(Status::TICKET_DRAFT); ?>" id="draft" class="form-check-input" <?php if($ticket->folder == Status::TICKET_DRAFT): echo 'checked'; endif; ?> />
                                    </div>
                                </span>
                            </div>
                            <div class="ticket-sidebar-info-action">
                                <label for="spam" class="content">
                                    <span class="text--warning">
                                        <?php echo $__env->make('admin.partials.svg.exclamation', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </span>
                                    <?php echo app('translator')->get('Move to Spam'); ?>
                                </label>
                                <span class="action-checkbox">
                                    <div class="form--check">
                                        <input type="radio" name="folder" value="<?php echo e(Status::TICKET_SPAM); ?>" id="spam" class="form-check-input" <?php if($ticket->folder == Status::TICKET_SPAM): echo 'checked'; endif; ?> />
                                    </div>
                                </span>
                            </div>
                            <div class="ticket-sidebar-info-action">
                                <label for="trash" class="content">
                                    <span class="text--danger">
                                        <?php echo $__env->make('admin.partials.svg.delete', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </span>
                                    <?php echo app('translator')->get('Move to Trash'); ?>
                                </label>
                                <span class="action-checkbox">
                                    <div class="form--check">
                                        <input type="radio" name="folder" value="<?php echo e(Status::TICKET_TRASH); ?>" id="trash" class="form-check-input" <?php if($ticket->folder == Status::TICKET_TRASH): echo 'checked'; endif; ?> />
                                    </div>
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php $__env->startPush('script'); ?>
    <script>
        (function($) {
            "use strict";

            $('.copyBtn').on('click', function() {
                var ticketId = $(this).data('ticket');
                navigator.clipboard.writeText(ticketId).then(function() {
                    notify('success', 'Ticket ID copied to clipboard!');
                }, function(err) {
                    notify('error', 'Failed to copy Ticket ID: ' + err);
                });
            });

            let statusId = `<?php echo e($ticket->ticket_status_id); ?>`;
            let priorityId = `<?php echo e($ticket->priority_id); ?>`;
            let departmentId = `<?php echo e($ticket->department_id); ?>`;
            let adminId = `<?php echo e($ticket->assign_staff_id); ?>`;
            let selectedTags = `<?php echo e(json_encode($ticket->tags->pluck('id')->toArray())); ?>`;
            selectedTags = JSON.parse(selectedTags);

            let folder = `<?php echo e($ticket->folder); ?>`;

            $('select[name="status"]').val(statusId).trigger('change');
            $('select[name="priority"]').val(priorityId).trigger('change');
            $('select[name="department"]').val(departmentId).trigger('change');
            $('select[name="admin"]').val(adminId).trigger('change');

            $('[name=status]').on('change', function() {
                statusId = $(this).val();
                saveTicketData();
            });

            $('[name=priority]').on('change', function() {
                priorityId = $(this).val();
                saveTicketData();
            });

            $('[name=department]').on('change', function() {
                departmentId = $(this).val();
                saveTicketData();
            });

            $('[name=admin]').on('change', function() {
                adminId = $(this).val();
                saveTicketData();
            });

            let oldTags = [];

            $('[name="tags[]"]').on('select2:close', function() {
                selectedTags = $(this).val() || [];
                if (JSON.stringify(oldTags) === JSON.stringify(selectedTags)) {
                    return;
                }
                oldTags = selectedTags;

                clearTimeout(window.tagsTimeout);
                window.tagsTimeout = setTimeout(function() {
                    saveTicketData();
                }, 2000);
            });

            $('[name=folder]').on('change', function() {
                folder = $(this).val();
                saveTicketData();
            });

            function saveTicketData() {
                $.ajax({
                    url: "<?php echo e(route('admin.ticket.update', $ticket->id)); ?>",
                    type: "POST",
                    data: {
                        _token: "<?php echo e(csrf_token()); ?>",
                        status: statusId,
                        priority: priorityId,
                        department: departmentId,
                        admin: adminId,
                        tags: selectedTags,
                        folder: folder
                    },
                    success: function(response) {
                        if (response.status === 'error') {
                            notify('error', response.message);
                            return;
                        }

                        $('.departmentName').text(response.data.ticket.department.name);
                        triggerDepartmentChange(response.data.ticket.department_id);
                        $('.adminName').text(response.data.ticket.admin?.name);
                        $('.priorityNameText').text(response.data.ticket.priority.name);
                        $('.statusName').text(response.data.ticket.ticket_status.name);
                        $('.ticketStatus')
                            .text(response.data.ticket.ticket_status.name)
                            .attr('data-hsl', JSON.stringify(response.data.ticket.ticket_status.converted_color));

                        $('.priorityName').attr('data-hsl_color', JSON.stringify(response.data.ticket.priority.converted_color));

                        let tagsHtml = response.data.ticket.tags
                            .map(tag =>
                                `<span class="tag-style tagStyle" data-converted_color='${JSON.stringify(tag.converted_color)}'>${tag.name}</span>`
                            )
                            .join(' ');
                        $('.ticketTags').html(tagsHtml);

                        applyBadgeColors();

                        let $adminSelect = $('select[name="admin"]');
                        $adminSelect.empty();
                        $adminSelect.append(`<option value=""><?php echo app('translator')->get('Select One'); ?></option>`);
                        response.data.admins.forEach(function(admin) {
                            $adminSelect.append(
                                `<option value="${admin.id}"${response.data.ticket.assign_staff_id == admin.id ? ' selected' : ''}>${admin.name}</option>`
                            );
                        });

                        adminId = response.data.ticket.assign_staff_id;

                        notify('success', response.message);
                    },
                    error: function(xhr) {
                        notify('error', 'Failed to update ticket: ' + xhr.responseJSON.message);
                    }
                });
            }

            function triggerDepartmentChange(departmentId) {
                $('.changeDepartment').each(function() {
                    if ($(this).data('department_id') == departmentId) {
                        $(this).trigger('click');
                        return false;
                    }
                });
            };

        })(jQuery);
    </script>
<?php $__env->stopPush(); ?>
<?php /**PATH C:\laragon\www\projects\custom_viserdesk\Files\core\resources\views/admin/ticket/sidebar.blade.php ENDPATH**/ ?>