<?php

	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		import_file(DIR_MODELS."Model.class.php");

		$id_categoria_loja = (int) filter_input(INPUT_POST, "id_categoria_loja", FILTER_SANITIZE_NUMBER_INT);
		$id_subcategoria_loja = (int) filter_input(INPUT_POST, "id_subcategoria_loja", FILTER_SANITIZE_NUMBER_INT);

		$model = new Model();

		if (!empty($id_categoria_loja)) {
			$result = $model->query("
				SELECT * FROM subcategoria_loja 
				WHERE 
					id_categoria_loja = :id AND 
					status_subcategoria_loja = 'S'
				ORDER BY 
					nome_subcategoria_loja ASC
			", [
				":id" => $id_categoria_loja
			]);
		}
		else if (!empty($id_subcategoria_loja)) {
			$result = $model->query("
				SELECT * FROM subcategoria_loja 
				WHERE 
					id_subcategoria_loja_pai = :id AND 
					status_subcategoria_loja = 'S'
				ORDER BY 
					nome_subcategoria_loja ASC
			", [
				":id" => $id_subcategoria_loja
			]);
		}
		else{
			echo json_encode(["status" => false]);
		}

		if ($result->rowCount() > 0) {
			$response = ["status" => true];

			foreach ($result->fetchAll() as $key => $value) {
				foreach ($value as $k => $v) {
					$response["dados"][$key][$k] = $v;
				}
			}

			echo json_encode($response);
		}
		else{
			echo json_encode(["status" => false]);
		}
	}
	else{
		show_403();
	}


?>