<% layout('../layouts/main') -%>

<!-- DataTables -->
<% stylesheet('/css/dataTables.bootstrap.min.css') %>

<!-- DataTables -->
<% script('/js/jquery.dataTables.min.js') %>
<% script('/js/dataTables.bootstrap.min.js') %>

<% block('custom_scripts', `
<script>
  $(document).ready(function () {
      $('#example1').DataTable({
          "aoColumnDefs": [
             {'bSortable': false, 'aTargets': [4]},
//              //{"aTargets": [3], "sClass": "text-justify"},
//              {"sClass": "center", "aTargets": [1, 2]},
              {"width": "9%", "targets": [0]},
              {"width": "15%", "targets": [4]},
//              {"width": "8%", "targets": [8]},
//                  //{"width": "9%", "targets": [7]},
          ],
          "paging": true,
          "lengthChange": true,
          "searching": true,
          "ordering": true,
          "info": true,
          "autoWidth": false,
          "stateSave": true
      });
  });

  $(document).ready(function () {
      $(document).on('click', ".delete_confirmation", function (e) {
          e.preventDefault();
          // if (confirm("Do you want delete this item")) {
          //     //alert($(this).attr('href'));
          //     window.location.href = $(this).attr('href');
          // }
      });
      $(".show-more-button").on('click', function() {
	// If text is shown less, then show complete
        if($(this).attr('data-more') == 0) {
            $(this).attr('data-more', 1);
            $(this).css('display', 'block');
            $(this).text('Read Less');

            $(this).prev().css('display', 'none');
            $(this).prev().prev().css('display', 'inline');
        }
        // If text is shown complete, then show less
        else if(this.getAttribute('data-more') == 1) {
            $(this).attr('data-more', 0);
            $(this).css('display', 'inline');
            $(this).text('Read More');

            $(this).prev().css('display', 'inline');
            $(this).prev().prev().css('display', 'none');
        }
      });
  });

</script>
`) %>

<% block('custom_style', `
<style>
  .long-text {
	display: none
  }
  .text-dots {
      color: blue
  }
  .show-more-button {
      color: blue
  }
</style>
`) %>

<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        Reviews
        <!--<small>advanced tables</small>-->
    </h1>
    <ol class="breadcrumb">
        <li><a href="/dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Place</li>
    </ol>
</section>

<!-- Main content -->
<section class="content">
    <% if (Object.prototype.hasOwnProperty.call(flash, 'success')) { %>
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                aria-hidden="true">&times;</span></button>
        <%= flash.success %>
    </div>
    <% } %>
    <% if (Object.prototype.hasOwnProperty.call(flash, 'error')) { %>
    <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                aria-hidden="true">&times;</span></button>
        <%= flash.error %>
    </div>
    <% } %>
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">List of Reviews</h3>
                    <div class="pull-right">
                        <a href="<%= link || '' %>" class="btn btn-primary">Back</a>
                    </div>
                </div>
                <!-- /.box-header -->
                <div class="box-body">
                    <table id="example1" class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>Sl. No.</th>
                                <th>User</th>
                                <th>Comment</th>
                                <th>Rating</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <% 
                            if(doc){
                            for(var i=0; len=doc.length, i < len; i++) { %>
                            <tr>
                                <td>
                                    <%= (i+1) %>
                                </td>
                                <td>
                                    <%= doc[i].user ? doc[i].user.name : "Anonymous" %>
                                </td>
                                <td>
                                    <%
                                    var limit = 3;
                                    console.log("doc[i].comment.length: ", doc[i].comment.length)
                                    if(doc[i].comment.length > limit) {
                                    %>
                                    <span class="short-text">
                                        <%= doc[i].comment.substr(0, limit) %>
                                    </span>
                                    <span class="long-text">
                                        <%= doc[i].comment.substr(limit) %>
                                    </span>
                                    <span class="text-dots">
                                        ...
                                    </span>
                                    <span class="show-more-button" data-more="0">
                                        Read More
                                    </span>
                                    <% } else { %>
                                    <%= doc[i].comment %>
                                    <% } %>
                                </td>
                                <td>
                                    <%= doc[i].rating %>
                                </td>
                                <td class="text-center">
                                    <% if(doc[i].active) { %>
                                    <a href="<%= '/review/setActive/'+doc[i]._id+'/false/?rLink='+rLink %>"
                                        class="btn btn-sm btn-success" title="Set Active Status to false"><span
                                            class="glyphicon glyphicon-ok"></span></a>
                                    <% } else { %>
                                    <a href="<%= '/review/setActive/'+doc[i]._id+'/true/?rLink='+rLink %>"
                                        class="btn btn-sm btn-danger" title="Set Active Status to true"><span
                                            class="glyphicon glyphicon-remove"></span></a>
                                    <% } %>
                                </td>
                            </tr>
                            <% 
                            }
                            }
                            %>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th>Sl. No.</th>
                                <th>User</th>
                                <th>Comment</th>
                                <th>Rating</th>
                                <th>Action</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</section>
<!-- /.content -->