<!doctype html>
<html lang="es">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">

    <title>ADD ONLINE!</title>
  </head>
  <body>

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$dbhost = 'localhost';
$dbname = 'plusmusi_data';
$dbusername = 'plusmusi_owner';
$dbpassword = '^@#F-NBx3W}+';


$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
//  $data = $pdo->query("SELECT * FROM pueba")->fetchAll();
//  $statement = $link->prepare('INSERT INTO pueba ( nombre ) VALUES (:name ) ');
//  $statement->execute(['name' => $nombre ]);  


function get_json($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch);
    curl_close($ch);
    return json_decode($result, true);
}

$genero = &$_GET['g'];
$artista = &$_GET['a'];

$url = 'https://temporal2023.online/fm/gord.php';

if( !empty($artista) ){
    $ruta = '?g='.$genero.'&a='.$artista;
}else if ( !empty($genero)){
    $ruta = '?g='.$genero;
}else{
    $ruta = '';
}

$ruta = str_replace(" ","+",$ruta);
echo '<div class="jumbotron">';
foreach (get_json($url) as $file) {
    if( $file != 'gord.php' ){
        echo '<a href="?g='.$file.'">'.$file.'</a> - ';
    }
}
echo '</div>';

echo '<div><ul class="list-group list-group-flush">';
foreach (get_json($url.$ruta) as $file) {


        $ex = explode("/", $file);
        if(count($ex) == 2){
            $artista = $ex[1];
            $genero = $ex[0];
            $tm = $pdo->prepare("SELECT id, genero, artista FROM artistas WHERE artista = :artista and genero = :genero ");
            $tm->execute(['artista' => $artista, 'genero' => $genero ]);
            $dat = $tm->fetch();
            if( empty($dat['id'])){
                $statement = $pdo->prepare('INSERT INTO artistas ( genero, artista ) VALUES (:genero, :artista ) ');
                $statement->execute(['genero' => $genero, 'artista' => $artista ]);
                echo '<li class="list-group-item list-group-item-success">'.$file.'</li>';
            }else{
                echo '<li class="list-group-item"><a href="?g='.$genero.'&a='.$artista.'">'.$file.'</a></li>';
            }
        }

        if(count($ex) == 3){
            $genero = $ex[0];
            $artista = $ex[1];
            $cancion = str_replace(array(".mp3",'.skm'),"",explode(" - ",$ex[2])[1]);
            $mp3 = 'https://temporal2023.online/fm/'.$file;
            $tm = $pdo->prepare("SELECT id, genero, artista, cancion, url FROM canciones WHERE genero = :genero AND artista LIKE :artista AND cancion LIKE :cancion ");
            $tm->execute(['genero' => $genero, 'artista' => $artista, 'cancion' => $cancion ]);
            $dat = $tm->fetch();
            if( empty($dat['id'])){
                $stmt = $pdo->prepare('INSERT INTO canciones ( genero, artista, cancion, url ) VALUES (:genero, :artista, :cancion, :url ) ');
                $stmt->bindValue(':genero', $genero);
                $stmt->bindValue(':artista', $artista, PDO::PARAM_STR );
                $stmt->bindValue(':cancion', $cancion);
                $stmt->bindValue(':url', $mp3);
                $stmt->execute();
                echo '<li class="list-group-item list-group-item-success">'.$mp3.'</li>';
            }else{
                echo '<li class="list-group-item">'.$mp3.'</li>';
            }
        }

}
echo '</ul></div>';

?>

  </body>
</html>