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

	$name = $_POST['name'];
	$email = $_POST['email'];
	$password = $_POST['password'];
	$hp = $_POST['hp'];
	$alamat = $_POST['alamat'];
	$lahir = $_POST['lahir'];
	$photo = $_FILES['photo']['name'];
	$tmp = $_FILES['photo']['tmp_name'];

	$query = "SELECT * FROM tbl_user WHERE name_user = '$name' AND email_user = '$email'";
	$check = mysqli_fetch_array(mysqli_query($conn, $query));

	if (isset($check)) {
		$msg = 'Failed create an account';
		echo json_encode($msg);
	}else{
		mysqli_query($conn, "INSERT INTO tbl_user(name_user, email_user, photo_user, password, hp, alamat, lahir) VALUES ('".$name."', '".$email."', '".$photo."', '".$password."', '".$hp."', '".$alamat."', '".$lahir."')");
		move_uploaded_file($tmp, '../image/user/'.$photo);
		$msg = 'Successfully create an account';
		echo json_encode($msg);
		
		//-- Registrasi ke ebook
	    $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://penastudio.id/pi/ebook/public/api/register',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => array(
             'password' => $_POST['password'], 
             'username' => $_POST['name'], 
             'email' => $_POST['email'], 
             'name' => $_POST['name'], 
             'contact_number' => $_POST['hp']
          ),
          CURLOPT_HTTPHEADER => array(
            'Cookie: PHPSESSID= ".$_COOKIE["PHPSESSID"]."'
          ),
        ));
        
        $response = curl_exec($curl);
        curl_close($curl);
        
        
        //-- Registrasi ke akademi
        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://penastudio.id/pi/akademi/api/signup',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => array('first_name' => $name,'last_name' => '-','email' => $email,'password' => $password),
          CURLOPT_HTTPHEADER => array(
            'Cookie: PHPSESSID=".$_COOKIE["PHPSESSID"]."'// ci_session=3444841b41bfff5120064968e5a73a4845bb948a'
          ),
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        //echo $response;
        
        //-- Registrasi ke store
        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://penastudio.id/pi/store/api/v1/auth/registration',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'POST',
          CURLOPT_POSTFIELDS => array('f_name' => $name,'l_name' => '-','email' => $email,'phone' => $hp,'password' =>  $password),
          CURLOPT_HTTPHEADER => array(
            'Cookie: PHPSESSID=".$_COOKIE["PHPSESSID"]."'
          ),
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        //echo $response;
        
	}
?>