<?php

use common\helpers\GlobalParam;
use domain\Ticket\Ticket\TicketModule;
use domain\User\User\UserModule;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$ticketModule = TicketModule::getInstance();
$userModule = UserModule::getInstance();
$userCurrent = GlobalParam::getCurrentUser();
$this->setTitle('Voir un ticket');
$this->addBreadcrumb(['label' => 'Support', 'url' => ['index']]);
$this->addBreadcrumb('Voir un ticket');

?>
<div class="ticket-view">
    <div class="box box-solid">
        <div class="box-body">
            <table class="table">
                <tbody>
                    <tr>
                        <td><strong>Sujet</strong></td>
                        <td><?= Html::encode($ticket->subject); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Ouverture</strong></td>
                        <td><?= $ticketModule->getTicketDateCreatedAtFormat($ticket); ?></td>
                    </tr>
                    <tr>
                        <td><strong>Statut</strong></td>
                        <td><?= $ticketModule->getTicketStatusLabelAsHtml($ticket); ?></td>
                    </tr>
                    <?php if($userModule->getAuthorizationChecker()->isGrantedAsAdministrator($userCurrent)): ?>
                        <tr>
                            <td><strong>Producteur</strong></td>
                            <td><?= $ticketModule->isTicketUnread($ticket, $ticket->user) ?
                                    '<span class="label label-warning">Non lu</span>' :
                                    '<span class="label label-success">Lu</span>'; ?>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <ul class="timeline">
        <?php foreach($ticket->ticketMessages as $key => $ticketMessage): ?>
            <li>
                <?php if ($key === array_key_last($ticket->ticketMessages)): ?>
                    <a name="bottom"></a>
                <?php endif; ?>
                <a name="<?= $ticketMessage->id ?>"></a>
                <i class="fa fa-user <?= $userModule->isAdmin($ticketMessage->user) ? 'bg-orange' : 'bg-aqua'; ?>"></i>
                <div class="timeline-item">
                    <span class="time"><i class="fa fa-clock-o"></i> <?= date('d/m/Y à H:i', strtotime($ticketMessage->created_at)) ?></span>
                    <h3 class="timeline-header"><?= Html::encode($userModule->getUsername($ticketMessage->user)); ?></h3>
                    <div class="timeline-body">
                        <?= nl2br($ticketMessage->message); ?>
                    </div>
                </div>
            </li>
        <?php endforeach; ?>
    </ul>

    <div class="box box-success">
        <div class="box-header">
            <h3 class="box-title"><i class="fa fa-comments"></i> Répondre</h3>
        </div>
        <div class="box-body">
            <?php $form = ActiveForm::begin(); ?>
            <?= $form->field($ticketMessageResponse, 'message')->textarea(['rows' => 6]); ?>
            <div class="form-group">
                <?= Html::submitButton('Répondre', ['class' => 'btn btn-success btn-sm']) ?>
            </div>
            <?php ActiveForm::end(); ?>
        </div>
    </div>

    <div class="box <?= $ticketModule->isTicketOpen($ticket) ? 'box-danger' : 'box-success'; ?>">
        <div class="box-header">
            <h3 class="box-title">
                <?php if($ticketModule->isTicketOpen($ticket)): ?>
                    <i class="fa fa-folder"></i> Cliquez ici si vous souhaitez fermer le ticket
                <?php else: ?>
                    <i class="fa fa-folder-open"></i> Cliquez ici si vous souhaitez rouvrir le ticket
                <?php endif; ?>
            </h3>
        </div>
        <div class="box-body">
            <?php if($ticketModule->isTicketOpen($ticket)): ?>
                <?= Html::a('Fermer le ticket', ['close', 'id' => $ticket->id], ['class' => 'btn btn-danger btn-sm']) ?>
            <?php else: ?>
                <?= Html::a('Rouvrir le ticket', ['open', 'id' => $ticket->id], ['class' => 'btn btn-success btn-sm']) ?>
            <?php endif; ?>
        </div>
    </div>

</div>