<?php
use EasyChat\View\HtmxFragmentRenderer;
use EasyChat\View\QuickReplyFragments;

$global_replies = isset($global_replies) && is_array($global_replies) ? $global_replies : [];
$personal_replies = isset($personal_replies) && is_array($personal_replies) ? $personal_replies : [];
$can_manage_global = !empty($can_manage_global_replies);
$limits = isset($quick_reply_limits) && is_array($quick_reply_limits) ? $quick_reply_limits : [];
$quick_reply_ai = isset($quick_reply_ai) && is_array($quick_reply_ai) ? $quick_reply_ai : [];
$ai_available = !empty($quick_reply_ai['available']);
$ai_reason = (string)($quick_reply_ai['reason'] ?? 'unknown');
$ai_knowledge_available = (int)($quick_reply_ai['knowledge_active_total'] ?? 0) > 0;
$max_title_length = (int)($limits['max_title_length'] ?? 40);
$max_text_length = (int)($limits['max_text_length'] ?? 4000);
$quick_reply_fragments = new QuickReplyFragments();
?>

<div class="quick-replies-page card border-0 shadow-sm rounded-4 p-4">
    <div class="quick-replies-header">
        <div>
            <h4 class="fw-bold text-dark mb-1" style="font-family: 'Outfit', sans-serif;"><i class="fa fa-bolt me-2 text-warning"></i> Быстрые ответы</h4>
        </div>
        <button type="button" class="btn btn-primary easychat-admin-primary-action px-3 py-2 d-flex align-items-center btn-add-visible-reply">
            <i class="fa fa-plus me-2"></i> Добавить шаблон
        </button>
    </div>

    <?php echo $quick_reply_fragments->alert('success', 'Шаблоны успешно сохранены.', true); ?>
    <?php echo $quick_reply_fragments->alert('error', '', true); ?>

    <div class="quick-reply-tabs" role="tablist" aria-label="Типы быстрых ответов">
        <button type="button" class="quick-reply-tab active" data-target="global-panel">
            <i class="fa fa-users"></i>
            Общие для команды
            <span class="quick-reply-count"><?php echo count($global_replies); ?></span>
        </button>
        <button type="button" class="quick-reply-tab" data-target="personal-panel">
            <i class="fa fa-user"></i>
            Мои ответы
            <span class="quick-reply-count"><?php echo count($personal_replies); ?></span>
        </button>
    </div>

    <form id="global-form" class="quick-reply-panel active" data-panel="global-panel" data-editable="<?php echo $can_manage_global ? '1' : '0'; ?>" method="post" action="index.php?route=quickreply/save" hx-post="index.php?route=quickreply/save" hx-target="#global-replies-list" hx-swap="<?php echo QuickReplyFragments::e(HtmxFragmentRenderer::quickReplyListSwap()); ?>" hx-sync="this:replace" hx-indicator="#quick-reply-sticky-save">
        <input type="hidden" name="scope" value="global">
        <input type="hidden" name="csrf_token" value="<?php echo QuickReplyFragments::e($easychat_csrf_token ?? ''); ?>">
        <div class="quick-reply-panel-heading">
            <div>
                <h5>Общие шаблоны</h5>
            </div>
            <?php if ($can_manage_global) { ?>
                <button type="button" class="d-none btn-add-reply quick-reply-add-transport" data-scope="global" aria-hidden="true" tabindex="-1" hx-get="index.php?route=quickreply/blankRow&scope=global" hx-target="#global-replies-list" hx-swap="<?php echo QuickReplyFragments::e(HtmxFragmentRenderer::quickReplyAppendSwap()); ?>" hx-sync="#global-replies-list:queue" hx-indicator="#global-replies-list">Добавить</button>
            <?php } ?>
        </div>
        <div class="replies-list" id="global-replies-list">
            <?php echo $quick_reply_fragments->replyRows('global', $global_replies, $can_manage_global, $max_title_length, $max_text_length, $ai_available, $ai_reason, $ai_knowledge_available); ?>
        </div>
    </form>
    <button type="button" class="d-none quick-reply-reorder-submit" data-reorder-list="#global-replies-list" aria-hidden="true" tabindex="-1" hx-post="index.php?route=quickreply/reorder" hx-include="#global-form [name=&quot;csrf_token&quot;], #global-form [name=&quot;scope&quot;], #global-replies-list .reply-order-id" hx-sync="#global-replies-list:replace" hx-swap="none" hx-indicator="#global-replies-list"></button>

    <form id="personal-form" class="quick-reply-panel" data-panel="personal-panel" data-editable="1" method="post" action="index.php?route=quickreply/save" hx-post="index.php?route=quickreply/save" hx-target="#personal-replies-list" hx-swap="<?php echo QuickReplyFragments::e(HtmxFragmentRenderer::quickReplyListSwap()); ?>" hx-sync="this:replace" hx-indicator="#quick-reply-sticky-save">
        <input type="hidden" name="scope" value="personal">
        <input type="hidden" name="csrf_token" value="<?php echo QuickReplyFragments::e($easychat_csrf_token ?? ''); ?>">
        <div class="quick-reply-panel-heading">
            <div>
                <h5>Личные шаблоны</h5>
            </div>
            <button type="button" class="d-none btn-add-reply quick-reply-add-transport" data-scope="personal" aria-hidden="true" tabindex="-1" hx-get="index.php?route=quickreply/blankRow&scope=personal" hx-target="#personal-replies-list" hx-swap="<?php echo QuickReplyFragments::e(HtmxFragmentRenderer::quickReplyAppendSwap()); ?>" hx-sync="#personal-replies-list:queue" hx-indicator="#personal-replies-list">Добавить</button>
        </div>
        <div class="replies-list" id="personal-replies-list">
            <?php echo $quick_reply_fragments->replyRows('personal', $personal_replies, true, $max_title_length, $max_text_length, $ai_available, $ai_reason, $ai_knowledge_available); ?>
        </div>
    </form>
    <button type="button" class="d-none quick-reply-reorder-submit" data-reorder-list="#personal-replies-list" aria-hidden="true" tabindex="-1" hx-post="index.php?route=quickreply/reorder" hx-include="#personal-form [name=&quot;csrf_token&quot;], #personal-form [name=&quot;scope&quot;], #personal-replies-list .reply-order-id" hx-sync="#personal-replies-list:replace" hx-swap="none" hx-indicator="#personal-replies-list"></button>

    <div id="quick-reply-sticky-save" class="quick-reply-sticky-save" role="status" aria-live="polite" aria-hidden="true">
        <div class="quick-reply-sticky-copy">
            <span class="quick-reply-sticky-dot" aria-hidden="true"></span>
            <span class="quick-reply-sticky-title">Есть несохранённые изменения</span>
            <span class="quick-reply-sticky-scope">Сохраните активную вкладку.</span>
        </div>
        <div class="quick-reply-sticky-actions">
            <button type="button" class="btn btn-light btn-sm quick-reply-sticky-reset">Отменить</button>
            <button type="button" class="btn btn-primary btn-sm quick-reply-sticky-submit">
                <i class="fa fa-check me-1"></i><span>Сохранить</span>
            </button>
        </div>
    </div>
</div>



<script type="text/javascript">
window.EasyChatQuickReplyConfig = {
    maxTextLength: <?php echo (int)$max_text_length; ?>
};
</script>
<?php echo easychat_script_tag('js/views/quickreply.js'); ?>
