<?php

require_once("../connection.php");

$key=mysqli_real_escape_string($connect, $_POST['key']);
$nama_toko=mysqli_real_escape_string($connect, $_POST['name_store']);
$alamat_toko=mysqli_real_escape_string($connect, $_POST['address_store']);
$warna_toko=mysqli_real_escape_string($connect, $_POST['color_store']);
$jam_operasional=mysqli_real_escape_string($connect, $_POST['operational_hour']);
$nowa=mysqli_real_escape_string($connect, $_POST['nowa']);
$id=mysqli_real_escape_string($connect, $_POST['id']);
$linkfb=mysqli_real_escape_string($connect, $_POST['linkfb']);
$linkinstagram=mysqli_real_escape_string($connect, $_POST['linkinstagram']);
$subdomain=mysqli_real_escape_string($connect, $_POST['subdomain']);

$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){
    
    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;
  
  $folder = date("m-d-y-H");
	
	$datadomain = mysqli_query($connect,"SELECT * FROM online_store WHERE subdomain='$subdomain'");
	$d=mysqli_fetch_array($datadomain);
	$jumlahsub   = mysqli_num_rows($datadomain);

	if (empty($lokasi_file)){
	if ($jumlahsub > 0){
		
		mysqli_query($connect,"UPDATE online_store SET name_store   	   = '$nama_toko',
								 address_store			   = '$alamat_toko',
								 color_store				   = '$warna_toko',
								 operational_hour		   = '$jam_operasional',
								 linkfb		   	   		   = '$linkfb',
								 linkinstagram		   	   = '$linkinstagram',
								 nowa			       	   = '$nowa'
                             WHERE id_online_store	               = '$id'"); 
		
		$json = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
	
    echo json_encode($json);
		
	}else{
		
		mysqli_query($connect,"UPDATE online_store SET name_store   	   = '$nama_toko',
								 address_store			   = '$alamat_toko',
								 color_store				   = '$warna_toko',
								 operational_hour		   = '$jam_operasional',
								 linkfb		   	   		   = '$linkfb',
								 linkinstagram		   	   = '$linkinstagram',
								 subdomain		   	   	   = '$subdomain',
								 nowa			       	   = '$nowa'
                             WHERE id_online_store	               = '$id'");

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

    echo json_encode($json);
			  
	
	}
}else{
UploadBanner($nama_file_unik);
	if ($jumlahsub > 0){
		
		mysqli_query($connect,"UPDATE online_store SET name_store   	   = '$nama_toko',
								 address_store			   = '$alamat_toko',
								 color_store				   = '$warna_toko',
								 operational_hour		   = '$jam_operasional',
								 linkfb		   	   		   = '$linkfb',
								 linkinstagram		   	   = '$linkinstagram',
								 img		   	   	   	= '$folder/$nama_file_unik',
								 nowa			       	   = '$nowa'
                             WHERE id_online_store	               = '$id'");
		
		$json = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
	
    echo json_encode($json);
	
		
	}else{
		
		mysqli_query($connect,"UPDATE online_store SET name_store   	   = '$nama_toko',
								 address_store			   = '$alamat_toko',
								 color_store				   = '$warna_toko',
								 operational_hour		   = '$jam_operasional',
								 linkfb		   	   		   = '$linkfb',
								 linkinstagram		   	   = '$linkinstagram',
								 subdomain		   	   	   = '$subdomain',
								 img		   	   	   = '$folder/$nama_file_unik',
								 nowa			       	   = '$nowa'
                             WHERE id_online_store	               = '$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);	
}
?>
