<?php

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;
use Mtc\Shop\Assessment\Question;
use Mtc\Shop\Item;

$path = '../../../';
require_once $path . 'core/includes/header.inc.php';


if ($_POST['action'] === 'searchItems') {

    echo json_encode([
        'success' => '1',
        'items' => Item::searchItems($_POST['query'])
            ->toArray(),
    ]);
    exit;
}

if ($_POST['action'] === 'save') {
    /** @var Question $question */
    $question = Question::query()
        ->find($_POST['questionId']);
    if (!empty($question)) {
        if ($_POST['type'] === 'recommend') {
            $question->recommended = $_POST['recommended'] ?? null;
        } else {
            $question->excluded = $_POST['recommended'] ?? null;
        }
        $question->save();
    }
    echo json_encode([
        'success' => '1',
    ]);
    exit;
}

?>