<?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));
$homeid = mysqli_real_escape_string($con, trim($request->home_id));
$catname = mysqli_real_escape_string($con, trim($request->category_name));
$subcatname = mysqli_real_escape_string($con, trim($request->sub_category_name));
// $subcatname = "Milk Sweets";
$title = mysqli_real_escape_string($con, trim($request->form_value->title));
$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));

$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)
{
    $scatid = 0;
    $catid = 0;
    if($mode == "add")
    {
        if($catname == "" || $title == "" || $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'];
                }
            }
            $status = 1;
            $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($row1 = $sc_result->fetch_assoc())
                {
                    $scatid = $row1['id'];
                }
            }
            else
            {
                $scatid = 0;
            }
    
            if($catid != null && $scatid != null or $scatid == 0)
            {
                $sql = "INSERT INTO home_section(shop_id,shop_code,title,cid,sid,cattime,catstarttime,catstoptime,status) VALUES ('$shop_id','$shop_code','$title','$catid','$scatid','$cattiming','$starttime','$stoptime','$status')";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    // $rows = array('status'=>true,'message' => $subcatname);
                    $rows = array('status'=>true,'message' => 'New Home Section Added');
                    echo json_encode($rows);
                }
            }
            else
            {
                $rows = array('status'=>false,'message' => 'No Category Available');
                echo json_encode($rows);
                
            }
        }
        
    }
    
    else if($mode == "edit")
    {
        if($catname == "" || $title == "" || $homeid == "" || $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'];
                }
            }
            $status = 1;
            $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($row = $s_result->fetch_assoc())
            {
                $catid = $row['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($row1 = $sc_result->fetch_assoc())
                {
                    $scatid = $row1['id'];
                }
            }
            else
            {
                $scatid = 0;
            }
    
            if($catid != null && $scatid != null  or $scatid == 0)
            {
                $sql = "UPDATE home_section set title='$title',cid='$catid',sid='$scatid',cattime = '$cattiming',catstarttime = '$starttime',catstoptime = '$stoptime' where id='$homeid' and shop_id='$shop_id' and shop_code='$shop_code'";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $rows = array('status'=>true,'message' => 'Home Section Updated');
                    echo json_encode($rows);
                }
            }
            else
            {
                $rows = array('status'=>false,'message' => 'No Category Available');
                echo json_encode($rows);
                
            }
        }
       
    }
    
    else if($mode == "delete")
    {
        if($homeid == "")
        {
            $rows = array('status'=>false,'message' => 'Fields Missing');
            echo json_encode($rows);
        }
        else
        {
            $sql = "DELETE from home_section where id='$homeid' and shop_id='$shop_id' and shop_code='$shop_code'";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $rows = array('status'=>true,'message' => 'Home Section Deleted');
                echo json_encode($rows);
            }
        }
    }

}
else
{
    $rows = array('status'=>false,'message' => 'Data Error');
    echo json_encode($rows);
}
}

?>
