<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script>
<!-- TOP Nav Bar END -->
<div class="container-fluid">
   <div class="row">
      <div class="col-sm-12">
         <div class="iq-card">
            <div class="iq-card-header d-flex justify-content-between">
               <div class="iq-header-title">
                  <h4 class="card-title">Rates for <?= html_escape($pitch->name) ?></h4>
               </div>
               <span class="table-add float-right mb-3 mr-2">
                <a href="<?php echo base_url(); ?>Pitches/" class="btn btn-default"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</a>&emsp;
                  <a href="<?= site_url('Pitches/rate_card_add/'.$pitch->id) ?>" class="btn btn-sm iq-bg-success" ><i
                     class="ri-add-fill"><span class="pl-1">Add New</span></i>
                  </a>
                  </span>
            </div>
            <div class="iq-card-body">
             <div id="table" class="table-editable">
            <table id="myTable" class="display table" style="width:100%">
              <thead>
                <tr>
                    <th>Sl No.</th>
                    <th>Day Type</th>
                    <th>Time</th>
                    <th>Slot (min)</th>
                    <th>Price</th>
                    <th>Priority</th>
                    <th>Action</th>
                </tr>
              </thead>
              <tbody>
                <?php  
                $start=0;
                foreach($rates as $r)
                   { $start++;

                ?>
                <tr>
                    <td scope="row"><?php echo $start; ?></td>
                    <td><?= $r->day_type ?></td>
                    <td><?= ($r->time_start? $r->time_start : 'Any') . ' - ' . ($r->time_end? $r->time_end : 'Any') ?></td>
                    <td><?= $r->slot_minutes ?></td>
                    <td><?= $r->price ?></td>
                    <td><?= $r->priority ?></td>
                    
                    <td>
                        <!-- <a href="<?php echo base_url().'Pitches/Edit/'.$recordSet[$i]['id']; ?>" class="edit" title="Edit"><i class="fa fa-pencil" aria-hidden="true"></i>   </a> -->
                        <a href="javascript:void(0)"  onclick="delete_function(<?php echo $r->id; ?>);" class="delete remScnt" title="Delete"><i class="fa fa-trash" style="color:red;" aria-hidden="true" title="Delete"></i> </a> 

                    </td>
                </tr>

              <?php } ?>
              </tbody>
            </table>
          </div>
          </div>
         </div>
      </div>
   </div>
</div>

<!-- Manage Hours Modal -->
<div class="modal fade" id="hoursModal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Manage Hours: <span id="pitchName"></span></h5>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <div class="modal-body">
        <!-- Flash message -->
        <div id="hoursMsg"></div>

        <form id="hoursForm">
          <input type="hidden" name="pitch_id" id="pitchId">

          <div id="hoursFormContent"></div>

          <div class="form-group text-right">
            <button type="submit" class="btn btn-success">Save Hours</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>


<script>
$(document).on("click", ".manage-hours", function(){
    var pitchId = $(this).data("id");
    var pitchName = $(this).data("name");

    $("#pitchId").val(pitchId);
    $("#pitchName").text(pitchName);

    // Load existing hours
    $.get("<?= base_url('Pitches/pitch_hours_get/'); ?>" + pitchId, function(html){
        $("#hoursFormContent").html(html);
    });

    $("#hoursModal").modal("show");
});

// Save hours
$("#hoursForm").submit(function(e){
    e.preventDefault();

    $.post("<?= base_url('Pitches/pitch_hours_save'); ?>", $(this).serialize(), function(response){
        let res = JSON.parse(response);
        if(res.status === 'success'){
            $("#hoursMsg").html('<p class="text-success">'+res.message+'</p>');
        } else {
            $("#hoursMsg").html('<p class="text-danger">'+res.message+'</p>');
        }
    });
});

function delete_function(id) {

$.confirm({
    title: 'Delete!',
    content: 'Are you want to delete this data?',
    type: 'red',
    typeAnimated: true,
    buttons: {
        tryAgain: {
            text: 'Yes',
            btnClass: 'btn-red',
            action: function(){
              $.ajax({
          type: "POST",
          url: "<?php echo base_url(); ?>Pitches/Delete/",
          data: {id : id }, 
          success: function(html) {
            $.alert("Your data has been successfully deleted");
            setInterval(function(){
              location.reload();
            }, 3000);
            }
          });
            }
        },
        close: function () {
        }
    }
 });
}

</script>

<style type="text/css">
  table.dataTable thead .sorting, 
table.dataTable thead .sorting_asc, 
table.dataTable thead .sorting_desc {
    background : none;
}
</style>