<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
$mode = mysqli_real_escape_string($con, trim($request->mode));
$bannerid = mysqli_real_escape_string($con, trim($request->bannerid));
$catname = mysqli_real_escape_string($con, trim($request->category_name));
$filesource = mysqli_real_escape_string($con, trim($request->form_value->fileSource));

$type = mysqli_real_escape_string($con, trim($request->type));
$shop_id = mysqli_real_escape_string($con, trim($request->shop_id));
$shop_code = mysqli_real_escape_string($con, trim($request->shop_code));

// db name = banner
include 'token_check.php';

if($token_check)
{
    if($mode == "add")
    {
        if($filesource == null ||  $catname == "")
        {
            $rows = array('status'=>false,'message' => 'Fields Missing');
            echo json_encode($rows);
        }
        else
        {
            $s_sql = "SELECT id FROM category WHERE catname='$catname' and shop_id='$shop_id' and shop_code='$shop_code'";
            $s_result = mysqli_query($con,$s_sql);
            if($row = $s_result->fetch_assoc())
            {
                $catid = $row['id'];
            }
            
            $url = $file_upload_url."/".$city_path."/".$cat_path."/".$shop_id;
            $type_path = strtolower($type);
            if (!file_exists($url . $type_path)) {
                mkdir($url . "/" . $type_path, 0777, true);
                $upload_url = $url. "/" .$type_path;
            }
            else
            {
                $upload_url = $url. "/" .$type_path;
            }

            $image_parts = explode(";base64,", $filesource);
            $image_type_aux = explode("image/", $image_parts[0]);
            $image_type = $image_type_aux[1];
            $image_base64 = base64_decode($image_parts[1]);
            
            $image_file_name = uniqid(). '.png';
            $uploadfile = $upload_url  . "/" . $image_file_name;
            
            $accesspath = $file_upload_extension_url. "/" .$city_path. "/" .$cat_path. "/" .$shop_id. "/";
            $savefile = $accesspath . $type_path. "/" . $image_file_name;

            file_put_contents($uploadfile, $image_base64);
            
            $sql = "INSERT INTO banner(shop_id,shop_code,bimg,cid) VALUES ('$shop_id','$shop_code','$savefile','$catid')";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Banner Added');
                echo json_encode($rows);
            }
        }
    }
    
    else if($mode == "edit")
    {
        if($bannerid == null || $catname == "")
        {
            $rows = array('status'=>false,'message' => 'Fields Missing');
            echo json_encode($rows);
        }
        else
        {
            $s_sql = "SELECT id FROM category WHERE catname='$catname' and shop_id='$shop_id' and shop_code='$shop_code'";
            $s_result = mysqli_query($con,$s_sql);
            if($row = $s_result->fetch_assoc())
            {
                $catid = $row['id'];
            }
            if($filesource == "")
            {
                $sql = "UPDATE banner set cid = '$catid' where id = '$bannerid' and shop_id='$shop_id' and shop_code='$shop_code'";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $rows = array('status'=>true,'message' => 'Banner Updated');
                    echo json_encode($rows);
                }
            }
            else
            {
                $type_path = strtolower($type);
                $upload_url = $file_upload_url."/".$city_path."/".$cat_path."/".$shop_id. "/" .$type_path;

                $image_parts = explode(";base64,", $filesource);
                $image_type_aux = explode("image/", $image_parts[0]);
                $image_type = $image_type_aux[1];
                $image_base64 = base64_decode($image_parts[1]);
                
                $image_file_name = uniqid(). '.png';
                $uploadfile = $upload_url  . "/" . $image_file_name;
                
                $accesspath = $file_upload_extension_url. "/" .$city_path. "/" .$cat_path. "/" .$shop_id. "/";
                $savefile = $accesspath . $type_path. "/" . $image_file_name;

                file_put_contents($uploadfile, $image_base64);
                    
                $sql = "UPDATE banner set bimg = '$savefile',cid = '$catid' where id = '$bannerid' and shop_id='$shop_id' and shop_code='$shop_code'";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $rows = array('status'=>true,'message' => 'Banner Updated');
                    echo json_encode($rows);
                }
                
            }
            
        }
    }
    
    else if($mode == "delete")
    {
        if($bannerid == "")
        {
            $rows = array('status'=>false,'message' => 'Banner Error');
            echo json_encode($rows);
        }
        else
        {
            $sql = "DELETE FROM banner where id ='$bannerid' and shop_id='$shop_id' and shop_code='$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Banner Deleted');
                echo json_encode($rows);
            }
        }
    }
}
else
{
    $rows = array('status'=>false,'message' => 'Data Error');
    echo json_encode($rows);
}
}

?>
