<?php
switch($formData->t){
	case 'create':
		$gambar = str_replace('\\','',str_replace(' ','+',$formData->gambar_toko));
		$path = 'assets/images/toko/';
		$imgName = saveImg($gambar,$path,50);
		$url = $userData->ID.'-'.create_slug($formData->nama);
		$q=mysqli_query($connect, "INSERT INTO `toko` (`ID_user`, `nama_toko`, `alamat_toko`, `gambar_toko`, `url_toko`, `date_create`)
									VALUES ('".$userData->ID."', '".$formData->nama."', '".$formData->alamat."', '$imgName', '$url', '$now');");
		if(!$q){
			$data = array('status'=>0,'msg'=>mysqli_error($connect));	
		}else{
			goto getData;
		}
	break;
	case 'edit':
		
		if(!$q){
			$data = array('status'=>0,'msg'=>mysqli_error($connect));	
		}else{
			goto getData;
		}
	break;
	case 'delete':
		$toko=mysqli_fetch_array(mysqli_query($connect, "SELECT `gambar_toko` FROM `toko` WHERE `ID`='".$formData->id."' AND `ID_user`='".$userData->ID."'"));
		$gambar_toko = 'images/toko/'.$toko[0];
		if(is_file($gambar_toko)){
			unlink($gambar_toko);
		}
		$q=mysqli_query($connect, "DELETE FROM `toko` WHERE `ID`='".$formData->id."' AND `ID_user`='".$userData->ID."'");
		if(!$q){
			$data = array('status'=>0,'msg'=>mysqli_error($connect));	
		}else{
			goto getData;
		}
	break;
	default:
		getData:
		if($formData->l==1){
			$q=mysqli_query($connect, "select * from toko where ID_user='".$userData->ID."'");
		}else{
			$q=mysqli_query($connect, "select b.* from karyawan_toko a,toko b where a.ID_user='".$userData->ID_user."' and a.ID_karyawan='".$userData->ID."' and b.ID=a.ID_toko");
		}
		$toko=[];
		while($b=mysqli_fetch_assoc($q)){
			$toko[]=$b;
		}
		if(!$q){
			$data = array('status'=>0,'msg'=>mysqli_error($connect));	
		}else{
			$data = array('status'=>1,'toko'=>$toko,'msg'=>'success');
		}
	break;
}