<?php include 'includes/session.php'; ?>
<?php include 'includes/sessions.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

  <?php include 'includes/navbar.php'; ?>
  <?php include 'includes/menubar.php'; ?>

            <?php 
              $isread=1;
              // $aid = (isset($_GET['aid']) ? $_GET['aid'] : '');
              // $did=intval($_GET['aid']);  
              date_default_timezone_set('Asia/Dhaka');
              $sql = "UPDATE attendance SET isread=:isread WHERE id = :aid";
              $query = $dbh -> prepare($sql);
              $query->bindParam(':isread',$isread,PDO::PARAM_STR);
              $query->bindParam(':aid',$aid,PDO::PARAM_STR);
              $query->execute();
            ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper"> 
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Users
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Materials</li>
      </ol>
    </section>
    <!-- Main content -->
    <section class="content">
      <?php
        if(isset($_SESSION['error'])){
          echo "
            <div class='alert alert-danger alert-dismissible'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4><i class='icon fa fa-warning'></i> Error!</h4>
              ".$_SESSION['error']."
            </div>
          ";
          unset($_SESSION['error']);
        }
        if(isset($_SESSION['success'])){
          echo "
            <div class='alert alert-success alert-dismissible'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4><i class='icon fa fa-check'></i> Success!</h4>
              ".$_SESSION['success']."
            </div>
          ";
          unset($_SESSION['success']);
        }
      ?>
      <div class="row">
          
        <div class="col-xs-12">
          <button class='btn btn-success btn-sm btn-flat add-user'><i class='fa fa-plus'></i> Add Materials</button>
                       
          <div class="box">
            <!-- <div class="box-header with-border">
              <a href="#addnew" data-toggle="modal" class="btn btn-primary btn-sm btn-flat"><i class="fa fa-plus"></i> Add New Attendence</a>
            </div> -->
            <div class="box-body">
            <div class="table-responsive">
              <table id="example1" class="table table-bordered">
                <thead>
                  <th class="hidden"></th>
                  <th>Name</th>
                  <th>Phone</th>
                 <th>File</th>
                  <!-- <th>Time Out</th>
                  <td>".date('h:i A', strtotime($row['time_out']))."</td> -->
                  <th>Tools</th>
                </thead>
                <tbody>
                  <?php
                    $sql = "SELECT * FROM materials ORDER BY id DESC";
                    $query = $conn->query($sql);
                    while($row = $query->fetch_assoc()){
               
                      echo "
                        <tr>
                          <td class='hidden'></td>
                          <td>".$row['material_name']."</td>
                          <td>".$row['course_name']."</td>
                          <td><a href='https://xpydernetwork.com/admin_of_xpyder_tracking/admin/materials/".$row['file_name']."' target='_blank'>Download</a></td>

                         
                          "?>
                            
                          <?php 
                          echo
                       "
                          <td>
                          <button class='btn btn-success btn-sm btn-flat edit' data-id='".$row['id']."'><i class='fa fa-trash'></i> Edit</button>
                       
                            <button class='btn btn-danger btn-sm btn-flat delete' data-id='".$row['id']."'><i class='fa fa-trash'></i> Delete</button>
                          </td>
                        </tr>
                      ";
                    }
                  ?>
                </tbody>
              </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>   
  </div>
  
  <!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1" role="dialog" aria-labelledby="addUserModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="addUserModalLabel">Add New Materials</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form id="addUserForm" action="upload_material.php" method="POST" enctype="multipart/form-data">
        <div class="form-group">
    <label for="material_name">Material Name</label>
    <input type="text" class="form-control" id="material_name" name="material_name" required>
  </div>

  <div class="form-group">
    <label for="course_name">Course Name</label>
    <select class="form-control" id="course_name" name="course_name" required>
      <option value="">Select a Course</option>
      <?php
        // Fetching courses from the database
         $sql = "SELECT * FROM course_upload";
        $query = $conn->query($sql);
        while($row = $query->fetch_assoc()){
          echo "<option value='".$row['name']."'>".$row['name']."</option>";
        }
      ?>
    </select>
  </div>

  <div class="form-group">
    <label for="file">Upload File (PDF, Word, Image)</label>
    <input type="file" class="form-control" id="file" name="file" required>
  </div>

  <button type="submit" class="btn btn-primary">Upload Material</button>
        </form>
      </div>
    </div>
  </div>
</div>

    
  <?php include 'includes/footer.php'; ?>
  <?php include 'includes/users_modal.php'; ?>
</div>
<?php include 'includes/scripts.php'; ?>
<script>
$(function(){
    
    
  $(document).on('click', '.btn-success.add-user', function(e) {
    e.preventDefault();
    $('#addUserModal').modal('show'); // Show the modal
  });
    
  $(document).on('click', '.edit', function(e) {
    e.preventDefault();
    $('#edit').modal('show');
    var id = $(this).data('id');
    getRow(id);
  });

  $('.delete').click(function(e){
    e.preventDefault();
    $('#delete').modal('show');
    var id = $(this).data('id');
    getRow(id);
  });
});

function getRow(id){
  $.ajax({
    type: 'POST',
    url: 'users_row.php',
    data: {id:id},
    dataType: 'json',
    success: function(response){

      $('#user_id').val(response.id);
      $('#name').val(response.name);
      $('#phone').val(response.phone);
      $('#email').val(response.email);
      $('#t_pin').val(response.tpin);
      $('#pwd').val(response.password);
      $('#first_name').val(response.first_name);
      $('#last_name').val(response.last_name);
      $('#email').val(response.email);
      $('#used_refer_code').val(response.used_refer_code);
    }
  });
}
</script>
</body>
</html>
