<?php
 
include 'dbconfig.php';
 
// Create connection

//$date1=$_GET['date1'];
//$date2=$_GET['date2'];
//$kodecab=$_GET['kodecab'];
//$item_code=$_GET['item_code'];
$ruser=$_POST['ruser'];
$rpass=$_POST['rpass'];
$hak=$_POST['hak'];
$tipe=$_POST['tipe'];

//echo $notrans;

$conn = mysqli_connect($servername, $username, $password, $dbname);
 
// Check connection
 
if (!$conn) {
 
    die("Connection failed: " . mysqli_connect_error());
 
}

$check=mysqli_query($conn,"select * from TbUser where user='$ruser'");
if (mysqli_num_rows($check)>0 && $tipe=='New')
{
    $result['message']='double';
    echo json_encode($result);
    return;
}



if ($tipe=='Delete')
{ 
$sql1="delete from  TbUser where user='$ruser'";

$response=mysqli_query($conn,$sql1) or die('ERROR '.$sql1) ;

 if ($response==1)
 {
  $result['message']='ok';
  echo json_encode($result);
 }
return;
}


if ($tipe=='New')
{ 
$sql1="insert into TbUser (user,password,hak) values ('$ruser','$rpass','$hak') ";

$response=mysqli_query($conn,$sql1) or die('ERROR '.$sql1) ;

 if ($response==1)
 {
  $result['message']='ok';
  echo json_encode($result);
 }
return;
}

if ($tipe=='Edit')
{ 
$sql1="update TbUser set password='$rpass',hak='$hak' where user ='$ruser' ";

$response=mysqli_query($conn,$sql1) or die('ERROR '.$sql1) ;

 if ($response==1)
 {
  $result['message']='ok';
  echo json_encode($result);
 }
return;
}
 
if ($tipe=='View')
{ 

 
$sql3="select * from TbUser where user like '%".$ruser."%' order by user asc";
$res3 = mysqli_query($conn, $sql3);
$sql41=mysqli_query($conn,"select * from TbUser where user like '%".$ruser."%' order by user asc");
$sql42=mysqli_num_rows($sql41);

 $items = array();
 
 if (mysqli_num_rows($res3) > 0) {
 
    while($row = mysqli_fetch_object($res3)){
 
        array_push($items, $row);
 
//$result["errorcode"] = "1";
 
    $result["data"] = $items;
 
}}
else
{
 
       $result["errormsg"] = "failed";
 
}
 
 $result["jum"] = strval($sql42); 
echo json_encode($result);
 
mysqli_close($conn);
}
 
?>