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

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 setting where id='1'");
            if($row = $sel->fetch_assoc())
            {
                $starttime = $row['start_time'];
                $stoptime = $row['end_time'];
            }
        }
        
        if($filesource != '' || $filesource != null)
        {
            $folderPath = "product/";
            $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);
        }
        else
        {
            $file = "";
        }
        
        $s_sql = "SELECT id FROM category WHERE catname='$catname'";
        $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'";
            $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(pname,cid,sid,psdesc,pgms,pprice,status,stock,pimg,ptype,pstatus,discount,popular,date,protime,prostarttime,prostoptime) VALUES 
            ('$productname','$catid','$scatid','$productdesc','$productquantity','$productprice','1','$pstock','$file','$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 setting where id='1'");
            if($row = $sel->fetch_assoc())
            {
                $starttime = $row['start_time'];
                $stoptime = $row['end_time'];
            }
        }
        
        $s_sql = "SELECT id FROM category WHERE catname='$catname'";
        $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'";
            $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',pimg='$filesource',pgms='$productquantity',pprice='$productprice',stock='$pstock',ptype='$producttype',discount='$productdiscount',
                popular='$ppopular',protime = '$cattiming',prostarttime = '$starttime',prostoptime = '$stoptime' where id='$productid'";
                $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'";
            $z_result = mysqli_query($con,$z_sql);
            if($rows = $z_result->fetch_assoc())
            {
                $pimg = "https://api.iconxtechnologies.in/Arabian_hut/".$rows['pimg'];
                if($pimg == $filesource)
                {
                    $file = $rows['pimg'];
                }
                else
                {
                    $folderPath = "product/";
                    $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);
                }
                
                if($catid != null && $scatid != null or $scatid == 0)
                {
                    $sql = "UPDATE product set pname = '$productname',cid='$catid',sid='$scatid',psdesc='$productdesc',pimg='$file',pgms='$productquantity',pprice='$productprice',stock='$pstock',ptype='$producttype',discount='$productdiscount',
                    popular='$ppopular',protime = '$cattiming',prostarttime = '$starttime',prostoptime = '$stoptime' where id='$productid'";
                    $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'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Product Deleted');
            echo json_encode($rows);
        }
    }
}
}
else
{
    $rows = array('status'=>false,'message' => 'Something Error');
    echo json_encode($rows);
}

?>
