<?php
     include '../script.php';

    if($_GET["action"] == "List"){
        $filename = '/tmp/dashboard/relay';        
        $arr = [];
        $str = file_get_contents($filename);
        $json = json_decode($str, true);
        array_push($arr, $json);
        
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Records'] = $arr;
        print json_encode($jTableResult);
    
    
    }else if($_GET["action"] == "Create"){
        if ($_SERVER["REQUEST_METHOD"] == "POST") {

            $smtp = test_input($_POST["smtp"]);
            $UserName = test_input($_POST["UserName"]);
            $pass = test_input($_POST["pass"]);
            $recordDate = date("Y-m-d H:i:s");

            //$ID = CountRows();

            $arr = array("smtp" => $smtp, "UserName" => $UserName, "pass" => $pass,"RecordDate" => $recordDate);


            write_temp($arr, "ap", "relay");
            write_UserToPasswd();
            write_RelayToPasswd($arr);

            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            $jTableResult['Record'] = $arr;
            print json_encode($jTableResult);

       
        }
        
    }else if($_GET["action"] == "Delete"){
        $tmpRelay = '/tmp/dashboard/relay';   
        file_put_contents($tmpRelay, "");
        
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        print json_encode($jTableResult);
        
    }