	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	*/

	require_once("../connection.php");
	
	$key=mysqli_real_escape_string($connect, $_POST['key']);
	$email=mysqli_real_escape_string($connect, $_POST['email']);
	$telpon=mysqli_real_escape_string($connect, $_POST['telephone']);
	$nama_pelanggan=mysqli_real_escape_string($connect, $_POST['name_customer']);
	$alamat=mysqli_real_escape_string($connect, $_POST['address']);

	$getusers = mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	      = mysqli_fetch_array($getusers);
	$jumlah   = mysqli_num_rows($getusers);
	
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	$cek_email	= mysqli_num_rows(mysqli_query($connect,"SELECT email FROM customer WHERE email='$email'"));
	$cek_hp		= mysqli_num_rows(mysqli_query($connect,"SELECT telephone FROM customer WHERE address='$telpon'"));


	if ($jumlah > 0){
	
	if ($cek_email > 0){
		
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Email is already registered'
	);
    echo json_encode($json);
	
	}else if ($cek_hp > 0){
		
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'HP No. Registered'
	);
    echo json_encode($json);
	
	}else{
	mysqli_query($connect,"INSERT INTO customer(name_customer,
								  email,
								  telephone,
								  address,
								  img,
                                  user) 
                    VALUES('$nama_pelanggan',
						   '$email',
						   '$telpon',
						   '$alamat',
						   'avatar.png',
                           '$m[phone_number]')");
  



	$datapelanggan = mysqli_query($connect,"SELECT id_customer FROM customer WHERE user='$m[phone_number]' ORDER BY id_customer DESC LIMIT 1");
	$p=mysqli_fetch_array($datapelanggan);

	$query = "SELECT * FROM customer WHERE user='$m[phone_number]' AND id_customer='$p[id_customer]'";
	$hasil = mysqli_query($connect,$query);

	$response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
  $response["data"] = array();
  while($x = mysqli_fetch_array($hasil)){
    $h['id_customer'] 		= $x["id_customer"];
    $h['name_customer'] 	= $x["name_customer"];
    $h['address'] 			= $x["address"];
    $h['email'] 			= $x["email"];
    $h['telephone'] 			= $x["telephone"];
    array_push($response["data"], $h);
  }
  echo json_encode($response);


	}

	}else{
	
	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '00',
	'msg' 		=> 'User Not Found'
	);

    echo json_encode($json);	
	}
	?>