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

// db name = home

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 setting where id='1'");
            if($row = $sel->fetch_assoc())
            {
                $starttime = $row['start_time'];
                $stoptime = $row['end_time'];
            }
        }
        $status = 1;
        $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($catid != null && $scatid != null or $scatid == 0)
        {
            $sql = "INSERT INTO home(title,cid,sid,cattime,catstarttime,catstoptime,status) VALUES ('$title','$catid','$scatid','$cattiming','$starttime','$stoptime','$status')";
            $result = mysqli_query($con,$sql);
            if($result)
            {
                $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 setting where id='1'");
            if($row = $sel->fetch_assoc())
            {
                $starttime = $row['start_time'];
                $stoptime = $row['end_time'];
            }
        }
        $status = 1;
        $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($catid != null && $scatid != null  or $scatid == 0)
        {
            $sql = "UPDATE home set title='$title',cid='$catid',sid='$scatid',cattime = '$cattiming',catstarttime = '$starttime',catstoptime = '$stoptime' where id='$homeid'";
            $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 where id='$homeid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Home Section Deleted');
            echo json_encode($rows);
        }
    }
}

}

?>
