<section class="content-header">
    <h1><?= __('Ticket') ?> #<?= $ticket->getId() ?></h1>

    <div class="actions">
        <a data-ajax class="btn btn-default btn-sm" href="<?= admin_url_to('/helpdesk/tickets/' . $type) ?>"><?= __('List') ?></a>
    </div>
</section>

<section class="content">
    <div class="box box-default">
        <form data-ajax data-upload action="<?= admin_url_to('/helpdesk/tickets/add_message/' . $ticket->getId()) ?>" method="post">
            <?= csrf_field() ?>

            <div class="box-body">
                <div class="section">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                            <h5><?= __('Ticket') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div class="form-group">
                                <label class="control-label"><?= __('Subject') ?></label>
                                <p class="form-control-static"><?= $ticket->getSubject() ?></p>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label"><?= __('Department') ?></label>
                                        <p class="form-control-static"><?= $ticket->getDepartment() ?></p>
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label"><?= __('Author') ?></label>
                                        <p class="form-control-static"><?= $ticket->getAccount() ?></p>
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label"><?= __('Status') ?></label>
                                        <p class="form-control-static"><?= $status[$ticket->getStatus()] ?></p>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label"><?= __('Responsible') ?></label>
                                        <p class="form-control-static"><?= $ticket->getResponsibleName() ?></p>
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label"><?= __('Created at') ?></label>
                                        <p class="form-control-static"><?= $ticket->getCreated()->format('d/m/Y H:i') ?></p>
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label"><?= __('Update at') ?></label>
                                        <p class="form-control-static"><?= $ticket->getUpdated()->format('d/m/Y H:i') ?></p>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="control-label"><?= __('Message') ?></label>
                                <p class="form-control-static"><?= nl2br($ticket->getMessage()) ?></p>
                            </div>

                            <div class="form-group">
                                <label class="control-label"><?= __('Images') ?></label>
                                <?php if ($ticket->hasImages()): ?>
                                    <div class="row">
                                        <?php foreach (explode(',', $ticket->getImages()) as $image): ?>
                                            <a class="col-md-2" href="<?= upload_to('/helpdesk/' . $image) ?>" target="_blank">
                                                <div class="thumbnail">
                                                    <img src="<?= upload_to('/helpdesk/' . $image) ?>" />
                                                </div>
                                            </a>
                                        <?php endforeach ?>
                                    </div>
                                <?php else: ?>
                                    <p class="form-control-static">-</p>
                                <?php endif ?>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="section">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                            <h5><?= __('Messages') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <ul class="list">
                                <?php foreach ($ticket->getMessages() as $message): ?>
                                    <li>
                                        <div class="desc">
                                            <span class="name"><?= $message->getAuthor() ?></span>
                                            <span class="created"><?= util('Date')->dateTime($message->getCreated()) ?></span>
                                            <p><?= nl2br($message->getMessage()) ?></p>

                                            <?php if ($message->hasImages()): ?>
                                                <div class="clear" style="height:20px"></div>
                                                <div class="row" style="margin:0!important;">
                                                    <?php foreach (explode(',', $message->getImages()) as $image): ?>
                                                        <a class="col-md-2" href="<?= upload_to('/helpdesk/' . $image) ?>" target="_blank">
                                                            <div class="thumbnail">
                                                                <img src="<?= upload_to('/helpdesk/' . $image) ?>" />
                                                            </div>
                                                        </a>
                                                    <?php endforeach ?>
                                                </div>
                                            <?php endif ?>
                                        </div>
                                    </li>
                                <?php endforeach ?>
                            </ul>

                            <?php if ($ticket->getStatus() != 3): ?>
                                <h3><?= __('Add new message') ?></h3>
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label" for="ticket-status"><?= __('Status') ?></label>
                                            <select class="form-control" id="ticket-status" name="status">
                                                <?php foreach ($status as $key => $value): ?>
                                                    <option value="<?= $key ?>"<?= $ticket->getStatus() == $key ? ' selected="selected"' : '' ?>><?= $value ?></option>
                                                <?php endforeach ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label" for="ticket-responsible"><?= __('Responsible') ?></label>
                                            <select class="form-control" id="ticket-responsible" name="responsible">
                                                <option value=""></option>
                                                <?php foreach ($users as $user): ?>
                                                    <option value="<?= $user['username'] ?>"<?= $ticket->getResponsible() == $user['username'] ? ' selected="selected"' : '' ?>><?= $user['name'] ?></option>
                                                <?php endforeach ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="ticket-images"><?= __('Images') ?></label>
                                    <input data-upload-field type="file" multiple name="images" id="images" />
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="ticket-message"><?= __('Message') ?></label>
                                    <textarea class="form-control" name="message" id="ticket-message"></textarea>
                                </div>

                                <?php if ($ticket->getStatus() != 3): ?>
                                    <div class="box-footer" style="padding-left:0">
                                        <input style="margin-bottom:10px;" type="submit" class="btn btn-primary" value="<?= __('Answer') ?>" />
                                    </div>
                                <?php endif ?>
                            <?php endif ?>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</section>