<?php


	include("connection.php");
	$db = new dbObj();
	$connString =  $db->getConnstring();

	$params = $_REQUEST;

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

	switch($action) {
	 case 'add':
		$empCls->insertEmployee($params);
	 break;
	 case 'edit':
		$empCls->updateEmployee($params);
	 break;
	 case 'delete':
		$empCls->deleteEmployee($params);
	 break;
	 default:
	 $empCls->getEmployees($params);
	 return;
	}
	class Employee {
	protected $conn;
	protected $data = array();
	function __construct($connString) {
		$this->conn = $connString;
	}

	public function getEmployees($params) {
		$this->data = $this->getRecords($params);

		echo json_encode($this->data);
	}

	function insertEmployee($params) {
		$data = array();;
		$sql = "INSERT INTO `employee` (employee_name, employee_salary, employee_age) VALUES('" . $params["name"] . "', '" . $params["salary"] . "','" . $params["age"] . "');  ";

		echo $result = mysqli_query($this->conn, $sql) or die("error to insert employee data");

	}

	function getRecords() {
		$bebek = "w3-red w3-btn";
		//$sid = $_GET['sid']
		$rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
		$sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'id';
		$sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';
		$query = isset($_POST['query']) ? $_POST['query'] : true;
		$qtype = isset($_POST['qtype']) ? $_POST['qtype'] : true;
		$queryx = isset($_POST['queryx']) ? $_POST['queryx'] : true;
		$qtypex = isset($_POST['qtypex']) ? $_POST['qtypex'] : true;
		if (isset($_POST['page'])) { $page  = $_POST['page']; } else { $page=1; };
        $start_from = ($page-1) * $rp;
		$sql = "SELECT * FROM invoiceout where saverec='1' and $qtype like '%$query%' and $qtypex like '%$queryx%'
		order by ".$sortname." ".$sortorder." LIMIT ". $start_from .",".$rp;
		$sqlTot = "SELECT count(id)as totalnya FROM `invoiceout` where saverec='1' and $qtype like '%$query%' and $qtypex like '%$queryx%'";
		$sqljum = "SELECT sum(total)as jumharga FROM `invoiceout` where saverec='1' and $qtype like '%$query%' and $qtypex like '%$queryx%'";
		$qtot = mysqli_query($this->conn, $sqlTot) or die("error to fetch tot employees data");
		$queryRecords = mysqli_query($this->conn, $sql) or die("error to fetch employees data");
		$qtotjum = mysqli_query($this->conn, $sqljum) or die("error to fetch tot employees data");

		while( $row = mysqli_fetch_assoc($queryRecords) ) {
			//$data[] = $row;
			//$data[]['tamb'] = "jajal";

			$data[] = array(
				'tamb'=>'
				<div class="w3-btn w3-blue w3-small w3-round-large" onclick="edit(`'.$row['id'].'`)">Update</div>
				<div class="w3-btn w3-red w3-small w3-round-large" onclick="alert(`'.$row['id'].'`)">Del</div>
				',
				'id'=>$row['id'],
				'deb'=>$row['deb'],
				'job'=>$row['job'],
				'pri'=>$row['pri']
			);

		}

		while( $rw = mysqli_fetch_assoc($qtot) ) {
			$jumlah = $rw['totalnya'];
		}
		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;

	}

	function updateEmployee($params) {
		$data = array();
		//print_R($_POST);die;
		$sql = "Update `employee` set employee_name = '" . $params["edit_name"] . "', employee_salary='" . $params["edit_salary"]."', employee_age='" . $params["edit_age"] . "' WHERE id='".$_POST["edit_id"]."'";

		echo $result = mysqli_query($this->conn, $sql) or die("error to update employee data");
	}

	function deleteEmployee($params) {
		$data = array();
		//print_R($_POST);die;
		$sql = "delete from `employee` WHERE id='".$params["id"]."'";
		//echo $result = mysqli_query($this->conn, $sql) or die("error to delete employee data");
		//echo $params;
	}
}


?>
