{% extends 'dashboard/employee_dashboard/main.html' %}
{% load static %}
{% block content %}
<h4 class="fw-bold py-3 mb-4">Visitor </h4>
<div style="padding: 10px;">
  {% if messages %}
    {% for message in messages %}
      <div class="alert {% if 'success' in message.tags %}alert-success{% elif 'danger' in message.tags %}alert-danger{% elif 'warning' in message.tags %}alert-warning{% elif 'info' in message.tags %}alert-info{% endif %} alert-dismissible" role="alert" id="success-alert">
        {{ message }}
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
        </button>
      </div>
    {% endfor %}
  {% endif %}
</div>
<div class="row">
    <div class="col-lg-12 col-md-12">
      <div class="card">
        <div class="table-responsive" style="margin: 10px;">
          <table class="datatables-basic table table-bordered table-responsive" id="all_visitor_user">
            <thead>
              <tr>
                <th>ID</th>
                <th>VISITOR NAME</th>
                <th>IMAGE</th>
                <th> VISITOR ID</th>
                <th>EMAIL</th>
                <th>MOBILE</th>
                <th>GENDER</th>
                <th>ADDRESS</th>
                <th>USER CREATED BY</th>
                <th>CREATED AT</th>
                <th>UPDATED AT</th>
                <th>ACTIONS</th>
              </tr>
            </thead>
          </table>
          {% csrf_token %}
        </div>
      </div>
    </div>
</div>
<script src="{% static 'plugins/jQuery/jQuery-2.1.3.min.js' %}" type="text/javascript"></script>
<script>
  $(document).ready(function() {
    // Retrieve CSRF token value from the hidden input
    var csrfToken = $('input[name=csrfmiddlewaretoken]').val();

    // Set up AJAX to include the CSRF token in the headers for non-GET requests
    $.ajaxSetup({
        beforeSend: function(xhr, settings) {
            if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
                xhr.setRequestHeader("X-CSRFToken", csrfToken);
                console.log("CSRF Token set in header: ", csrfToken);
            }
        }
    });

    // Define the AJAX URL, ensure it's correctly rendered
    var ajaxUrl = "{% url 'employee_user_page_ajax' %}";

    // Initialize DataTable with server-side processing
    $('#all_visitor_user').DataTable({
        "ajax": {
            "url": ajaxUrl,
            "type": "POST"
        },
        "columns": [
            { data: 'id'},
            {
              data: null, // Use null data to indicate that we are not using a direct field
              render: function(data, type, row) {
                  return row.first_name + ' ' + row.last_name;
              }
          },

            {
              data: 'image',
              render: function(data, type, row) {
                  let imageUrl = data ? '{{IMAGEPATH}}' + data : '{{DEFAULT_IMAGE}}';
                  return '<img src="' + imageUrl + '" alt="Visitor Image" width="50" height="50"/>';
              }
          },
          {data : 'uni_id'},
          { data : 'email'},
          { data : 'mobile'},
          { data : 'gender'},
          {data :'address'},
          {
            data: null, // Use null data to indicate that we are not using a direct field
            render: function(data, type, row) {
                // Check if created_by_first_name and created_by_last_name are available
                if (row.created_by_first_name && row.created_by_last_name) {
                    return row.created_by_first_name + ' ' + row.created_by_last_name;
                } else {
                    // Fallback to row.first_name and row.last_name
                    return row.first_name + ' ' + row.last_name;
                }
            }
        },
            { data : 'created_at'},
            { data : 'updated_at'},
            {
                data: null,
                render: function(data, type, row) {
                    var editUrl = "{% url 'employee_user_new_appointment' 0 %}".replace('0', row.id);

                    var editButton = '<a title="NEW APPOINTMENT" href="' + editUrl + '" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Edit"><i class="bx bx-plus"></i></a>';

                    return editButton;
                }

            }


        ],
        "processing": true,
        "serverSide": true,
        "responsive": true,
        "ordering": false,
        lengthMenu: [
            [10, 25, 50, 100],
            [10, 25, 50, 100]
        ],
        layout: {
            topStart: {
                buttons: ['csv','pageLength']
            }
       },
    });
});


  </script>

{% endblock %}
