<?php 
include 'aksi/koneksi.php';
$cabang = $_GET['cabang'];

// Database connection info 
$dbDetails = array( 
    'host' => $servername, 
    'user' => $username, 
    'pass' => $password, 
    'db'   => $db
); 
 
// DB table to use 
// $table = 'members'; 
$table = <<<EOT
 (
    SELECT 
      a.barang_id, 
      a.barang_kode,
      a.barang_deskripsi,
      a.barang_nama,
      a.barang_kategori_id, 
      a.barang_status, 
      a.barang_harga,
      a.barang_harga_beli,
      a.barang_stock,
      a.barang_terjual,
      a.barang_tanggal,
      a.barang_cabang,
      b.kategori_id,
      b.kategori_nama
    FROM barang a
    LEFT JOIN kategori b ON a.barang_kategori_id = b.kategori_id
 ) temp
EOT;
 
// Table's primary key 
$primaryKey = 'barang_id'; 
 
// Array of database columns which should be read and sent back to DataTables. 
// The `db` parameter represents the column name in the database.  
// The `dt` parameter represents the DataTables column identifier. 
$columns = array( 
    array( 'db' => 'barang_id', 'dt'              => 0 ),
    array( 'db' => 'barang_kode', 'dt'            => 1 ), 
    array( 'db' => 'barang_deskripsi', 'dt'            => 2 ),
    array( 'db' => 'barang_nama', 'dt'            => 3 ), 
    array( 'db' => 'kategori_nama',  'dt'         => 4 ), 
    array( 'db' => 'barang_harga',      'dt'      => 5 ),
    array( 'db' => 'barang_harga_beli',      'dt'      => 6 ),
    array( 'db' => 'barang_stock',      'dt'      => 7 ),
     array( 'db' => 'barang_terjual',      'dt'      => 8 ),
    array( 'db' => 'barang_tanggal',      'dt'      => 9 ),
    array( 
        'db'        => 'barang_status', 
        'dt'        => 10, 
        'formatter' => function( $d, $row ) { 
            // Ternary Operator
            return ($d == 1) ? "<span class='badge badge-success'>Dijual</span>" : 
            ($d == 2 ? "<span class='badge badge-info'>Khusus Servis</span>" : 
            ($d == 3 ? "<span class='badge badge-primary'>Dijual & Untuk Sevis</span>" :  
            "<span class='badge badge-danger'>Tidak Dijual<span>")); 
        } 
    ) 
); 

// Include SQL query processing class 
require 'aksi/ssp.php'; 

// require('ssp.class.php');

// Output data as json format 
echo json_encode( 
    SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns, null, "barang_cabang = $cabang " )
    // SSP::simple( $_GET, $dbDetails, $table, $primaryKey, $columns)

);