{% extends 'dashboard/admin_dashboard/main.html' %}
{% load static %}
{% block content %}
<h4 class="fw-bold py-3 mb-4"><span class="text-muted fw-light">Safety Training /</span> All Safety Training</h4>
<div style="padding: 10px;">
  {% if messages %}
    {% for message in messages %}
      <div class="alert {% if 'success' in message.tags %}alert-success alert-dismissible{% elif 'danger' in message.tags %}alert-danger alert-dismissible{% endif %}" 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="mt-3 d-flex justify-content-start">
        <a href="{% url 'admin_safety_training_add' %}" class="btn btn-primary" style="margin: 10px;">
            Add Safety Training</a>
        </div>
        <div class="table-responsive" style="margin: 10px;">
          <table class="table table-bordered table-striped" id="safety_training_table">
            <thead>
              <tr>
                <th>ID</th>
                <th>TITLE</th>
                <th>STATUS</th>
                <th>CREATED AT</th>
                <th>UPDATED AT</th>
                <th>ACTION</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 src="{% static 'dist/js/pages/safety_training_delete.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 'admin_safety_training_ajax_page_ajax' %}";
    var baseUrl = window.location.origin;
    // Initialize DataTable with server-side processing
    $('#safety_training_table').DataTable({
        "ajax": {
            "url": ajaxUrl,
            "type": "POST"
        },
        "columns": [
            { data: 'id'},
            { data : 'title'},
            {
              data: 'is_active',
              render: function(data, type, row) {
                  return data == 1 ? 'Yes' : 'No';
              }
            },
            { data : 'created_at'},
            { data : 'updated_at'},
            {
                data: null,
                render: function(data, type, row) {
                    var editUrl = "{% url 'admin_safety_training_edit' 0 %}".replace('0', row.id);
                    var deleteUrl = "{% url 'admin_safety_training_delete' 0 %}".replace('0', row.id);

                    var editButton = '<a title="EDIT" href="' + editUrl + '" class="btn btn-primary" data-toggle="tooltip" data-placement="top" title="Edit"><i class="bx bx-pencil"></i></a>';
                    var deleteButton = '<button title="DELETE" class="btn btn-danger delete-link" data-id="' + row.id + '" value="' + deleteUrl + '"><i class="bx bx-trash"></i></button>';

                    var videoUrl = baseUrl + "/safety_training/" + row.video_file; // Assuming video_file is a field in the database
                    var videoButton = '<a title="VIEW VIDEO" href="' + videoUrl + '" target="_blank" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="View Video"><i class="bx bx-show"></i></a>';

                    return editButton + ' ' + deleteButton + ' ' + videoButton;

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


  </script>
{% endblock %}


