<body id="page-top">

  <?php $user_id = $this->session->user_id; ?>

  <!-- Sidebar -->
  <?php $this->load->view('restricted/layout/sidebar_editor'); ?>
  <!-- Sidebar -->

  <div id="content-wrapper" class="d-flex flex-column">
    <div id="content">

      <!-- NavBar -->
      <?php $this->load->view('restricted/layout/navbar'); ?>
      <!-- Navbar -->

      <!-- Container Fluid-->
      <div class="container-fluid" id="container-wrapper">
        <div class="d-sm-flex align-items-center justify-content-between mb-4">
          <h1 class="h3 mb-0 text-purple"><?php echo $title; ?></h1>
          <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="./">Home</a></li>
            <li class="breadcrumb-item active" aria-current="page"><?php echo $title; ?></li>
          </ol>
        </div>


        <div class="row mb-3">
          <!-- Pending Requests Card Example -->
          <div class="col-xl-12 col-md-12 mb-4">
            <div class="card mb-4">
              <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                <h6 class="m-0 font-weight-bold text-primary"><?php echo $title; ?></h6>
              </div>
              <div class="table-responsive">
                <table class="table align-items-center table-flush table-hover" id="dataTableHover">
                  <thead class="thead-light">
                    <tr>
                      <th>Código</th>
                      <th>Enviado em</th>
                      <th>Modificado em</th>
                      <th>Título do capítulo</th>
                      <th>Status</th>
                      <th>Ação</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php if ($chapters) : ?>
                      <?php $i = 0; ?>
                      <?php foreach ($chapters as $chapter) : ?>
                        <?php $book = $this->core_model->get_by_id('books', array('id' => $chapter->book_id)); ?>
                        <tr class="chapter_row" data-toggle="collapse" data-target=".chapter_details<?php echo $chapter->id; ?>">
                          <td><?php echo $chapter->unique_code; ?></td>
                          <td><?php $date = date_create($chapter->created_at);
                              echo date_format($date, 'd/m/Y'); ?></td>
                          <td><?php if ($chapter->modified_at != '0000-00-00 00:00:00') : $date = date_create($chapter->modified_at);
                                echo date_format($date, 'd/m/Y');
                              endif; ?></td>
                          <td><?php echo $chapter->title; ?></td>
                          <td>
                            <?php
                            switch ($chapter->status):
                              case 'waiting_review':
                                echo "<span class='badge badge-warning'>Agurdando revisão</span>";
                              break;
                              case 'in_review':
                                  echo "<span class='badge badge-info'>Em revisão</span>";
                              break;
                              case 'approved':
                                  echo "<span class='badge badge-success'>Aprovado</span>";
                              break;
                              case 'recused':
                                  echo "<span class='badge badge-danger'>Recusado</span>";
                              break;
                              case 'approved_with_warnings':
                                  echo "<span class='badge badge-dark'>Aprovado com pendências</span>";
                              break;
                              default:
                                  echo "<span class='badge badge-secondary'>Indefinido</span>";
                              break;
                            endswitch;
                            ?>
                          </td>
                          <td>
                            <a href="#" data-toggle="modal" data-target="#modalChapter<?php echo $chapter->id; ?>" data-id="modalChapter<?php echo $chapter->id; ?>" class="btn btn-info btn-sm">
                              <i class="fas fa-eye"></i>
                            </a>
                          </td>
                        </tr>

                        <tr class="collapse chapter_d chapter_details<?php echo $chapter->id; ?>">
                          <td colspan="10" class="py-4">
                            <h5 class="pb-4"><u>Comentários</u></h5>
                            <?php $comments = $this->core_model->get_all('comments', array('chapter_id' => $chapter->id)); ?>
                            <?php if ($comments) : ?>
                              <?php foreach ($comments as $comment) : ?>
                                <?php $comment_author = $comment->comment_author; ?>
                                <?php
                                if ($this->ion_auth->in_group('editor', $comment_author)) {
                                  $author_group = "E";
                                } else if ($this->ion_auth->in_group('reviewer', $comment_author)) {
                                  $author_group = "R";
                                }
                                ?>
                                <div class="ml-3 py-1">
                                  <div class="row">
                                    <div class="col-sm-8">
                                      <div class="row">
                                        <div class="mt-1">
                                          <span class="badge badge-info chapter_comment_author"><?php echo $author_group; ?></span>
                                        </div>
                                        <div class="col-sm-11">
                                          <span class="badge font-weight-light"><?php $date = date_create($comment->created_at); echo date_format($date, 'd/m/Y H:i'); ?></span>
                                          <p class="ml-1"><?php echo $comment->comment; ?></p>
                                        </div>
                                      </div>
                                    </div>
                                    <div class="col-sm-3 ml-5 pl-2">
                                      <span class="font-weight-light"><small>Status</small></span>
                                      <p>
                                        <?php
                                        switch ($comment->status):
                                          case 'waiting_review':
                                            echo "<span class='badge badge-warning'>Agurdando revisão</span>";
                                          break;
                                          case 'in_review':
                                              echo "<span class='badge badge-info'>Em revisão</span>";
                                          break;
                                          case 'approved':
                                              echo "<span class='badge badge-success'>Aprovado</span>";
                                          break;
                                          case 'recused':
                                              echo "<span class='badge badge-danger'>Recusado</span>";
                                          break;
                                          case 'approved_with_warnings':
                                              echo "<span class='badge badge-dark'>Aprovado com pendências</span>";
                                          break;
                                          default:
                                              echo "<span class='badge badge-secondary'>Indefinido</span>";
                                          break;
                                        endswitch;
                                        ?>
                                      </p>
                                    </div>
                                  </div>
                                  <hr>
                                </div>
                              <?php endforeach; ?>
                            <?php else : ?>
                              <div class="ml-3 py-1">
                                <span class="font-weight-light">Ainda não há comentário de editores/revisores...</span>
                              </div>
                            <?php endif; ?>
                          </td>

                        </tr>

                        <!-- Modal Chapter -->
                        <div class="modal fade" id="modalChapter<?php echo $chapter->id; ?>" tabindex="-1" role="dialog" aria-labelledby="modalChapterTitle" aria-hidden="true">
                          <div class="modal-dialog modal-dialog-centered" role="document">
                            <div class="modal-content">
                              <div class="modal-header bg-primary">
                                <h5 class="modal-title text-white" id="modalChapterTitle">Detalhes do capítulo</h5>
                              </div>
                              <div class="modal-body">
                                <div class="row">
                                  <div class="col-sm-6 py-3">
                                    <h5 class="text-primary">Livro</h5>
                                    <p><?php echo $book->title; ?></p>
                                  </div>
                                  <div class="col-sm-6 py-3">
                                    <h5 class="text-primary">Título do capítulo</h5>
                                    <p><?php echo $chapter->title; ?></p>
                                  </div>
                                  <div class="col-sm-12 py-3">
                                    <h5 class="text-primary">Palavras-chave</h5>
                                    <?php $keywords = explode(',', $chapter->keywords); ?>
                                    <?php foreach ($keywords as $keyword) : ?>
                                      <span class="badge badge-primary p-2 mb-1"><?php echo $keyword; ?></span>
                                    <?php endforeach; ?>
                                  </div>
                                  <div class="col-sm-11 pt-3">
                                    <h5 class="text-primary text-left">Documento em anexo</h5>
                                  </div>
                                  <div class="card text-center col-sm-12">
                                    <div class="card-body">
                                      <p class="card-text" style="font-size: 13px;"><img style="height: 30px;" src="<?php echo base_url('public/assets/restricted/img/file-check-alt-blue.svg'); ?>" alt=""><?php echo $chapter->document; ?></p>
                                      <a download href="<?php echo base_url('./uploads/authors/' . $chapter->author_id . '/' . $chapter->document); ?>" class="btn btn-primary"><img style="height: 25px;" src="<?php echo base_url('public/assets/restricted/img/file-check-alt.svg'); ?>" alt="">Baixar</a>
                                    </div>
                                  </div>
                                  <div class="col-sm-12 py-3">
                                    <h5 class="text-primary">Comentário sobre a revisão</h5>
                                    <div class="row">
                                      <form action="<?php echo base_url('restricted/editor/home/chapter_comments/' . $chapter->id); ?> " method="POST">
                                        <input type="hidden" name="comment_author" value="<?php echo $user_id; ?>">
                                        <div class="col-sm-9">
                                          <textarea class="form-control" name="comment" id="comment" rows="10"></textarea>
                                        </div>
                                        <div class="col-sm-3">
                                          <select class="form-control col-12 mb-3" name="chapter_status" id="chapter_status">
                                            <option value="">Status...</option>
                                            <option value="waiting_review">Aguardando revisão</option>
                                            <option value="in_review">Em revisão</option>
                                            <option value="approved">Aprovado</option>
                                            <option value="recused">Recusado</option>
                                            <option value="approved_with_warnings">Aprovado com pendências</option>
                                          </select>
                                          <p>
                                            <?php
                                            switch ($chapter->status):
                                              case 'waiting_review':
                                                echo "<span class='badge badge-warning'>Agurdando revisão</span>";
                                              break;
                                              case 'in_review':
                                                  echo "<span class='badge badge-info'>Em revisão</span>";
                                              break;
                                              case 'approved':
                                                  echo "<span class='badge badge-success'>Aprovado</span>";
                                              break;
                                              case 'recused':
                                                  echo "<span class='badge badge-danger'>Recusado</span>";
                                              break;
                                              case 'approved_with_warnings':
                                                  echo "<span class='badge badge-dark'>Aprovado com pendências</span>";
                                              break;
                                              default:
                                                  echo "<span class='badge badge-secondary'>Indefinido</span>";
                                              break;
                                            endswitch;
                                            ?>
                                          </p>
                                        </div>
                                    </div>
                                    <input type="submit" class="btn btn-primary mt-1" value="Salvar">
                                    </form>
                                  </div>
                                </div>
                                <!-- /Row -->
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-outline-primary" data-dismiss="modal">Fechar</button>
                              </div>
                            </div>
                          </div>
                        </div>
                      <?php endforeach; ?>
                    <?php else : ?>
                      <tr>
                        <td>Nenhum registro encontrado...</td>
                      </tr>
                    <?php endif; ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>

        </div>
        <!--/Row-->

        <!-- Modal Logout -->
        <?php $this->load->view('restricted/layout/modal_logout'); ?>
        <!-- /Modal -->

      </div>
      <!--- /Container Fluid-->
    </div><!-- /Content -->