
        <?php
        // Don't show this container when only rendering the widget section
        if (!(isset($render_section) ? $render_section : null) && !(isset($is_ajax) ? $is_ajax : null)) {
        ?>
        <div class="col-md-12">
        <?php
        }
        ?>
            <?php
            echo (isset($message) ? $message : null);

            $this->WidgetClient->clear();
            $this->WidgetClient->create($this->_('ClientEmails.index.boxtitle_emails', true), ['id' => 'client_emails'], (isset($render_section) ? $render_section : null));
            $this->WidgetClient->startBody();

            if ((isset($logs) ? $logs : false) && ($num_logs = count($logs)) > 0) {
            ?>
            <div class="table-responsive">
                <table class="table table-curved table-striped-alt table-hover" id="emails">
                    <thead>
                        <tr>
                            <th>
                                <a href="<?php echo $this->Html->safe($this->base_uri . 'emails/index/?sort=date_sent&order=' . ($sort == 'date_sent' ? $negate_order : $order));?>" class="ajax<?php echo $this->Html->safe($sort == 'date_sent' ? ' ' . $order : '');?>"><?php $this->_('ClientEmails.index.heading_date');?></a>
                            </th>
                            <th>
                                <a href="<?php echo $this->Html->safe($this->base_uri . 'emails/index/?sort=subject&order=' . ($sort == 'subject' ? $negate_order : $order));?>" class="ajax<?php echo $this->Html->safe($sort == 'subject' ? ' ' . $order : '');?>"><?php $this->_('ClientEmails.index.heading_subject');?></a>
                            </th>
                            <th>
                                <span><?php $this->_('ClientEmails.index.heading_summary');?></span>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // Display all email logs
                        for ($i = 0; $i < $num_logs; $i++) {
                        ?>
                        <tr class="expand email_log">
                            <td><?php echo $this->Date->cast((isset($logs[$i]->date_sent) ? $this->Html->safe($logs[$i]->date_sent) : null), 'date_time');?></td>
                            <td><?php (print (isset($logs[$i]->subject) ? $this->Html->safe($logs[$i]->subject) : null));?></td>
                            <?php
                            // Limit length of summary
                            $summary = (isset($logs[$i]->body_text) ? $this->Html->safe($logs[$i]->body_text) : null);
                            if (strlen($summary) > 40) {
                                if (function_exists('mb_strimwidth')) {
                                    $summary = trim(mb_strimwidth($summary, 0, 40)) . '...';
                                } else {
                                    $summary = trim(substr($summary, 0, 40)) . '...';
                                }
                            }
                            ?>
                            <td><?php echo $summary;?></td>
                        </tr>
                        <tr class="expand_details">
                            <td class="subtable" colspan="3">
                                <ul class="nav nav-tabs">
                                    <li class="nav-item">
                                        <a href="<?php echo $this->Html->safe('#text_' . $i);?>" class="nav-link active" data-toggle="tab"><?php $this->_('ClientEmails.index.text_text');?></a>
                                    </li>
                                    <li class="nav-item">
                                        <a href="<?php echo $this->Html->safe('#html_' . $i);?>" class="nav-link" data-toggle="tab"><?php $this->_('ClientEmails.index.text_html');?></a>
                                    </li>
                                </ul>

                                <div class="tab-content">
                                    <div id="<?php echo $this->Html->safe('text_' . $i);?>" class="tab-pane fade p-0 active show">
                                        <div class="table-responsive">
                                            <table class="table table-striped multi_heading border-top-0 p-0 m-0">
                                                <thead>
                                                    <tr class="email_header">
                                                        <td><?php $this->_('ClientEmails.index.text_to');?></td>
                                                        <td>
                                                            <a href="mailto:<?php (print (isset($logs[$i]->to_address) ? $this->Html->safe($logs[$i]->to_address) : null));?>"><?php (print (isset($logs[$i]->to_address) ? $this->Html->safe($logs[$i]->to_address) : null));?></a>
                                                        </td>
                                                    </tr>
                                                    <?php
                                                    // Show CC addresses
                                                    if (!empty($logs[$i]->cc_address)) {
                                                    ?>
                                                    <tr class="email_header">
                                                        <td><?php $this->_('ClientEmails.index.text_cc');?></td>
                                                        <td>
                                                            <?php
                                                            $j = 0;
                                                            foreach ($logs[$i]->cc_address as $cc_address) {
                                                            ?><?php echo $j++ > 0 ? ', ' : '';?>
                                                            <a href="mailto:<?php (print (isset($cc_address) ? $this->Html->safe($cc_address) : null));?>"><?php (print (isset($cc_address) ? $this->Html->safe($cc_address) : null));?></a><?php
                                                            }
                                                            ?>
                                                        </td>
                                                    </tr>
                                                    <?php
                                                    }
                                                    ?>
                                                    <tr class="email_header">
                                                        <td><?php $this->_('ClientEmails.index.text_from');?></td>
                                                        <td>
                                                            <a href="mailto:<?php (print (isset($logs[$i]->from_address) ? $this->Html->safe($logs[$i]->from_address) : null));?>"><?php (print (isset($logs[$i]->from_name) ? $this->Html->safe($logs[$i]->from_name) : null));?></a>
                                                        </td>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td colspan="2" class="body"><?php echo nl2br($this->Html->hyperlink((isset($logs[$i]->body_text) ? $this->Html->safe($logs[$i]->body_text) : null)));?></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    <div id="<?php echo $this->Html->safe('html_' . $i);?>" class="tab-pane fade p-0">
                                        <div class="table-responsive">
                                            <table class="table table-striped multi_heading border-top-0 p-0 m-0">
                                                <thead>
                                                    <tr class="email_header">
                                                        <td><?php $this->_('ClientEmails.index.text_to');?></td>
                                                        <td>
                                                            <a href="mailto:<?php (print (isset($logs[$i]->to_address) ? $this->Html->safe($logs[$i]->to_address) : null));?>"><?php (print (isset($logs[$i]->to_address) ? $this->Html->safe($logs[$i]->to_address) : null));?></a>
                                                        </td>
                                                    </tr>
                                                    <?php
                                                    // Show CC addresses
                                                    if (!empty($logs[$i]->cc_address)) {
                                                    ?>
                                                    <tr class="email_header">
                                                        <td><?php $this->_('ClientEmails.index.text_cc');?></td>
                                                        <td>
                                                            <?php
                                                            $j = 0;
                                                            foreach ($logs[$i]->cc_address as $cc_address) {
                                                            ?><?php echo $j++ > 0 ? ', ' : '';?>
                                                            <a href="mailto:<?php (print (isset($cc_address) ? $this->Html->safe($cc_address) : null));?>"><?php (print (isset($cc_address) ? $this->Html->safe($cc_address) : null));?></a><?php
                                                            }
                                                            ?>
                                                        </td>
                                                    </tr>
                                                    <?php
                                                    }
                                                    ?>
                                                    <tr class="email_header">
                                                        <td><?php $this->_('ClientEmails.index.text_from');?></td>
                                                        <td>
                                                            <a href="mailto:<?php (print (isset($logs[$i]->from_address) ? $this->Html->safe($logs[$i]->from_address) : null));?>"><?php (print (isset($logs[$i]->from_name) ? $this->Html->safe($logs[$i]->from_name) : null));?></a>
                                                        </td>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <td colspan="2" class="body">
                                                            <iframe class="border-0 w-100" srcdoc="<?php echo $this->Html->safe($logs[$i]->body_html ?? '');?>"></iframe>
                                                        </td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <?php
                        }
                        ?>
                    </tbody>
                </table>
            </div>
            <?php
            } else {
            ?>
            <div class="alert alert-info">
                <p>
                    <?php $this->_('ClientEmails.index.no_results');?>
                </p>
            </div>
            <?php
            }

            $this->WidgetClient->endBody();
            if ($this->Pagination->hasPages()) {
                $this->WidgetClient->startFooter();
                $this->Pagination->build();
                $this->WidgetClient->endFooter();
            }
            $this->WidgetClient->end();
            ?>
        <?php
        // Don't show this container when only rendering the widget section
        if (!(isset($render_section) ? $render_section : null) && !(isset($is_ajax) ? $is_ajax : null)) {
        ?>
        </div>
        <?php
        }
        ?>
<?php
$this->Javascript->setInline('
    $(document).ready(function() {
        $(".tab-content .tab-pane.show").show();
        $(".subtable .nav-link").click(function() {
            var parent = $(this).parents(".subtable")
            parent.find(".nav-link").removeClass("active");
            $(this).addClass("active");

            var tab = $(this).attr("href");
            parent.find(".tab-content .tab-pane").removeClass("active show");
            $(tab).addClass("active show");
        });
    });
');
?>
