
<?= $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>Master Talent</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
            
              <li class="breadcrumb-item active">Master Talent</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
    <input type="hidden" id="hdn_manager">
    <input type="hidden" id="hdn_user">

      <!-- Default box -->
      <div class="card card-primary">
        <div class="card-header">
          <h3 class="card-title">Talent Data</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:1800px;" class="table table-bordered table-hover">
                  <thead>
                  <tr>
                    <th>Action</th>
                    <th>No</th>
                    <th>Name</th>
                    <th>Nickname</th>
                    <th>Phone number</th>
                    <th>Email</th>
                    <th>Address</th>
                    <th>Birthdate</th>
                    <th>Instagram</th>
                    <th>Youtube</th>
                    <th>Tiktok</th>
                    <th>Contract end date</th>
                    
                  </tr>
                  </thead>
                  <tbody>
                                    
                  
                  </tbody>
                  
                </table>
                 </div>
        </div>
        <!-- /.card-body -->
        <div class="card-footer">
           <button type="submit" class="btn btn-danger" data-toggle="modal" data-target="#form-add">Add new</button>
        </div>
        <!-- /.card-footer-->
      </div>
      <!-- /.card -->

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
  </div>
  <?php include('Talent_add.php');?>
   <?php include('Talent_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/moment/moment.min.js')?>"></script>

<script>
  $(function () {

    $( "#x_birth" ).datepicker({ dateFormat: 'dd/mm/yy'});
    $( "#contract_start_dt" ).datepicker({ dateFormat: 'dd/mm/yy'});
    $( "#contract_end_dt" ).datepicker({ dateFormat: 'dd/mm/yy'});

    $( "#ex_birth" ).datepicker({ dateFormat: 'dd/mm/yy'});
    $( "#econtract_start_dt" ).datepicker({ dateFormat: 'dd/mm/yy'});
    $( "#econtract_end_dt" ).datepicker({ dateFormat: 'dd/mm/yy'});
  });


  function loadtalent(){
   
     $.ajax({
            type: "GET",
            url: "<?= site_url('talent/fetch')?>",
        
            success: function (response) {
            //  console.log(response.data);
             $.each(response.data, function (key, value) { 

              var $start_dt, $end_dt;


              $end_dt = "";

              if(value['CONTRACT_START_DT'] != null){
                $start_dt = moment(value['CONTRACT_START_DT']).format("DD/MM/YYYY");
              }
              if(value['CONTRACT_END_DT'] != null){
                $end_dt = moment(value['CONTRACT_END_DT']).format("DD/MM/YYYY");
              }
             
              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['TALENT_ID']+"</td>";
              body += "<td >"+value['XNAME']+"</td>";
              body += "<td>"+value['NICKNAME']+"</td>";
              body += "<td>"+value['PHONE_NUMBER']+"</td>";
              body += "<td>"+value['EMAIL']+"</td>";
              body += "<td>"+value['ADRESS']+"</td>";
              body += "<td>"+ moment(value['BIRTHDATE']).format("DD/MM/YYYY")+"</td>";
              body += "<td>"+value['IG']+"</td>";
              body += "<td>"+value['YOUTUBE']+"</td>";
              body += "<td>"+value['TIKTOK']+"</td>";
              body += "<td>"+$end_dt+"</td>";
            

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

       
  }

  function loaduser(param){
  
    
      if(param == "new")
      {
        var ddl_username = $("#username");
        var $data = {
               'KONDISI' : ' USER_LEVEL IN ("TALENT_MANAGER","TALENT") and USER_NAME not in (select USER_NAME from ref_talent )'
            }  
      }else
      {
        var ddl_username = $("#eusername");  
        var $data = {
               'KONDISI' : ' USER_LEVEL IN ("TALENT_MANAGER","TALENT")'
            }
      }
        
     $.ajax({
            type: "POST",
            url: "<?= site_url('user/fetch/where')?>",
            data: $data,
        
            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['USER_LEVEL']).html(value['USER_NAME']));
               
             });
             if($("#hdn_user").val() != "")
             {
                 $("#euser").val($("#hdn_user").val());
             }
             
            }
          });

       
  }
  function loadmanager(param){
 
    var $data = {
               'KONDISI' : ' USER_LEVEL IN ("TALENT_MANAGER")'
            }
       if(param == "new")
       {     
       var ddl = $("#manager");
       }else
       {
        var ddl = $("#emanager");
       }      
     $.ajax({
            type: "POST",
            url: "<?= site_url('user/fetch/where')?>",
            data: $data,
        
            success: function (response) {
             ddl.empty().append('<option selected="selected" value="">Please select</option>');
          
             $.each(response.data, function (key, value) { 
               ddl.append($("<option></option>").val(value['USER_NAME']).html(value['USER_NAME']));
               
             });
             if($("#hdn_manager").val() != "")
             {
                 $("#emanager").val($("#hdn_manager").val());
             }


             
            }
          });

       
  }

  $("#username").change(function (e) { 
    e.preventDefault();
    //toastr.error($("#username").children("option").filter(":selected").text())
    if($("#username").val()== "TALENT")
    {
      loadmanager("new");
       $( "#manager" ).prop( "disabled", false );

    }else
    {
       $( "#manager" ).prop( "disabled", true );
    }
  });

  $(document).ready(function () {
    loadtalent();
   loadmanager("new");
   loaduser("new");
    $(document).on('click','.save-data', function () {
       if( $("#username").val() == "" || $("#name").val() == "" || $("#phone").val() == "" || $("#email").val() == "" || $("#address").val() == "" || $("#x_birth").val() == "" || $("#x_birth").val() == "" || $("#instagram").val() == "" || $("#twitter").val() == "" || $("#youtube").val() == "" || $("#tiktok").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 = {
            'XNAME' : $("#name").val(),
            'NICKNAME' : $("#nickname").val(),
            'PHONE_NUMBER' : $("#phone").val(),
            'EMAIL' : $("#email").val(),
            'ADRESS' : $("#address").val(),
            'BIRTHDATE' : $("#x_birth").val(),
            'IG' : $("#instagram").val(),
            'TWITTER' : $("#twitter").val(),
            'YOUTUBE' : $("#youtube").val(),
            'TIKTOK' : $("#tiktok").val(),
            'NOTES' : $("#notes").val(),
            'CONTRACT_START_DT' : $("#contract_start_dt").val(),
            'CONTRACT_END_DT' : $("#contract_end_dt").val(),
            'FILE_ID' : $("#x_photo").val(),
            'USER_NAME' : $("#username").children("option").filter(":selected").text(),
            'PARENT_ID' : $("#manager").val()
           
          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('talent/simpan')?>",
            data: $data,
           
            success: function (response) {
              $('#form-add').modal('hide');
              $('#form-add').find('input').val('');
              $("#manager").empty();
              loaduser("new");
              loadtalent();
           

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

       }
    });
  });

  $(document).on('click','.edit-btn', function () {
      var id = $(this).closest('tr').find('.data-id').text();
        loaduser("edit");
        loadmanager("edit");
    $.ajax({
      type: "POST",
      url: "<?= site_url('talent/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
    
       $.each(response, function (key, value) { 

        var $start_dt, $end_dt;

        if(value['CONTRACT_START_DT'] != null){
          $start_dt = moment(value['CONTRACT_START_DT']).format("DD/MM/YYYY");
        }
        if(value['CONTRACT_END_DT'] != null){
          $end_dt = moment(value['CONTRACT_END_DT']).format("DD/MM/YYYY");
        }



       
          $('#form-edit').find('input').val('');
           $("#ename").val(value['XNAME']),
           $("#enickname").val(value['NICKNAME']),
           $("#ephone").val(value['PHONE_NUMBER']),
           $("#eemail").val(value['EMAIL']),
           $("#eaddress").val(value['ADRESS']),
           $("#ex_birth").val(moment(value['BIRTHDATE']).format("DD/MM/YYYY")),
           $("#einstagram").val(value['IG']),
           $("#etwitter").val(value['TWITTER']),
           $("#eyoutube").val(value['YOUTUBE']),
           $("#etiktok").val(value['TIKTOK']),
           $("#enotes").val(value['NOTES']),
           $("#econtract_start_dt").val($start_dt),
           $("#econtract_end_dt").val($end_dt),
           $("#ex_photo").val(value['FILE_ID']),
           $("#eusername option").filter(function() { return $(this).text() == value['USER_NAME'] }).prop('selected', true),
           $("#emanager").val(value['PARENT_USERNAME']).change(),
           $("#etalent_id").val(id),
           $("#hdn_manager").val(value['PARENT_USERNAME']),
           $("#hdn_user").val(value['USER_NAME']),
           $("#form-edit").modal("show");
          
          
       });
        if($("#eusername").val() == "TALENT")
          {
             $( "#emanager" ).prop( "disabled", false );
          }else 
          {
             $( "#emanager" ).prop( "disabled", true );
          }

          
      }
    });
  });

  // confirm Update data
  $(document).on('click','.edit-data', function () {
      if( $("#eusername").val() == "" || $("#ename").val() == "" || $("#ephone").val() == "" || $("#eemail").val() == "" || $("#eaddress").val() == "" || $("#ex_birth").val() == "" || $("#ex_birth").val() == "" || $("#einstagram").val() == "" || $("#twitter").val() == "-" || $("#eyoutube").val() == "" || $("#etiktok").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 = {
            'XNAME' : $("#ename").val(),
            'NICKNAME' : $("#enickname").val(),
            'PHONE_NUMBER' : $("#ephone").val(),
            'EMAIL' : $("#eemail").val(),
            'ADRESS' : $("#eaddress").val(),
            'BIRTHDATE' : $("#ex_birth").val(),
            'IG' : $("#einstagram").val(),
            'TWITTER' : $("#etwitter").val(),
            'YOUTUBE' : $("#eyoutube").val(),
            'TIKTOK' : $("#etiktok").val(),
            'NOTES' : $("#enotes").val(),
            'CONTRACT_START_DT' : $("#econtract_start_dt").val(),
            'CONTRACT_END_DT' : $("#econtract_end_dt").val(),
            'FILE_ID' : $("#ex_photo").val(),
            'USER_NAME' : $("#eusername").children("option").filter(":selected").text(),
            'PARENT_ID' : $("#emanager").val(),
            'TALENT_ID' : $("#etalent_id").val()
           
          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('talent/update')?>",
            data: $data,
           
            success: function (response) {
              $('#form-edit').modal('hide');
              $('#form-edit').find('input').val('');
              $("#emanager").empty();
              loaduser("new");
              loadtalent();
           

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

       }
    });


 

</script>

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