<?php
session_start();
include('../include/connection.php');
include('../include/app_config.php');
if(isset($_GET['action']))
{
    switch($_GET['action'])
    {
        case 'resolve':
            $sql = "update complaints set status=:status,resolved_date=:date where id=:id";
            $q=$con->update_query($sql,array(':status'=>RESOLVED_STATUS, ':date'=>date('d-m-Y H:i A'), ':id'=>$_GET['id']));
            echo json_encode(array('action'=>"Resolved"));
            break;
            
        case 'accept':
            $sql = "update complaints set status=:status,accepted_date=:date where id=:id";
            $q=$con->update_query($sql,array(':status'=>ACCEPTED_STATUS, ':date'=>date('d-m-Y H:i A'), ':id'=>$_GET['id']));
            echo "<script>window.location='../view-complaint?id=".$_GET['id']."'</script>";
            break;
            
        case 'deny':
            $sql = "update complaints set status=:status where id=:id";
            $q=$con->update_query($sql,array(':status'=>PENDING_STATUS, ':id'=>$_GET['id']));
            echo "<script>window.location='../view-complaint?id=".$_GET['id']."'</script>";
            break;
            
        case 'reply':
            $success = 0;
            $sql = "insert into complaints_comments (body,complaint_id,sender,date_created)
                    values (:body,:id,:sender,:date)";
            $fields = array(
                ':body'=>$_POST['body'],
                ':id'=>$_POST['id'],
                ':sender'=>$_POST['sender'],
                ':date'=>date('d-m-Y H:i')
            );
            $q = $con->insert_query($sql,$fields);
            if($q)
            {
                $success = 1;
            }
            echo json_encode(array('success'=>$success));
            break;
    }
}
?>