Files
2026-09-15 10:35:56 +08:00

74 lines
5.9 KiB
PHTML

<?php
/** @var \Esun\CustomerMessage\Block\Account\Messages $block */
/** @var \Magento\Framework\Escaper $escaper */
$threads = $block->getThreads();
$thread = $block->getSelectedThread();
$entries = $block->getConversationMessages($thread);
$orderId = $block->getOrderId();
$conversationOrderId = $orderId ?: (int)($thread['order_id'] ?? 0);
$orderIncrementId = (string)($thread['order_increment_id'] ?? $block->getOrderIncrementId());
$isNewGeneral = !$orderId && !$thread;
?>
<div class="esun-conversation-layout <?= $orderId ? 'is-order-conversation' : '' ?>">
<?php if (!$orderId): ?>
<aside class="esun-conversation-sidebar">
<a class="action primary esun-conversation-new" href="<?= $escaper->escapeUrl($block->getUrl('customer-message/account/index', ['new' => 1])) ?>"><?= $escaper->escapeHtml(__('New Message')) ?></a>
<div class="esun-conversation-thread-list">
<?php foreach ($threads as $item): ?>
<a class="esun-conversation-thread <?= $thread && (int)$thread['thread_id'] === (int)$item['thread_id'] ? 'is-active' : '' ?>" href="<?= $escaper->escapeUrl($block->getUrl('customer-message/account/index', ['thread_id' => (int)$item['thread_id']])) ?>">
<strong><?= $escaper->escapeHtml($item['subject']) ?></strong>
<?php if ($item['thread_type'] === 'order' && $item['order_increment_id']): ?><small>#<?= $escaper->escapeHtml($item['order_increment_id']) ?></small><?php endif; ?>
<span><?= $escaper->escapeHtml(mb_substr((string)$item['last_content'], 0, 70)) ?></span>
<?php if ((int)$item['customer_unread_count']): ?><b><?= (int)$item['customer_unread_count'] ?></b><?php endif; ?>
</a>
<?php endforeach; ?>
</div>
</aside>
<?php endif; ?>
<section class="esun-conversation-main">
<header>
<h1><?= $escaper->escapeHtml($thread ? $thread['subject'] : ($orderId ? __('Order Messages') : __('New Message'))) ?></h1>
<?php if ($conversationOrderId && $orderIncrementId): ?><a href="<?= $escaper->escapeUrl($block->getUrl('sales/order/view', ['order_id' => $conversationOrderId])) ?>"><?= $escaper->escapeHtml(__('Order Number: %1', $orderIncrementId)) ?></a><?php endif; ?>
</header>
<div class="esun-conversation-messages">
<?php if ($entries): foreach ($entries as $entry): ?>
<article class="esun-conversation-entry is-<?= $escaper->escapeHtmlAttr($entry['sender_type']) ?>">
<small><?= $escaper->escapeHtml($entry['sender_type'] === 'customer' ? __('You') : __('Manager')) ?> · <?= $escaper->escapeHtml($block->formatDate($entry['created_at'], \IntlDateFormatter::MEDIUM, true)) ?></small>
<div class="esun-conversation-bubble"><?= nl2br($escaper->escapeHtml($entry['content'])) ?>
<?php if ($entry['attachments']): ?><div class="esun-conversation-images"><?php foreach ($entry['attachments'] as $attachment): ?><a href="<?= $escaper->escapeUrl($block->getUrl('customer-message/attachment/view', ['id' => (int)$attachment['attachment_id']])) ?>" target="_blank" rel="noopener"><img src="<?= $escaper->escapeUrl($block->getUrl('customer-message/attachment/view', ['id' => (int)$attachment['attachment_id']])) ?>" alt="<?= $escaper->escapeHtmlAttr($attachment['original_name']) ?>" loading="lazy"></a><?php endforeach; ?></div><?php endif; ?>
</div>
</article>
<?php endforeach; elseif (!$isNewGeneral): ?>
<p class="esun-conversation-empty"><?= $escaper->escapeHtml(__('No messages have been sent for this order yet.')) ?></p>
<?php endif; ?>
</div>
<form class="esun-conversation-compose" method="post" enctype="multipart/form-data" action="<?= $escaper->escapeUrl($block->getUrl('customer-message/thread/send')) ?>">
<?= $block->getBlockHtml('formkey') ?>
<?php if ($thread): ?><input type="hidden" name="thread_id" value="<?= (int)$thread['thread_id'] ?>"><?php endif; ?>
<?php if ($orderId): ?><input type="hidden" name="order_id" value="<?= $orderId ?>"><?php endif; ?>
<?php if ($isNewGeneral): ?><label><span><?= $escaper->escapeHtml(__('Subject')) ?></span><input class="input-text" type="text" name="subject" maxlength="255" required></label><?php endif; ?>
<label><span><?= $escaper->escapeHtml(__('Message')) ?></span><textarea name="content" rows="4" placeholder="<?= $escaper->escapeHtmlAttr(__('Please enter your message...')) ?>"></textarea></label>
<div class="esun-conversation-compose__actions">
<label class="esun-conversation-upload"><i class="iconfont icon-picture1" aria-hidden="true"></i><span><?= $escaper->escapeHtml(__('Add Images')) ?></span><input type="file" name="images[]" accept="image/jpeg,image/png,image/webp" multiple></label>
<small><?= $escaper->escapeHtml(__('JPG, PNG or WebP. Up to 5 images, 5 MB each.')) ?></small>
<button class="action primary" type="submit"><?= $escaper->escapeHtml(__('Send')) ?></button>
</div>
<div class="esun-conversation-preview" aria-live="polite"></div>
</form>
</section>
</div>
<script>
require(['jquery'], function ($) {
'use strict';
$('.esun-conversation-compose input[type=file]').on('change', function () {
var preview = $(this).closest('form').find('.esun-conversation-preview').empty();
Array.prototype.slice.call(this.files, 0, 5).forEach(function (file) {
if (!file.type.match(/^image\/(jpeg|png|webp)$/)) { return; }
var reader = new FileReader();
reader.onload = function (event) { $('<img>').attr('src', event.target.result).attr('alt', file.name).appendTo(preview); };
reader.readAsDataURL(file);
});
});
});
</script>