<?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));
$bannerimg = mysqli_real_escape_string($con, trim($request->form_value->fileSource));

// db name = banner

if($mode == "add")
{
    if($bannerimg == null ||  $catname == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        $s_sql = "SELECT id FROM category WHERE catname='$catname'";
        $s_result = mysqli_query($con,$s_sql);
        if($row = $s_result->fetch_assoc())
        {
            $catid = $row['id'];
        }
        
        $folderPath = "banner/";
        $image_parts = explode(";base64,", $bannerimg);
        $image_type_aux = explode("image/", $image_parts[0]);
        $image_type = $image_type_aux[1];
        $image_base64 = base64_decode($image_parts[1]);
        $file = $folderPath . uniqid() . '.png';
        file_put_contents($file, $image_base64);
        
        $sql = "INSERT INTO banner(bimg,cid) VALUES ('$file','$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'";
        $s_result = mysqli_query($con,$s_sql);
        if($row = $s_result->fetch_assoc())
        {
            $catid = $row['id'];
        }
        if($bannerimg == "")
        {
            $sql = "UPDATE banner set cid = '$catid' where id = '$bannerid'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Banner Updated');
                echo json_encode($rows);
            }
        }
        else
        {
            $folderPath = "banner/";
            $image_parts = explode(";base64,", $bannerimg);
            $image_type_aux = explode("image/", $image_parts[0]);
            $image_type = $image_type_aux[1];
            $image_base64 = base64_decode($image_parts[1]);
            $file1 = $folderPath . uniqid() . '.png';
            file_put_contents($file1, $image_base64);
                
            $sql = "UPDATE banner set bimg = '$file1',cid = '$catid' where id = '$bannerid'";
            $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'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Banner Deleted');
            echo json_encode($rows);
        }
    }
}

}

?>
