<?php
    $appeal = support_get_appeal_by_id(get('id'));
    $messages = get_list('support_messages', 'appeal_id = '.escape_db($appeal['id']), "id DESC LIMIT 1");
    if (isset($messages[0]) && $messages[0]['login'] != session('login')) {
        support_change_appeal_status($appeal['id'], 'read');
    }
    while (is_post('support_add_message')) {
        if (empty(post('text'))) {
            $res = message_error("Заполните все обязательные поля!");
            break;
        }
        $res = support_add_message(session('login'), $appeal['id'], post('text'), true);
        if ($res['error']) {
            break;
        }
        support_change_appeal_status($appeal['id'], "answered");
        $res = message_success("Вы ответили клиенту!");
        unset($_POST);
        break;
    }
    if (isset($res)) {
        $report = $res;
    }
?>

<div id="page-wrapper">
    <?php include(ROOT."/adg/pages/show_report.php"); ?>
    <div class="container-fluid">
        <!-- Flot Charts -->
        <div class="row">
            <div class="col-lg-12">
                <h2 class="page-header"><?php echo escape($page_title); ?> - <?php echo escape($appeal['subject']); ?></h2>
            </div>
        </div>
        <!-- /.row -->

        <div class="row">
            <div class="col-lg-12">
                <div class="messages">
                    <?php foreach (support_get_messages_list($appeal['id']) as $row) { ?>
                    <div class="item">
                        <div class="text">
                            <div class="heading">
                                <?php if (session('login') === $row['login']) { ?>
                                    <a href="#">You</a>
                                <?php } else { ?>
                                    <a href="#">Client (<?php echo escape($row['login']); ?>)</a>
                                <?php } ?>
                                <span class="date"><?php echo format_date($row['date']); ?></span>
                            </div>
                            <?php echo escape($row['text']); ?>
                        </div>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <form class="form-horizontal" method="post" action="">
                        <input type="hidden" name="id" value="<?php echo $appeal['id']; ?>">
                        <div class="form-group">
                            <label class="col-md-3 col-xs-12 control-label">Ваш ответ</label>
                            <div class="col-md-6 col-xs-12">
                                <textarea class="form-control" name="text" placeholder="Ваш ответ"><?php echo post("text"); ?></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-3 col-xs-12 control-label"></label>
                            <div class="col-md-6 col-xs-12">
                        <button name="support_add_message" type="submit" class="btn btn-primary pull-left">Ответить</button>
                            </div>
                        </div>
                </form>
            </div>
        </div>

    </div>
    <!-- /.container-fluid -->

</div>
<!-- /#page-wrapper -->

<style>
/* MESSAGES */
.messages {
  width: 100%;
  float: left;
}
.messages .item {
  width: 100%;
  float: left;
  margin-bottom: 10px;
  opacity: 1;
  filter: alpha(opacity = 1);
  -webkit-transition: all 200ms ease;
  -moz-transition: all 200ms ease;
  -ms-transition: all 200ms ease;
  -o-transition: all 200ms ease;
  transition: all 200ms ease;
}
.messages .item .text {
  background: #FFF;
  padding: 10px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  border: 1px solid #D5D5D5;
}
.messages .item .text .heading {
  width: 100%;
  margin-bottom: 5px;
}
.messages .item .text .heading a {
  text-decoration: none;
  font-size: 12px;
  color: #33414e;
  font-weight: 600;
  line-height: 20px;
}
.messages .item .text .heading .date {
  float: right;
  line-height: 20px;
  font-size: 11px;
  color: #CCC;
  font-weight: 600;
}
.messages .item .text:hover {
  border-color: #BBB;
}
.messages .item:nth-child(2n) .text {
  background: #F6F6F6;
}
.messages .item.item-visible {
  opacity: 1;
  filter: alpha(opacity = 100);
}
.messages.messages-img .item .image {
  float: left;
  width: 40px;
}
.messages.messages-img .item .image img {
  border: 2px solid #F5F5F5;
  border-radius: 50%;
  width: 40px;
}
.messages.messages-img .item .text {
  margin-left: 50px;
  position: relative;
}
.messages.messages-img .item .text:after,
.messages.messages-img .item .text:before {
  right: 100%;
  top: 20px;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}
.messages.messages-img .item .text:after {
  border-color: rgba(255, 255, 255, 0);
  border-right-color: #FFF;
  border-width: 5px;
  margin-top: -5px;
}
.messages.messages-img .item .text:before {
  border-color: rgba(213, 213, 213, 0);
  border-right-color: #D5D5D5;
  border-width: 6px;
  margin-top: -6px;
}
.messages.messages-img .item .text:hover:before {
  border-right-color: #BBB;
}
.messages.messages-img .item.in .image {
  float: right;
}
.messages.messages-img .item.in .text {
  margin-left: 0px;
  margin-right: 50px;
}
.messages.messages-img .item.in .text:after,
.messages.messages-img .item.in .text:before {
  right: auto;
  left: 100%;
}
.messages.messages-img .item.in .text:after {
  border-right-color: transparent;
  border-left-color: #FFF;
}
.messages.messages-img .item.in .text:before {
  border-right-color: transparent;
  border-left-color: #D5D5D5;
}
.messages.messages-img .item.in .text:hover:before {
  border-left-color: #BBB;
}
/* END MESSAGES */
</style>
