<?php
session_start();

require_once '../Config/Database.php';
require_once '../Model/achievement.php';
$achievement = new achievement();

$achievements = $achievement->read_all_achievements();

if(isset($_GET['all_achievements'])){
    echo json_encode(
        array("data"=>$achievements)
    ); 
}

if(isset($_POST['add_achievement'])){
    $achievement_level = $_POST['achievement_level'];
    $achievement_point = $_POST['achievement_point'];
    $achievement_reward = $_POST['achievement_reward'];
    $data = array(
        "achievement_level"=>$achievement_level,
        "achievement_point"=>$achievement_point,
        "achievement_reward"=>$achievement_reward
    );
    $success = $achievement->add_achievement($data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}

if(isset($_POST['update_achievement'])){
    $achievement_id = $_POST['achievement_id'];
    $achievement_level = $_POST['achievement_level'];
    $achievement_point = $_POST['achievement_point'];
    $achievement_reward = $_POST['achievement_reward'];
    $data = array(
        "achievement_level"=>$achievement_level,
        "achievement_point"=>$achievement_point,
        "achievement_reward"=>$achievement_reward
    );
    $key = array(
        "achievement_id"=>$achievement_id
    );
    $success = $achievement->update_achievement($key,$data);
    if($success){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}


if(isset($_POST['remove_achievement'])){
    $achievement_id = $_POST['achievement_id'];
    $data = $achievement->remove_achievement($achievement_id);
    if(!$data){
        echo json_encode(
            array('data'=>'success')
        );
    
    }else{
        echo json_encode(
            array('data'=>'fail')
        );
    
    }
}