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

	require_once("../connection.php");
	include "classes/class.phpmailer.php";
	
		$key=mysqli_real_escape_string($connect, $_POST['key']);
		$email=mysqli_real_escape_string($connect, $_POST['email']);
		$no_telp=mysqli_real_escape_string($connect, $_POST['phone_number']);
		$nama_lengkap=mysqli_real_escape_string($connect, $_POST['full_name']);
		$id_toko=mysqli_real_escape_string($connect, $_POST['id_store']);
		$alamat=mysqli_real_escape_string($connect, $_POST['address']);
		$level=mysqli_real_escape_string($connect, $_POST['level']);
		$gaji_tetap=mysqli_real_escape_string($connect, $_POST['salary_fixed']);
		$komisi=mysqli_real_escape_string($connect, $_POST['commission']);
		$tunjangan=mysqli_real_escape_string($connect, $_POST['allowance']);
		$potongan=mysqli_real_escape_string($connect, $_POST['piece']);


	$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 users WHERE email='$email'"));
	$cek_hp=mysqli_num_rows(mysqli_query($connect,"SELECT phone_number FROM users WHERE phone_number='$no_telp'"));
	
	$cekstaff=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM users WHERE master='$u[master]' AND blokir ='N'"));


	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' 		=> 'Phone No. Registered'
	);
    echo json_encode($json);
	
	}else if($cekstaff > 1 AND $m['packages']==0){
		
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Maximum 1 employee, Please upgrade Premium'
	);
    echo json_encode($json);	
		
		
	}else{
	
	include "../fungsi_thumb.php";
	
	$folder = date("m-d-y-H");

	$lokasi_file 		= $_FILES['img']['tmp_name'];
	$tipe_file   		= $_FILES['img']['type'];
	$nama_file   		= $_FILES['img']['name'];
	$acak        		= rand(1,99);
	$nama_file_unik	= $acak.$nama_file;

	$bytes = openssl_random_pseudo_bytes(3);

	$pwd = bin2hex($bytes);

    $pass=md5("123456");
	$ids=md5($no_telp);

    if (!empty($lokasi_file)){
	UploadFoto($nama_file_unik);
	mysqli_query($connect,"INSERT INTO users(full_name,
								  email,
								  password,
								  phone_number,
								  id_store,
								  address,
								  master,
								  id_session,
								  level,
								  salary_fixed,
								  commission,
								  allowance,
								  piece,
								  img) 
                    VALUES('$nama_lengkap',
						   '$email',
						   '$pass',
						   '$no_telp',
						   '$id_toko',
						   '$alamat',
						   '$m[phone_number]',
						   '$ids',
						   '$level',
						   '$gaji_tetap',
						   '$komisi',
						   '$tunjangan',
						   '$potongan',
						   '$folder/$nama_file_unik')");
						   
	}else{
	mysqli_query($connect,"INSERT INTO users(full_name,
								  email,
								  password,
								  phone_number,
								  id_store,
								  address,
								  master,
								  id_session,
								  level,
								  salary_fixed,
								  commission,
								  allowance,
								  piece,
								  img) 
                    VALUES('$nama_lengkap',
						   '$email',
						   '$pass',
						   '$no_telp',
						   '$id_toko',
						   '$alamat',
						   '$m[phone_number]',
						   '$ids',
						   '$level',
						   '$gaji_tetap',
						   '$komisi',
						   '$tunjangan',
						   '$potongan',
						   'avatar.png')");
	}
	$json = array(
    'status'	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success, login with password 123456'
	);

    echo json_encode($json);
	
	}

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

    echo json_encode($json);	
	}
	?>