
<?php
    //setting header to json
    header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *");


    $driverID = $_POST["d"];  // driverID
    $assigned = $_POST["a"];  // assigned
    $pinTypeID = $_POST["t"];  // pinTypeID
    $name = $_POST["n"];  // name
    $iconID = $_POST["i"];  // iconID
    $gpio = $_POST["g"];  // iconID

    // if ($assigned == 1) $assigned = "1";
    // else $assigned = "0";
    
    // create a portName for each pin
    $portName = "GPIO-";
    $portName .= $gpio;

    require '../php/database.php';

    if ($pinTypeID == 100) {$result = $mysqli->query("INSERT INTO `pinsAvailable` (`driverID`,`gpio`,`assigned`,`pinTypeID`,`name`,`portName`,`iconID`) VALUES ('".$driverID."', '".$gpio."', '".$assigned."', '11', 'Add Custom Name', '".$portName."', '".$iconID."')");}  // if nothing was assigned to pin
    else {$result = $mysqli->query("INSERT INTO `pinsAvailable` (`driverID`,`gpio`,`assigned`,`pinTypeID`,`name`,`iconID`) VALUES ('".$driverID."', '".$gpio."', '".$assigned."', '".$pinTypeID."', '".$name."', '".$iconID."')");}
    if ($mysqli->query($result) === TRUE) {
        echo "Record updated successfully";
    } 

    // //free memory associated with result
    $result->close();
    
    //close connection
    $mysqli->close();

    //print the data
    // print json_encode($dataWindow); 
?>