<?
	//include"../plg/ses.php";
	include"../plg/connection.php";
	$db = new dbObj();
	$connString =  $db->getConnstring();

	$params = $_REQUEST;

	$action = isset($params['action']) != '' ? $params['action'] : '';
	$empCls = new Piutang($connString);

	switch($action) {
	 default:
	 $empCls->getPiutang($params);
	 return;
	}

	class Piutang {
	protected $conn;
	protected $data = array();
	function __construct($connString) {
		$this->conn = $connString;
	}

	public function getPiutang($params) {
		$this->data = $this->getRecords($params);
		echo json_encode($this->data);
	}

	function getRecords() {
		$rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
		$sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'kode';
		$sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'asc';
		$query = isset($_POST['query']) ? $_POST['query'] : true;
		$qtype = isset($_POST['qtype']) ? $_POST['qtype'] : true;
		$query1 = isset($_POST['query1']) ? $_POST['query1'] : true;
		$qtype1 = isset($_POST['qtype1']) ? $_POST['qtype1'] : true;
		//$query2 = isset($_POST['query2']) ? $_POST['query2'] : true;
		//$qtype2 = isset($_POST['qtype2']) ? $_POST['qtype2'] : true;

		if (isset($_POST['page'])) {
			$page  = $_POST['page'];
		} else {
			$page=1;
		};

    $start_from = ($page-1) * $rp;

		$pencari = "del='0' and $qtype like '%$query%' and $qtype1 like '%$query1%'";
		$i =1;
		$sql = "SELECT * FROM masterkpi where $pencari
		order by ".$sortname." ".$sortorder." LIMIT ". $start_from .",".$rp;
		$queryRecords = mysqli_query($this->conn, $sql) or die("gagal");
		while( $row = mysqli_fetch_assoc($queryRecords) ) {
			$data[] = array(
				'nomor'=>'<div class="w3-small">'.$i++.'</div>',
				'id'=>'<div class="w3-small">'.$row['id'].'</div>',
				'kode'=>'<div class="w3-small">'.$row['kode'].'</div>',
				'kpi'=>'<div class="w3-small">'.$row['kpi'].'</div>',
				'pola'=>'<div class="w3-small">'.$row['pola'].'</div>',
				'des'=>'<div class="w3-small">'.$row['des'].'</div>',
				'sat'=>'<div class="w3-small">'.$row['sat'].'</div>',
				'fok'=>'<div class="w3-small">'.$row['fok'].'</div>',
				'area'=>'<div class="w3-small">'.$row['area'].'</div>',
				'kodejab'=>'<div class="w3-small">'.$row['kodejab'].'</div>',
				'tomb'=>'<div class="w3-small w3-blue w3-btn w3-round-large w3-card-4" onclick="editkan(`'.$row['id'].'`)">Update</div>&nbsp;
					<div class="w3-small w3-red w3-btn w3-round-large w3-card-4" onclick="hapuskan(`'.$row['id'].'`,`'.$row['kode'].'`)">Deleted</div>'
			);
		}

		$sqlTot = "SELECT count(id)as totalnya FROM `masterkpi` where $pencari";
		$qtot = mysqli_query($this->conn, $sqlTot) or die("gagal");
		while( $rw = mysqli_fetch_assoc($qtot) ) {
			$jumlah = $rw['totalnya'];
		}
		/*
		$sqljum = "SELECT count(nip)as jumharga FROM `karyawan` where $pencari";
		$qtotjum = mysqli_query($this->conn, $sqljum) or die("gagal");
		while( $nil = mysqli_fetch_assoc($qtotjum) ) {
			$harga = $nil['jumharga'];
		}
		*/
		$json_data = array(
			"page" => $page,
			"total" => $jumlah,
			//"harga" => number_format($harga),
			"rows" => $data  // total data array
		);

		return $json_data;

	}
}


?>
