<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
$catname = mysqli_real_escape_string($con, trim($request->catname));
$mode = mysqli_real_escape_string($con, trim($request->mode));
$subcattid = mysqli_real_escape_string($con, trim($request->subcatid));
$filesource = mysqli_real_escape_string($con, trim($request->form_value->fileSource));
$subcatname = mysqli_real_escape_string($con, trim($request->form_value->subcatname));
$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));

$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));

include 'token_check.php';

if($token_check)
{
    if($mode == "add")
    {
        if($catname == "" || $subcatname == "" || $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 shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
                if($row = $sel->fetch_assoc())
                {
                    $starttime = $row['start_time'];
                    $stoptime = $row['end_time'];
                }
            }
            if($filesource == null || $filesource == "")
            {
                $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($rows = $s_result->fetch_assoc())
                {
                    $catid = $rows['id'];
                }
                
                if($catid != null)
                {
                    $sql = "INSERT INTO subcategory(shop_id,shop_code,cat_id,name,cattime,catstarttime,catstoptime,catstatus) VALUES ('$shop_id','$shop_code','$catid','$subcatname','$cattiming','$starttime','$stoptime','1')";
                    $result = mysqli_query($con,$sql);
                    if($result)
                    {
                        $rows = array('status'=>true,'message' => 'Sub Category Added');
                        echo json_encode($rows);
                    }
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'No Category Available');
                    echo json_encode($rows);
                    
                }
            }
            else
            {
                $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);
                
                $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($rows = $s_result->fetch_assoc())
                {
                    $catid = $rows['id'];
                }
                
                if($catid != null)
                {
                    $sql = "INSERT INTO subcategory(shop_id,shop_code,cat_id,name,cattime,catstarttime,catstoptime,img,catstatus) VALUES ('$shop_id','$shop_code','$catid','$subcatname','$cattiming','$starttime','$stoptime','$savefile','1')";
                    $result = mysqli_query($con,$sql);
                    if($result)
                    {
                        $rows = array('status'=>true,'message' => 'Sub Category Added');
                        echo json_encode($rows);
                    }
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'No Category Available');
                    echo json_encode($rows);
                    
                }
            }
            
        }
    }
    else if($mode == "edit")
    {
        if($catname == "" || $subcattid == "" || $subcatname == "" || $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 shop_settings where shop_id='$shop_id' and shop_code='$shop_code'");
                if($row = $sel->fetch_assoc())
                {
                    $starttime = $row['start_time'];
                    $stoptime = $row['end_time'];
                }
            }
            
            if($filesource == "")
            {
                $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($rows = $s_result->fetch_assoc())
                {
                    $catid = $rows['id'];
                }
                
                if($catid != null && $subcattid != null)
                {
                    $sql = "UPDATE subcategory set cat_id='$catid',name='$subcatname',cattime = '$cattiming',catstarttime = '$starttime',catstoptime = '$stoptime' where id='$subcattid' and shop_id='$shop_id' and shop_code='$shop_code'";
                    $result = mysqli_query($con,$sql);
                    if($result)
                    {
                        $rows = array('status'=>true,'message' => 'Sub Category Updated');
                        echo json_encode($rows);
                    }
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'No Category Available');
                    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);
                
                $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($rows = $s_result->fetch_assoc())
                {
                    $catid = $rows['id'];
                }
                
                if($catid != null && $subcattid != null)
                {
                   $sql = "UPDATE subcategory set cat_id='$catid',name='$subcatname',img='$file',cattime = '$cattiming',catstarttime = '$starttime',catstoptime = '$stoptime' where id='$subcattid' and shop_id='$shop_id' and shop_code='$shop_code'";
                    $result = mysqli_query($con,$sql);
                    if($result)
                    {
                        $rows = array('status'=>true,'message' => 'Sub Category Updated');
                        echo json_encode($rows);
                    }
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'No Category Available');
                    echo json_encode($rows);
                    
                }
            }
        }
    }
    else if($mode == "delete")
    {
        if($subcattid == "")
        {
            $rows = array('status'=>false,'message' => 'Sub Category Error');
            echo json_encode($rows);
        }
        else
        {
            $sql = "DELETE FROM subcategory where id ='$subcattid' and shop_id='$shop_id' and shop_code='$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Sub Category Deleted');
                echo json_encode($rows);
            }
        }
    }
}
else
{
    $rows = array('status'=>false,'message' => 'Data Error');
    echo json_encode($rows);
}
}
else
{
    $rows = array('status'=>false,'message' => 'Something Error');
    echo json_encode($rows);
}

?>
