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

	require_once("../connection.php");
			include "classes/class.phpmailer.php";

	
			$email=mysqli_real_escape_string($connect, $_POST['email']);
			$no_telp=mysqli_real_escape_string($connect, $_POST['phone_number']);
			$password=mysqli_real_escape_string($connect, $_POST['password']);
			$name_store=mysqli_real_escape_string($connect, $_POST['name_store']);
			$currency=mysqli_real_escape_string($connect, $_POST['currency']);
			$full_name=mysqli_real_escape_string($connect, $_POST['full_name']);
			$address=mysqli_real_escape_string($connect, $_POST['address']);
			$referal2=mysqli_real_escape_string($connect, $_POST['referal']);
			$typestore=mysqli_real_escape_string($connect, $_POST['typestore']);
			$decimal=mysqli_real_escape_string($connect, $_POST['decimal']);
			
			
	$setting = mysqli_query($connect,"SELECT * FROM setting");
	$s=mysqli_fetch_array($setting);
	
	$dataupline = mysqli_query($connect,"SELECT * FROM users WHERE afiliasi='$referal2'");
	$up=mysqli_fetch_array($dataupline);
	
	if($no_telp=='000'){
		$referal="";
	}else{
		$referal=$up['phone_number'];
	}

	$cek_email=mysqli_num_rows(mysqli_query($connect,"SELECT email FROM users WHERE email='$email'"));
	$cek_hp=mysqli_num_rows(mysqli_query($connect,"SELECT phone_number FROM users WHERE phone_number='$no_telp'"));
	if ($cek_email > 0){
	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Email 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{
  
    $pass=md5($password);
	$ids=md5($no_telp);
	
	
	$datastore = mysqli_query($connect,"SELECT id_store FROM store ORDER BY id_store DESC LIMIT 1");
	$dt=mysqli_fetch_array($datastore);
	
	$no_aff= strtoupper($afiliasi);


	
	$nama = $name_store;
	$arr = explode(' ', $nama);
	$singkatan = "";
	foreach($arr as $kata)
	{
	$singkatan .= substr($kata, 0, 1);
	}
		$kode= strtoupper($singkatan);
		



	mysqli_query($connect,"INSERT INTO users(full_name,
								  email,
								  password,
								  master,
								  phone_number,
								  address,
								  level,
								  id_session,
								  afiliasi,
								  packages,
								  nominal_upgrade,
								  otp,
								  referal,
								  img) 
                    VALUES('$full_name',
						   '$email',
						   '$pass',
						   '$no_telp',
						   '$no_telp',
						   '$address',
						   'master',
						   '$ids',
						   '$kode$dt[id_store]',
						   '0',
						   '$s[proce_upgrade]',
						   '7110eda4d09e062aa5e4a390b0a572ac0d2c0220',
						   '$referal',
						   'avatar.png')");
						   
	mysqli_query($connect,"INSERT INTO store(name_store,
								  currency,
								  number_store,
								  user,
								  email,
								  nohp,
								  type,
								  address,
								  decimals,
								  typestore) 
                    VALUES('$name_store',
						   '$currency',
						   '$kode-$dt[id_store]',
						   '$no_telp',
						   '$email',
						   '$no_telp',
						   'master',
						   '$address',
						   '$decimal',
						   '$typestore')");
						   
	mysqli_query($connect,"INSERT INTO online_store(name_store,
								  color_store,
								  user,
								  operational_hour,
								  nowa,
								  linkinstagram,
								  subdomain,
								  address_store) 
                    VALUES('$name_store',
						   'Blue',
						   '$no_telp',
						   '',
						   '-',
						   '-',
						   '$kode$dt[id_store]',
						   '$address')");
						   
	$toko = mysqli_query($connect,"SELECT * FROM store WHERE user='$no_telp' AND type='master'");
	$t=mysqli_fetch_array($toko);				   
						   
						   
	mysqli_query($connect,"UPDATE users SET id_store = '$t[id_store]' WHERE id_session = '$ids'");

	
	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Registration Successful, Please Login'
	);
	
    echo json_encode($json);

	


	}
	?>