<?php
    global $wpdb, $dataApi;
    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX;
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_licenza'])) {
        $licenza = sanitize_text_field($_POST['licenza']);    
        $wpdb->update(
            $table_name,
            ['op_val' => $licenza],
            ['operazione' => 'licenza']
        );        
    }    
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_panel'])) {
        $panel = sanitize_text_field($_POST['panel']);    
        $wpdb->update(
            $table_name,
            ['op_val' => $panel],
            ['operazione' => 'panel']
        );        
    }    
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_staging'])) {
        $staging = sanitize_text_field($_POST['staging']);    
        $wpdb->update(
            $table_name,
            ['op_val' => $staging],
            ['operazione' => 'staging']
        );        
    }
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_cloud'])) {
        $drive = json_encode($_POST);
        $wpdb->update(
            $table_name,
            ['op_val' => $drive],
            ['operazione' => 'cloud']
        );        
    }

    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['btn_testoFinale'])) {
        $testoFinale = json_encode($_POST);
        $wpdb->update(
            $table_name,
            ['op_val' => $testoFinale],
            ['operazione' => 'testofinale']
        );        
    }

    $mia_licenza = $wpdb->get_results("SELECT `op_val` FROM $table_name WHERE `operazione`= 'licenza'");   
    $mio_panel = $wpdb->get_results("SELECT `op_val` FROM $table_name WHERE `operazione`= 'panel'"); 
    $mio_staging = $wpdb->get_results("SELECT `op_val` FROM $table_name WHERE `operazione`= 'staging'");  
    $mio_cloud = $wpdb->get_results("SELECT `op_val` FROM $table_name WHERE `operazione`= 'cloud'");  
    $cloudSettings = json_decode($mio_cloud['0']->op_val);
    $mio_testoFinale = $wpdb->get_results("SELECT `op_val` FROM $table_name WHERE `operazione`= 'testoFinale'");  
    $testoFinaleSettings = json_decode($mio_testoFinale['0']->op_val);
?>

<style>
.badge-success {
    color: green;
    background-color: lightgreen;
    border: 1px solid green;
    border-radius: 5px;
    padding: 5px;
}

.badge-denied {
    color: white;
    background-color: lightcoral;
    border: 1px solid red;
    border-radius: 5px;
    padding: 5px;
}
</style>
<!--  Stato sistema -->
<table width="100%">
<tr>

    <!--  Stato Sistema -->
    <td width="50%" style="padding-right: 20px; vertical-align: top;">
        <div class="postbox tools">
            <div class="inside">
                <h3>Stato Sistema</h3>
                <?php
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX;
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                 
                ?>
                DB Impostazioni:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>
                
                <!-- log -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_log';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Log:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- bookings -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_bookings';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Bookings:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- time_slots -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_time_slots';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Time Slots:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- sedi -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_sedi';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Sedi:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- utenti -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_utenti';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Utenti:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- slots_except -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_slots_except';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Slots Except:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- emails -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_emails';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Emails:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- indirizzi -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_indirizzi';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Indirizzi:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>

                <!-- origins -->
                <?php 
                    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_booking_origins';
                    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
                        $infoColorDB = 'red';
                        $infoTitoloDB = 'Mancante';
                    } else {
                        $infoColorDB = 'green';
                        $infoTitoloDB = 'Attivo';
                    }                                                        
                ?>
                DB Origins:<b style="color: <?php echo $infoColorDB; ?>;"> <?php echo $infoTitoloDB; ?></b><br>
            </div>  
        </div>
        <div class="postbox tools">
            <div class="inside">
                <h3>Servizi di sistema attivi</h3>
                <?php if(isset($dataApi['servizi'])) { 
                    foreach ($dataApi['servizi'] as $servizio) {
                        echo "<label class='badge-success'>{$servizio}</label> ";
                    }
                    } else { ?>
                    <label class="badge-denied">Nessun servizio attivo</label>
                <?php } ?>   
            </div>        
        </div>
    </td>

    <!--  Licenza -->
    <td width="50%" >
        <div class="postbox tools">
            <div class="inside">
                <h3>Licenza</h3>
                <form method="POST">
                    <input type="text" name="licenza" id="licenza" size="50" value="<?php echo $mia_licenza['0']->op_val; ?>" required>                                
                    <input type="submit" name="btn_licenza" class="button button-primary" value="Registra">
                </form>
                <p>
                    <label class="text-bold">Licenza valida fino al </label> <label style="font-weight: 700">
                        <?php 
                            global $dataApi; 
                            if(isset($dataApi['scadenza'])) {
                                echo date('d/m/Y', strtotime($dataApi['scadenza']));
                            } 
                        ?>
                    </label>
                </p>
            </div>  
            <div class="inside">
                <h3>Panel</h3>
                <form method="POST">
                    <input type="text" name="panel" id="panel" size="50" value="<?php echo $mio_panel['0']->op_val; ?>" required>                                
                    <input type="submit" name="btn_panel" class="button button-primary" value="Registra">
                </form>
            </div>  
            <div class="inside">
                <h3>Staging</h3>
                <form method="POST">
                    <input type="text" name="staging" id="staging" size="50" value="<?php echo $mio_staging['0']->op_val; ?>" required>                                
                    <input type="submit" name="btn_staging" class="button button-primary" value="Registra">
                </form>
            </div>  
        </div>
    </td>
</tr>

<tr>    

    <!-- Testo Finale -->
    <td width="50%" valign="top">
        <div class="postbox tools">
            <div class="inside">
                <h3>Testo Finale</h3>
                <form method="POST">
                    <div style="margin-bottom: 20px;">
                        <label style="font-weight: 700;">Titolo</label>
                        <input type="text" name="titoloConvalida" id="titoloConvalida" size="50" value="<?php echo isset($testoFinaleSettings->titoloConvalida) ? $testoFinaleSettings->titoloConvalida : ''; ?>" required>    
                    </div>
                    <div style="margin-bottom: 20px;">
                        <label style="font-weight: 700;">Testo</label>
                        <input type="text" name="testoConvalida" id="testoConvalida" size="50" value="<?php echo isset($testoFinaleSettings->testoConvalida) ? $testoFinaleSettings->testoConvalida : ''; ?>">    
                    </div>
                    <div style="margin-bottom: 20px;">
                        <label style="font-weight: 700;">Errore</label>
                        <input type="text" name="erroreConvalida" id="erroreConvalida" size="50" value="<?php echo isset($testoFinaleSettings->erroreConvalida) ? $testoFinaleSettings->erroreConvalida : ''; ?>">    
                    </div>
                    <div>
                        <input type="submit" name="btn_testoFinale" class="button button-primary" value="Registra">
                    </div>
                </form>
            </div>              
        </div>
    </td>
</tr>

<!-- Clouds -->
<?php 
if(isset($dataApi['servizi'])) {
    if(in_array('cloud', $dataApi['servizi'])) { 
?>
<tr>
    <td style="vertical-align: top;" colspan="2">
        <div class="postbox tools">
            <div class="inside" >
                <h3>Cloud</h3>
                <form method="POST">
                    <div class="row">
                        <div style="display: grid; grid-template-columns: 1fr 2fr 2fr 2fr 1fr;">
                            <div>
                                <label for="drive">Tipo:</label>
                                <select name="drive" id="drive">
                                    <!--<option value="dropbox" <?php if(isset($cloudSettings->drive) && $cloudSettings->drive == 'dropbox') echo 'selected'; ?>>Dropbox</option>-->
                                    <option value="google_drive" <?php if(isset($cloudSettings->drive) && $cloudSettings->drive == 'google_drive') echo 'selected'; ?>>Google Drive</option>
                                    <!--<option value="onedrive" <?php if(isset($cloudSettings->drive) && $cloudSettings->drive == 'onedrive') echo 'selected'; ?>>OneDrive</option>-->
                                </select>
                            </div>
                            <div>
                                <label for="refresh_token">Refresh Token:</label>
                                <input type="text" name="refresh_token" id="refresh_token" size="40" value="<?php echo isset($cloudSettings->refresh_token) ? $cloudSettings->refresh_token : ''; ?>" required>
                            </div>
                            <div>
                                <label for="client_id">ClientId:</label>
                                <input type="text" name="client_id" id="client_id"  size="40"  value="<?php echo isset($cloudSettings->client_id) ? $cloudSettings->client_id : ''; ?>" required>
                            </div>
                            <div>
                                <label for="client_secret">ClientSecret:</label>
                                <input type="text" name="client_secret" id="client_secret" class="regular-text" size="40"  value="<?php echo isset($cloudSettings->client_secret) ? $cloudSettings->client_secret : ''; ?>" required>
                            </div>
                            <div>
                                <input type="submit" name="btn_cloud" class="button button-primary" value="Registra">
                            </div>
                        </div>
                    </div> 
                </form>
            </div>
        </div>
    </td>
</tr>
<?php } 
} ?>


</table>