	<?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']);
		$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']);
		$id=mysqli_real_escape_string($connect, $_POST['id']);

	$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);


	if ($jumlah > 0){
	
	$userstaff = mysqli_query($connect,"SELECT * FROM users WHERE user='$no_telp'");
    $j=mysqli_fetch_array($userstaff);
	
	include "../fungsi_thumb.php";
  
	$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; 
	
	$folder = date("m-d-y-H");
  
  
	if (empty($lokasi_file)){
	mysqli_query($connect,"UPDATE users SET full_name   	   = '$nama_lengkap',
								  address			   = '$alamat',
								  email				   = '$email',
								  level				   = '$level',
								  salary_fixed		   = '$gaji_tetap',
								  commission			   = '$komisi',
								  allowance			   = '$tunjangan',
								  piece			   = '$potongan',
								  phone_number			   = '$no_telp',
								  id_store			   = '$id_toko'
                             WHERE id_session 	       = '$id'");
	}else{
	    UploadFoto($nama_file_unik);
	mysqli_query($connect,"UPDATE users SET full_name   	   = '$nama_lengkap',
								  address			   = '$alamat',
								  email				   = '$email',
								  level				   = '$level',
								  salary_fixed		   = '$gaji_tetap',
								  commission			   = '$komisi',
								  allowance			   = '$tunjangan',
								  piece			   = '$potongan',
								  phone_number			   = '$no_telp',
								  img			   = '$folder/$nama_file_unik',
								  id_store			   = '$id_toko'
                             WHERE id_session 	       = '$id'");
	}

	$json = array(
    'status' 		=> 'true',
    'errCode'	 	=> '01',
	'msg' 			=> 'Success'
	);

    echo json_encode($json);

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

    echo json_encode($json);	
	}
	?>