<?php
    session_start();
	include_once("../config.php");
	include_once("../functions.php");
    if (isset($_SESSION['user_id'])) {
        $data = post_input_serilization();
        if (isset($data['token']) && $data['token'] == 'getRate') {
            if(isset($data["cid"])){
                $result = $link->query("SELECT exchange_rate AS rate FROM currencies where cid = {$data['cid']}");
                $result = mysqli_fetch_assoc($result);
                echo json_encode($result);
                exit;
            }
            
        }else if (isset($data['token']) && $data['token'] == 'setRate') {
            if(isset($data["cid"]) && isset($data["rate"])){
                $result = $link->query("UPDATE currencies SET exchange_rate = {$data["rate"]} where cid = {$data["cid"]}");
                if($result)
                    echo json_encode(["result" => "success"]);
                else
                    echo json_encode(["result" => "failed"]);
                exit;
            }
            
        }else if (isset($data['token']) && $data['token'] == 'getCurrencyOptions') {
            $result_currencies = mysqli_query($link,"SELECT * FROM currencies order by cid ASC");
            while ($row_currencies = mysqli_fetch_array($result_currencies)) {
                echo "<option value='{$row_currencies['cid']}-{$row_currencies['exchange_rate'] }'>{$row_currencies['symbol']}</option>";
            }
        } 
    }