
<?= $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>Brand Data</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
            
              <li class="breadcrumb-item active">Master Brand</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">Brand 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:1000px;" class="table table-bordered table-hover">
                  <thead>
                  <tr>
                    <th>Action</th>
                    <th>No</th>
                    <th>Brand name</th>
                    <th>Instagram</th>
                    <th>Pic name</th>
                    <th>IG PIC</th>
                    <th>Phone number</th>
                    <th>PIC Address</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('Brand_add.php');?>
    <?php include('Brand_edit.php');?>
   <?php include('Brand_delete.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>

    $(document).ready(function () {
    loaddata();


    });


  function loaddata(){
   
     $.ajax({
            type: "GET",
            url: "<?= site_url('brand/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 += "<a href=\"#\" class=\"btn btn-danger btn-sm delete-btn\">Delete</a></div></td>";
              body += "<td class=\"data-id\">"+value['BRAND_ID']+"</td>";
              body += "<td >"+value['XNAME']+"</td>";
              body += "<td>"+value['IG']+"</td>";
              body += "<td>"+value['PIC_NAME']+"</td>";
              body += "<td>"+value['IG_PIC']+"</td>";
              body += "<td>"+value['PHONE_NUMBER']+"</td>";
              body += "<td>"+value['PIC_ADDRESS']+"</td>";
              body += "</tr>";
              $("#table-data tbody").append(body);
               
             });
              $("#table-data").DataTable();
            }
          });

       
  }


  $(document).on('click','.edit-btn', function () {
      var id = $(this).closest('tr').find('.data-id').text();
    $.ajax({
      type: "POST",
      url: "<?= site_url('brand/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
      
       $.each(response, function (key, dataval) { 
       
          $('#form-edit').find('input').val('');
          $("#ename").val(dataval['XNAME']);
          $("#einstagram").val(dataval['IG']);
          $("#epic_name").val(dataval['PIC_NAME']);
          $("#eig_pic").val(dataval['IG_PIC']);
          $("#ephone").val(dataval['PHONE_NUMBER']);
          $("#eaddress").val(dataval['PIC_ADDRESS']);
          $("#ebrand_id").val(id);
          $("#form-edit").modal("show");
       });
      }
    });

    });


     // confirm Update data
  $(document).on('click','.edit-data', function () {
       if( $("#ename").val() == "" || $("#einstagram").val() == "" || $("#epic_name").val() == "" || $("#eig_pic").val() == "" || $("#ephone").val() == "" || $("#eaddress").val() == "")
       {
        // Display an info toast with no title
          toastr.error("Mohon isi form dengan lengkap !")
          return false;
       }
       else
       { // jika sudah di isi semua.

          
          var $edata = {
            'XNAME' : $("#ename").val(),
            'IG' : $("#einstagram").val(),
            'PIC_NAME' : $("#epic_name").val(),
            'IG_PIC' : $("#eig_pic").val(),
            'PHONE_NUMBER' : $("#ephone").val(),
            'PIC_ADDRESS' : $("#eaddress").val(),
            'BRAND_ID' : $("#ebrand_id").val(),       
          };
    
          $.ajax({
            type: "POST",
            url: "<?= site_url('brand/update')?>",
            data: $edata,
           
            success: function (response) {
              $('#form-edit').modal('hide');
              $('#form-edit').find('input').val('');

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

       }
    });


  $(document).on('click','.save-data', function () {
       if( $("#name").val() == "" || $("#instagram").val() == "" || $("#pic_name").val() == "" || $("#ig_pic").val() == "" || $("#phone").val() == "" || $("#address").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(),
            'IG' : $("#instagram").val(),
            'PIC_NAME' : $("#pic_name").val(),
            'IG_PIC' : $("#ig_pic").val(),
            'PHONE_NUMBER' : $("#phone").val(),
            'PIC_ADDRESS' : $("#address").val()        
           
          };
          $.ajax({
            type: "POST",
            url: "<?= site_url('brand/simpan')?>",
            data: $data,
           
            success: function (response) {
              $('#form-add').modal('hide');
              $('#form-add').find('input').val('');

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

       }
    });


    $(document).on('click','.delete-btn', function () {
     var id = $(this).closest('tr').find('.data-id').text();
     $.ajax({
      type: "POST",
      url: "<?= site_url('brand/edit')?>",
      data: {'id' : id},
    
      success: function (response) {
    
       $.each(response, function (key, dataval) { 
          $("#d_data").val(id);
          $(".d_name").text(dataval['XNAME'] + "[" +id+ "]");
          $("#form-delete").modal("show");
       });
      }
    });

    });

    $(document).on('click','.delete-data', function () {
      
          var $ddata = {
            'BRAND_ID' : $("#d_data").val(),     
          };
    
          $.ajax({
            type: "POST",
            url: "<?= site_url('brand/delete')?>",
            data: $ddata,          
            success: function (response) {
              $('#form-delete').modal('hide');
              $("#table-data tbody").html("");
              loaddata();
              toastr.success(response.status);
              
           
            }
          });
    });

</script>

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