
<?= $this->extend('Page_layout') ?>
<?= $this->section('content') ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Finance Rate</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
            
              <li class="breadcrumb-item active">Finance Rate</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">

      <!-- Default box -->
      <div class="card card-primary">
        <div class="card-header">
          <h3 class="card-title">Database finance rate filter</h3>

          <div class="card-tools">
            <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
              <i class="fas fa-minus"></i>
            </button>
            <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
              <i class="fas fa-times"></i>
            </button>
          </div>
        </div>
        <div class="card-body">
          

             <div class="col-12" style="overflow-x:scroll">
             <table id="table-data" style="width:1000px;" class="table table-bordered table-hover">
                  <thead>
                  <tr>
                    <th>Action</th>
                    <th>ID</th>
                    <th>Talent</th>
                    <th>Year</th>
                    <th>IGS</th>
                    <th>IG Photo</th>
                    <th>IG Vid</th>
                    <th>IG Live</th>
                    <th>Twitter</th>
                    <th>Youtube</th>
                    <th>Ecommerce Live</th>
                    <th>Event Attendance</th>
                 
                    
                  </tr>
                  </thead>
                  <tbody>
                 
                </table>
                 </div>
        </div>
        <!-- /.card-body -->
        <div class="card-footer">
           <button type="submit" id="btn-add-form" class="btn btn-info" data-toggle="modal" data-target="#form-add">Add new</button>
        </div>
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  </div>
  <?php include('Finance_add.php');?>
   <?php include('Finance_edit.php');?>


   <?= $this->endSection() ?> 

  <?=$this->section("scripts")?>
<script src="<?=base_url('public/plugins/jquery-ui/jquery-ui.js')?>"></script>  
<script src="<?=base_url('public/plugins/datatables/jquery.dataTables.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-bs4/js/dataTables.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-responsive/js/dataTables.responsive.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-responsive/js/responsive.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/dataTables.buttons.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.bootstrap4.min.js')?>"></script>
<script src="<?=base_url('public/plugins/jszip/jszip.min.js')?>"></script>
<script src="<?=base_url('public/plugins/pdfmake/pdfmake.min.js')?>"></script>
<script src="<?=base_url('public/plugins/pdfmake/vfs_fonts.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.html5.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.print.min.js')?>"></script>
<script src="<?=base_url('public/plugins/datatables-buttons/js/buttons.colVis.min.js')?>"></script>
<script src="<?=base_url('public/plugins/money-format/simple.money.format.js')?>"></script>

<script>
  $(function () {
   
   

  });

   $('.currency').simpleMoneyFormat();
    function numberOnly(selector) {
            selector.value = selector.value.replace(/[^0-9]/g, '');
         }


  function getsessionform(){
  var $sessionlevel = "<?php echo $_SESSION['USER_LEVEL'] ?>";
     if($sessionlevel == "TALENT" )
            {
               $("#btn-add-form").hide();
                $tabledata =  $("#table-data").DataTable();
                $tabledata.column(0).visible( false );
            }
          

       
  }


  


   function loadrate(){
   
     $.ajax({
            type: "GET",
            url: "<?= site_url('finance/fetch')?>",
        
            success: function (response) {
            //  console.log(response.data);
             $.each(response.data, function (key, value) { 
             
              var body = "<tr>";
              body += "<td><div class=\"btn-group\"><a  class=\"btn btn-info btn-sm edit-btn\">Edit</a>";
              body += "</td>";
              body += "<td class=\"data-id\">"+value['RATE_ID']+"</td>";
              body += "<td >"+value['XNAME']+"</td>";
              body += "<td>"+value['R_YEAR']+"</td>";
              body += "<td>"+value['IGS']+"</td>";
              body += "<td>"+value['IG_PHOTO']+"</td>";
              body += "<td>"+value['IG_VIDEO']+"</td>";
              body += "<td>"+value['IG_LIVE']+"</td>";
              body += "<td>"+value['TWITTER']+"</td>";
              body += "<td>"+value['YOUTUBE']+"</td>";
              body += "<td>"+value['ECOM']+"</td>";
              body += "<td>"+value['EVENT_ATT']+"</td>";
              
            

              
              body += "</tr>";
              $("#table-data tbody").append(body);
               
             });
              $("#table-data").DataTable();
              getsessionform();
          
            }
          });

       
  }      

  function loadtalent(param){
  
    
      if(param == "new")
      {
        var ddl_username = $("#talent");
       
      }else
      {
        var ddl_username = $("#etalent");  
       
      }
       $.ajax({
            type: "GET",
            url: "<?= site_url('finance/talent/fetch')?>",
           
        
            success: function (response) {
           
             ddl_username.empty().append('<option selected="selected" value="">Please select</option>');
          
             $.each(response.data, function (key, value) { 
               ddl_username.append($("<option></option>").val(value['TALENT_ID']).html(value['XNAME']));
               
             });
             
            }
          });

       
  }

  $(document).ready(function () {
    loadtalent("new");
    loadrate();
 

    $(document).on('click','.save-data', function () {
       if( $("#talent").val() == "" || $("#year").val() == "" || $("#igs").val() == "" || $("#igphoto").val() == "" || $("#igvid").val() == "" || $("#iglive").val() == "" || $("#twitter").val() == "" || $("#youtube").val() == "" || $("#ecommerce").val() == "-" || $("#event").val() == "" )
       {
        // Display an info toast with no title
          toastr.error("Mohon isi form dengan lengkap !")
          return false;
       }
       else
       { // jika sudah di isi semua.
         
          var $data = {
            'TALENT_ID' : $("#talent").val(),
            'R_YEAR' : $("#year").val(),
            'IGS' : $("#igs").val(),
            'IG_PHOTO' : $("#igphoto").val(),
            'IG_VIDEO' : $("#igvid").val(),
            'IG_LIVE' : $("#iglive").val(),
            'TWITTER' : $("#twitter").val(),
            'YOUTUBE' : $("#youtube").val(),
            'ECOM' : $("#ecommerce").val(),
            'EVENT_ATT' : $("#event").val()
            

          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('finance/simpan')?>",
            data: $data,
           
            success: function (response) {
              $('#form-add').modal('hide');
              $('#form-add').find('input').val('');
               $("#table-data tbody").html("");
              loadtalent("new");
              loadrate();
              toastr.success(response.status);
            
          
           
            }
          });
         

       }
    });

    $(document).on('click','.edit-btn', function () {
      var id = $(this).closest('tr').find('.data-id').text();
      loadtalent("edit");
     
    $.ajax({
      type: "POST",
      url: "<?= site_url('finance/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
    
       $.each(response, function (key, value) { 
       
            $('#form-edit').find('input').val('');
            $("#etalent").val(value['TALENT_ID']).change(),
            $("#eyear").val(value['R_YEAR']),
            $("#eigs").val(value['IGS']),
            $("#eigphoto").val(value['IG_PHOTO']),
            $("#eigvid").val(value['IG_VIDEO']),
            $("#eiglive").val(value['IG_LIVE']),
            $("#etwitter").val(value['TWITTER']),
            $("#eyoutube").val(value['YOUTUBE']),
            $("#eecommerce").val(value['ECOM']),
            $("#eevent").val(value['EVENT_ATT']),
           
            $("#efinance_id").val(id),
           $("#form-edit").modal("show");
           
          
       });
      }
    });
  });


  });

  // confirm Update data
  $(document).on('click','.edit-data', function () {
         if( $("#etalent").val() == "" || $("#eyear").val() == "" || $("#eigs").val() == "" || $("#eigphoto").val() == "" || $("#eigvid").val() == "" || $("#eiglive").val() == "" || $("#etwitter").val() == "" || $("#eyoutube").val() == "" || $("#eecommerce").val() == "-" || $("#eevent").val() == "" )
       {
        // Display an info toast with no title
          toastr.error("Mohon isi form dengan lengkap !")
          return false;
       }
       else
       { // jika sudah di isi semua.

         var $data = {
            'TALENT_ID' : $("#etalent").val(),
            'R_YEAR' : $("#eyear").val(),
            'IGS' : $("#eigs").val(),
            'IG_PHOTO' : $("#eigphoto").val(),
            'IG_VIDEO' : $("#eigvid").val(),
            'IG_LIVE' : $("#eiglive").val(),
            'TWITTER' : $("#etwitter").val(),
            'YOUTUBE' : $("#eyoutube").val(),
            'ECOM' : $("#eecommerce").val(),
            'EVENT_ATT' : $("#eevent").val(),
            'RATE_ID' : $("#efinance_id").val(),
            

          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('finance/update')?>",
            data: $data,
           
            success: function (response) {
              $('#form-edit').modal('hide');
              $('#form-edit').find('input').val('');
              
              loadtalent("edit");
              loadrate();
           

               $("#table-data tbody").html("");
               
              toastr.success(response.status);
              
           
            }
          });
         

       }
    });

</script>

<?=$this->endSection()?>
