<?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->category_name));
$productid = mysqli_real_escape_string($con, trim($request->product_id));
$subcatname = mysqli_real_escape_string($con, trim($request->sub_category_name));
$productquantity = mysqli_real_escape_string($con, trim($request->product_values));
$productprice = mysqli_real_escape_string($con, trim($request->product_price));
$mode = mysqli_real_escape_string($con, trim($request->mode));

$filesource = mysqli_real_escape_string($con, trim($request->form_value->fileSource));
$outofstock = mysqli_real_escape_string($con, trim($request->form_value->out_of_stock));
$producttype = mysqli_real_escape_string($con, trim($request->form_value->product_type));
$productdesc = mysqli_real_escape_string($con, trim($request->form_value->product_description));
$productdiscount = mysqli_real_escape_string($con, trim($request->form_value->product_discount));
$productname = mysqli_real_escape_string($con, trim($request->form_value->product_name));
$productpopular = mysqli_real_escape_string($con, trim($request->form_value->product_popular));
$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")
    {
        // $filesource == "" || $subcatname == "" ||
        if($catname == "" ||  $producttype == "" || $productprice == "" || $outofstock == "" || $productdiscount == "" || $productname == "" || $productpopular == "" || $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 != '' || $filesource != null)
            {
                $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);
            }
            else
            {
                $savefile = "";
            }
            
            $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($subcatname != "")
            {
                $sc_sql = "SELECT id FROM subcategory WHERE cat_id = '$catid' and name='$subcatname' and shop_id='$shop_id' and shop_code='$shop_code'";
                $sc_result = mysqli_query($con,$sc_sql);
                if($rows = $sc_result->fetch_assoc())
                {
                    $scatid = $rows['id'];
                }
            }
            else
            {
                $scatid = 0;
            }
            
            if($outofstock == 'Yes')
            $pstock = 1;
            else if($outofstock == 'No')
            $pstock = 0;
            
            if($productpopular == 'Yes')
            $ppopular = 1;
            else if($productpopular == 'No')
            $ppopular = 0;
            
            $date = date("Y-m-d H:i:s");
            
            if($catid != null && $scatid != null)
            {
                $sql = "INSERT INTO product(shop_id,shop_code,pname,cid,sid,psdesc,pgms,pprice,status,stock,pimg,ptype,pstatus,discount,popular,date,protime,prostarttime,prostoptime) VALUES 
                ('$shop_id','$shop_code','$productname','$catid','$scatid','$productdesc','$productquantity','$productprice','1','$pstock','$savefile','$producttype','1','$productdiscount','$ppopular','$date','$cattiming','$starttime','$stoptime')";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $rows = array('status'=>true,'message' => 'New Product Added');
                    echo json_encode($rows);
                }
            }
            else
            {
                $rows = array('status'=>false,'message' => 'No Category Available');
                echo json_encode($rows);
                
            }
        }
    }
    
    else if($mode == "edit")
    {
        // $subcatname == "" ||
        if($catname == "" || $productid == "" ||  $producttype == "" || $productprice == "" || $outofstock == "" || $productdiscount == "" || $productname == "" || $productpopular == "" || $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'];
                }
            }
            
            $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($subcatname != "")
            {
                $sc_sql = "SELECT id FROM subcategory WHERE cat_id = '$catid' and name='$subcatname' and shop_id='$shop_id' and shop_code='$shop_code'";
                $sc_result = mysqli_query($con,$sc_sql);
                if($rows = $sc_result->fetch_assoc())
                {
                    $scatid = $rows['id'];
                }
            }
            else
            {
                $scatid = 0;
            }
            
            if($outofstock == 'Yes')
            $pstock = 0;
            else if($outofstock == 'No')
            $pstock = 1;
            
            if($productpopular == 'Yes')
            $ppopular = 1;
            else if($productpopular == 'No')
            $ppopular = 0;
            
            $date = date("Y-m-d H:i:s");
            
            if($filesource == "")
            {
                if($catid != null && $scatid != null or $scatid == 0)
                {
                    $sql = "UPDATE product set pname = '$productname',cid='$catid',sid='$scatid',psdesc='$productdesc',pgms='$productquantity',pprice='$productprice',stock='$pstock',ptype='$producttype',discount='$productdiscount',
                    popular='$ppopular',protime = '$cattiming',prostarttime = '$starttime',prostoptime = '$stoptime' where id='$productid' and shop_id='$shop_id' and shop_code='$shop_code'";
                    $result = mysqli_query($con,$sql);
                    if($result)
                    {
                        $rows = array('status'=>true,'message' => 'Product Updated');
                        echo json_encode($rows);
                    }
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'No Category1 Available');
                    echo json_encode($rows);
                    
                }
            }
            else
            {
                $z_sql = "SELECT pimg FROM product where id='$productid' and shop_id='$shop_id' and shop_code='$shop_code'";
                $z_result = mysqli_query($con,$z_sql);
                if($rows = $z_result->fetch_assoc())
                {
                    $pimg = $rows['pimg'];
                    if($pimg == $filesource)
                    {
                        $savefile = $pimg;
                    }
                    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);
                    }
                    
                    if($catid != null && $scatid != null or $scatid == 0)
                    {
                        $sql = "UPDATE product set pname = '$productname',cid='$catid',sid='$scatid',psdesc='$productdesc',pimg='$savefile',pgms='$productquantity',pprice='$productprice',stock='$pstock',ptype='$producttype',discount='$productdiscount',
                        popular='$ppopular',protime = '$cattiming',prostarttime = '$starttime',prostoptime = '$stoptime' where id='$productid' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $result = mysqli_query($con,$sql);
                        if($result)
                        {
                            $rows = array('status'=>true,'message' => 'Product Updated');
                            echo json_encode($rows);
                        }
                    }
                    else
                    {
                        $rows = array('status'=>false,'message' => 'No Category2 Available');
                        echo json_encode($rows);
                        
                    }
                }
                else
                {
                    $rows = array('status'=>false,'message' => $z_sql);
                    echo json_encode($rows);
                }
                
            }
        }
        
    }
    
    else if($mode == "delete")
    {
        if($productid == "")
        {
            $rows = array('status'=>false,'message' => 'Product Error');
            echo json_encode($rows);
        }
        else
        {
            $sql = "DELETE FROM product where id ='$productid' and shop_id='$shop_id' and shop_code='$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Product 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);
}

?>
