<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../config/database.php';
include_once '../objects/city.php';

date_default_timezone_set('Asia/Rangoon'); 
session_start();

$database = new Database();
$db = $database->getConnection();
 
$city = new City($db);

$data = json_decode($_POST['objArr']);

$newname = "";
if (strpos($_POST['showResult'], 'http://wsm.net.mm/') !== false) {
    $newname = "";
}else{
    define('UPLOAD_DIR', './upload/');
    $img = $_POST['showResult'];
    $img_data = base64_decode(explode(',', $img)[1]);
    $file = UPLOAD_DIR . date("Y.m.d.H.i.s") . '.png';//uniqid()
    $success = file_put_contents($file, $img_data);
    $newname = date("Y.m.d.H.i.s") . '.png';
}


$city->profile_picture = $newname;

$city->name = $data[0]->name;
$city->code = $data[0]->code;
$city->invoice_remark = $data[0]->invoice_remark;	
$city->location = $data[0]->location;
// $city->entry_by = $_SESSION['user'];
// $city->entry_date_time = date("Y-m-d H:i:s");

if($data[0]->id==""){

		$arr = array(
			"message" => $city->name
		);
	if($city->exist()){
	    $arr = array(
			"message" => "duplicate"
		);
	}else if($city->create()){
		$arr = array(
			"message" => "created"
		);
	}else{
	    $arr = array(
			"message" => "errorCreate"
		);
	}
}else{
	$city->id = $data[0]->id;
	if($city->update()){
		$arr = array(
			"message" => "updated"
		);
	}else{
	    $arr = array(
			"message" => "errorUpdate"
		);
	}
}
echo json_encode($arr);
?>