<?php

$cekID = new cekID();
$cekID->cek('serial', 'no', 1);


$clsCrud = new crudData();
$view = $clsCrud->editData('serial', 'no', 1);
$data = $view->fetch(PDO::FETCH_OBJ);


?>
<style>
    .fontInput {
        font-size: 11px;
        font-family: arial;
    }

    .judul {
        font-size: 15px;
        font-family: arial;
    }
</style>

<div class="content-wrapper">

    <?php require('./layout/headerpage.php'); ?>

    <div class="container col-12 mb-4 pb-5">
        <div class="row  mx-3 pr-3">
            <div class="col-12">
                <div class="col-12 ">
                    <div class="card  mx-3">
                        <div class="timeline-header bg-primary py-2 pl-2">
                            <b class="text-light judul"><span class="bi bi-device-ssd"></span> Backup Restore Data </b>
                        </div>

                        <div class="row col-md-12">
                            <div class="col-md-6">
                                <div class="card-body mt-4 px-1 mx-2">

                                    <div class="row">
                                        <div class="col-12 col-sm-6 col-md-12">
                                            <div class="info-box mb-3">


                                                <div class="info-box-content">
                                                    <div class="relative mb-0 pb-4">
                                                        <input name="judul" value="BACKUP DATABASE" style="background-color: gainsboro;" class="form-control-sm col-sm-3 fontInput small bg-default mt-3 text-center" disabled>
                                                    </div>

                                                    <div class="col-sm-12 invoice-col pt-0">

                                                        <form action="" method="post">

                                                            <?php $csrf->token(); ?>
                                                            <input type="text" class="form-control form-control-sm" value="<?php echo $data->id; ?>" name="id" hidden />
                                                            <div class="row">

                                                                <div class="col-md-12">
                                                                    <b class='text-danger'>Record Data Tahun <?php echo $tahun; ?></b>
                                                                    <?php


                                                                    $backup = new backup;
                                                                    $link = new linkpage;
                                                                    $clsCrud = new crudData;
                                                                    $id_tahun = $clsCrud->getValue('tb_tp', " where tp='$tahun'", 'id');
                                                                    echo "<table class='table table-sm table-stipped'>
                                                                        <tr>
                                                                            <td>" . $backup->countRecord($tahun) . "</td>
                                                                        </tr>
                                                                        </table>";

                                                                    ?>
                                                                </div>

                                                            </div>

                                                            <div class='row'>
                                                                <span class="ml-3">Database yang terbackup hanya data Tahun Pelajaran <?php echo $tahun; ?></span>
                                                                <div class="col-12 py-0 mb-3">
                                                                    <div class="row float-left">

                                                                        <div class="col-auto">
                                                                            <button type="submit" class="btn btn-primary btn-xs mx-1 px-2 mt-1" name="backup"><i class="bi-save"></i> PROSES BACKUP DATA </button>
                                                                        </div>
                                                                        <div class="col-auto">
                                                                            <?php
                                                                            $servroot = $_SERVER['DOCUMENT_ROOT'];
                                                                            //$servweb = $_SERVER['HTTP_HOST'];
                                                                            $clsTime = new time;
                                                                            $metode = $clsTime->getDateBackup() . "_M";

                                                                            $tp_back = substr($tahun, 0, 4) . '_' . substr($tahun, 5, 4);

                                                                            if (file_exists($servroot . "/asset/backup/AKSSIMAKOM6_" . $tp_back . "_" . $metode . ".zip") || file_exists("../../asset/backup/AKSSIMAKOM6_" . $tp_back . "_" . $metode . ".zip")) {
                                                                                echo "<a href='../../asset/backup/AKSSIMAKOM6_" . $tp_back . "_" . $metode . ".zip'><button type='button' class='btn btn-secondary btn-xs mx-1 mt-1' name='download'><i class='bi-download'></i> DOWNLOAD FILE BACKUP</button></a>";
                                                                            } else {
                                                                                echo "";
                                                                            };

                                                                            ?>
                                                                        </div>

                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </form>


                                                    </div>
                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="card-body mt-4 px-1 mx-2">

                                    <div class="row">
                                        <div class="col-12 col-sm-6 col-md-12">
                                            <div class="info-box mb-3">


                                                <div class="info-box-content">
                                                    <div class="relative mb-0 pb-4">
                                                        <input name="judul" value="RESTORE DATABASE" style="background-color: gainsboro;" class="form-control-sm col-sm-3 fontInput small bg-default mt-3 text-center" disabled>
                                                    </div>

                                                    <div class="col-sm-12 invoice-col pt-0">

                                                        <form action="" method="post" enctype="multipart/form-data">

                                                            <?php $csrf->token(); ?>
                                                            <input type="text" class="form-control form-control-sm" value="<?php echo $tahun; ?>" name="restore" hidden />

                                                            <div class="form-group">
                                                                <label for="exampleInputFile">File Backup (Format File : <i class="text-danger">AKSSIMAKOM6_..sql</i>)</label>
                                                                <div class="input-group">
                                                                    <div class="custom-file">
                                                                        <input type="file" class="custom-file-input" id="exampleInputFile" name="datafile" accept=".sql">
                                                                        <label class="custom-file-label" for="exampleInputFile">Pilih File Backup</label>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class='row mt-4'>

                                                                <div class="col-12 py-0 mb-4">
                                                                    <div class="row float-right">
                                                                        <div class="col-auto">
                                                                            <button type="submit" class="btn btn-primary btn-xs mx-1 px-2 mt-1" name="subrestore" onclick="return confirm('Apakah Anda yakin akan restore database?')"><i class="bi-gear"></i> RESTORE </button>
                                                                        </div>

                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </form>
                                                    </div>
                                                </div>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function() {
        bsCustomFileInput.init();
    });
</script>
<?php

$clsbackup = new backup();
if (isset($_POST['backup'])) {
    $csrf = $csrf->cekToken();
    $clsbackup->backup_proses($tahun, 'M');
} elseif (isset($_POST['subrestore'])) {
    $csrf = $csrf->cekToken();
    $clsbackup->restore($tahun);
} else {
    echo '';
}
