<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    if(isset($_GET['sid']) && isset($_GET['token']))
    {
        $shop_id = $_GET['sid'];
        $shop_code = $_GET['token'];
    
        include 'token_check.php';
        
        if($token_check)
        {
            $val = mysqli_real_escape_string($con, trim($request->value));
            if($val != null)
            {
                $s_sql = "SELECT * FROM category where id = '$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                $s_query = mysqli_query($con,$s_sql);
                if($rows = $s_query->fetch_assoc())
                {
                    $status = $rows['catstatus'];
                }
                if($status != null)
                {
                    if($status==0)
                    {
                        $c_sql = "UPDATE category set catstatus = '1' where id='$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $c_result = mysqli_query($con,$c_sql);
                        $sc_sql = "UPDATE subcategory set catstatus = '1' where cat_id='$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $sc_result = mysqli_query($con,$sc_sql);
                        $p_sql = "UPDATE product set pstatus = '1' where cid='$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $p_result = mysqli_query($con,$p_sql);
                        if($c_result && $sc_result && $p_result)
                        {
                            $rows = array('status'=>true,'message' => 'Category Publish On');
                            echo json_encode($rows);
                        }
                    }
                    else if($status==1)
                    {
                        $c_sql = "UPDATE category set catstatus = '0' where id='$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $c_result = mysqli_query($con,$c_sql);
                        $sc_sql = "UPDATE subcategory set catstatus = '0' where cat_id='$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $sc_result = mysqli_query($con,$sc_sql);
                        $p_sql = "UPDATE product set pstatus = '0' where cid='$val' and shop_id='$shop_id' and shop_code='$shop_code'";
                        $p_result = mysqli_query($con,$p_sql);
                        if($c_result && $sc_result && $p_result)
                        {
                            $rows = array('status'=>true,'message' => 'Category Publish Off');
                            echo json_encode($rows);
                        }
                    }
                }
               
            }
            else
            {
                $rows = array('status'=>false,'message' => 'Something Error');
                echo json_encode($rows);  
            }
        }
        else
        {
            $rows = array('status'=>"",'message' => 'Data Error');
            echo json_encode($rows);
        }
    }
}

?>