
 <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">My Task Details</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item"><a href="#">My Task</a></li>
              <li class="breadcrumb-item active">My Task Details</li>

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="card card-default">
          <div class="card-header">
            <h3 class="card-title">Course Name : <b><?php echo $all_lession[0]->title; ?></b></h3>
            <div class="card-tools">
              <span><i class="fa fa-clock"></i>&nbsp;&nbsp;<b id="time"></b></span>
            </div>
          </div>
          <div class="card-body">
            <div class="row">
              <div class="col-sm-12 mt-3">
                <table class="table table-bordered table-striped" id="table">
                  <?php foreach ($all_lession as $key => $l_details):?>
                  <tr>
                    <td colspan="4">Chapter Name : <b><?php echo $l_details->ch_lession_name; ?></b></td>
                  </tr>
                    <?php $i=1; foreach (json_decode($l_details->ch_chapters) as $key => $ch_chapters): 
                    $user_id = user()->user_id;
                    $this->db->where('inst_att_chapter_id', $l_details->ch_id);
                    $this->db->where('inst_att_lession_id', $key);
                    $this->db->where('inst_att_user_id', $user_id);
                    $attendance = $this->db->get('instructor_attendance')->row();
                    ?>
                      <tr>
                        <td width="15%">Lession <?php echo $i; ?></td>
                        <td><b><?php echo $ch_chapters; ?></b></td>
                        <td width="17%"><b><?php if($attendance->inst_att_date_time){echo date('d-m-Y', strtotime($attendance->inst_att_date_time)).' '.date('h:i A', strtotime($attendance->inst_att_date_time));}?></b></td>
                        <td width="15%">
                          <?php if($attendance->inst_att_lession_status == 0 || !$attendance->inst_att_lession_status): ?>
                            <button onclick="open_modal('<?php echo $key; ?>', '<?php echo $l_details->ch_id; ?>', 1)" class="btn btn-xs btn-danger"><i class="fa fa-check" aria-hidden="true"></i> Uncomplete</button>
                          <?php else: ?>
                            <button onclick="update_status('<?php echo $key; ?>', '<?php echo $l_details->ch_id; ?>', 0)" class="btn btn-xs btn-success"><i class="fa fa-times" aria-hidden="true"></i> Complete</button>
                          <?php endif; ?>
                        </td>
                      </tr>
                    <?php $i++; endforeach ?>
                  <?php endforeach; ?>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
<div class="modal fade" id="note_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Add Study Note</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form id="note_form">
        <div class="modal-body">
          <div class="form-group">
            <label for="message-text" class="col-form-label">Note <span class="text-danger">*</span></label>
            <textarea class="form-control" id="message-text" name="inst_note" required placeholder="Enter Study Note"></textarea>
            <input type="hidden" name="key" id="key">
            <input type="hidden" name="lession_id" id="lession_id">
            <input type="hidden" name="status" value="1">
            <input type="hidden" name="course_id" value="<?php echo $all_lession[0]->id; ?>">
          </div>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-primary">Submit</button>
        </div>
      </form>
    </div>
  </div>
</div>
<script>
function open_modal(key, id, status) 
{
  if(status == 1)
  {
    $('#note_modal').modal('show');
    $('#key').val(key);
    $('#lession_id').val(id)
  }
}

$("#note_form").submit(function(e) {

    e.preventDefault(); 
    var form = $(this);
    
    $.ajax({
      beforeSend: function() {
          $(".loader").show();
      },
      type: "POST",
      url: '<?php echo base_url(); ?>/admin/course/update_lession_status_for_attendance',
      data: form.serialize(), 
      success: function(data)
      {
        $('#note_modal').modal('hide');
        $("#table").load(location.href + " #table");
        $('#message-text').val('');
        $(".loader").hide();
      }
    });
    
});

function update_status(key, id, status) 
{
    $.ajax({
      beforeSend: function() {
          $(".loader").show();
      },
      type: "POST",
      async: true,
      data : {key : key, lession_id :id },
      url: '<?php echo base_url(); ?>/admin/course/update_lession_status_for_attendance_unread',
      success: function (data) 
      {
        $("#table").load(location.href + " #table");
        $(".loader").hide();
      },
      error: function (e) {
        $(".loader").hide();
        console.log(e.responseText)
      }
    });
}

var intervalId = window.setInterval(function(){
  get_current_time();
}, 500);
function get_current_time() 
{
  var date = new Date();
  var n = date.toDateString();
  var time = date.toLocaleTimeString();
  document.getElementById('time').innerHTML = n + ' ' + time;
  }
</script>