<?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));
$catid = mysqli_real_escape_string($con, trim($request->catid));
$catname = mysqli_real_escape_string($con, trim($request->form_value->catname));
$starttime = mysqli_real_escape_string($con, trim($request->form_value->catstarttime));
$stoptime = mysqli_real_escape_string($con, trim($request->form_value->catstoptime));
$cattiming = mysqli_real_escape_string($con, trim($request->form_value->cattiming));
$filesource = mysqli_real_escape_string($con, trim($request->form_value->fileSource));

if($mode == "add")
{
    if($catname == null ||  $catname == "" || $cattiming == null ||  $cattiming == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        if($cattiming == 'Default')
        {
            $sel = $con->query("select start_time,end_time from setting where id='1'");
            if($row = $sel->fetch_assoc())
            {
                $starttime = $row['start_time'];
                $stoptime = $row['end_time'];
            }
        }
        
        if($filesource == null || $filesource == "")
        {
            $sql = "INSERT INTO category(catname,cattime,catstarttime,catstoptime,catstatus) VALUES ('$catname','$cattiming','$starttime','$stoptime','1')";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Category Added');
                echo json_encode($rows);
            }
        }
        else
        {
            $folderPath = "cat/";
            $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]);
            $file = $folderPath . uniqid() . '.png';
            file_put_contents($file, $image_base64);
            
            $sql = "INSERT INTO category(catname,cattime,catstarttime,catstoptime,catimg,catstatus) VALUES ('$catname','$cattiming','$starttime','$stoptime','$file','1')";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Category Added');
                echo json_encode($rows);
            }
        }
        
    }
}

else if($mode == "edit")
{
    if($catname == "" || $catid == "" || $cattiming == null ||  $cattiming == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        if($cattiming == 'Default')
        {
            $sel = $con->query("select start_time,end_time from setting where id='1'");
            if($row = $sel->fetch_assoc())
            {
                $starttime = $row['start_time'];
                $stoptime = $row['end_time'];
            }
        }
        
        if($filesource == "")
        {
            $sql = "UPDATE category set catname = '$catname',cattime = '$cattiming',catstarttime = '$starttime',catstoptime = '$stoptime' where id = '$catid'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Category Updated');
                echo json_encode($rows);
            }
        }
        else
        {
            $folderPath = "cat/";
            $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]);
            $file = $folderPath . uniqid() . '.png';
            file_put_contents($file, $image_base64);
            
            $sql = "UPDATE category set catname = '$catname',catimg = '$file',cattime = '$cattiming',catstarttime = '$starttime',catstoptime = '$stoptime' where id = '$catid'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Category Updated');
                echo json_encode($rows);
            }
        }
        
    }
}

else if($mode == "delete")
{
    if($catid == "")
    {
        $rows = array('status'=>false,'message' => 'Category Error');
        echo json_encode($rows);
    }
    else
    {
        $sql = "DELETE FROM category where id ='$catid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Category Deleted');
            echo json_encode($rows);
        }
    }
}

}

?>
