<?php
if (isset($_POST['register'])) {
    $post_name = $_POST['name'];
	$post_user = $_POST['user'];
	$post_pass = $_POST['pass'];
	$post_email = $_POST['email'];
	$post_phone = $_POST['phone'];
	$date = date('Y-m-d H:i:s');
	$exp = date('Y-m-d H:i:s');
    if (!$post_name || !$post_email || !$post_user || !$post_pass || !$post_phone){
        $_SESSION['alert'] = ['danger', 'Failed!', 'Masih Ada Form Kosong'];
       exit(header('location: '.base_url('auth/register')));
    }else if (mysqli_num_rows($db->query("SELECT * FROM users WHERE username='$post_user'")) == 1){
       $_SESSION['alert'] = ['danger', 'Failed!', $post_user.' Sudah Terdaftar Sebelumnya'];
       exit(header('location: '.base_url('auth/register')));
    }else if(mysqli_num_rows($db->query("SELECT * FROM users WHERE name='$post_name'")) == 1){
       $_SESSION['alert'] = ['danger', 'Failed!', $post_name.' Sudah Terdaftar Sebelumnya'];
       exit(header('location: '.base_url('auth/register')));
    }else if(mysqli_num_rows($db->query("SELECT * FROM users WHERE email='$post_email'")) == 1){
       $_SESSION['alert'] = ['danger', 'Failed!', $post_email.' Sudah Terdaftar Sebelumnya'];
       exit(header('location: '.base_url('auth/register')));
    }else if(mysqli_num_rows($db->query("SELECT * FROM users WHERE phone='$post_phone'")) == 1){
       $_SESSION['alert'] = ['danger', 'Failed!', $post_phone.' Sudah Terdaftar Sebelumnya'];
       exit(header('location: '.base_url('auth/register')));
    }else{
		$api = random(64);
		$api_id = random(15);
		$password = password_hash($post_pass, PASSWORD_DEFAULT);
		$db->query("INSERT INTO users VALUES('','$post_name', '$post_email','$post_phone','$post_user', '$password', '0', '0','Member','active','YES','$api','$api_id','0','0','$date','$exp')");
		$_SESSION['alert'] = ['success', 'Success!', 'Akun Berhasil Dibuat'];
	    exit(header('location: '.base_url('auth/login')));
    }
}