<?php



session_start();

$getCategoria = filter_input_array(INPUT_POST, FILTER_DEFAULT);

$setCategoria = array_map("strip_tags", $getCategoria);

$categoria = array_map("trim", $setCategoria);



$categoria['categoria_image'] = (isset($_FILES['categoria_image']['tmp_name']) ? $_FILES['categoria_image'] : null );

$categoria['categoria_data'] = date('Y-m-d H:i:s');





$action = $categoria['action'];

$jSon = array();



unset($categoria['action']);





if ($action):

    require ('../../../_app/Config.inc.php');

    require ('../../_models/AdminCategoria.class.php');

    $Upload = new Upload('../../uploads/categorias/');

endif;



switch ($action):

    case 'insert':

        $jSon['result'] = 0;

        if (isset($_FILES['cat_image']['tmp_name'])):

            $Upload->Image($_FILES['cat_image']);

            $categoria['categoria_image'] = $Upload->getResult();

            if ($Upload->getError()):

                $jSon['error'] = $Upload->getError();

                $jSon['result'] = 1;

            endif;

        endif;

        //print_r($categoria);

        if (empty($json['result']) && $jSon['result'] == 1):

            $jSon['error'] = [$jSon['error'], 'error'];

        else:

            $cadastrar = new AdminCategoria;

            $cadastrar->ExeCreate($categoria);

            $jSon['error'] = [$cadastrar->getError()[0], $cadastrar->getError()[1]];

        endif;

        break;



    case 'update':

        $alterar = new AdminCategoria;

        $alterar->ExeUpdate($categoria['categoria_id'], $categoria);

        $jSon['error'] = [$alterar->getError()[0], $alterar->getError()[1]];

        break;



    case 'delet':

        $CategoriaId = ($categoria['categoria_id'] ? $categoria['categoria_id'] : null );

        $deletar = new AdminCategoria;

        $deletar->ExeDelete($CategoriaId);

        $jSon['error'] = [$deletar->getError()[0], $deletar->getError()[1]];

        break;



    default :

        $jSon['error'] = "Erro ao selecionar Ação!";

endswitch;



echo json_encode($jSon);

?>

