<?php

namespace App\Http\Controllers\Administrator;
use App\Http\Controllers\Controller;
// use Alert;
use App\Http\Requests;
use Artisan;
use Log;
use Storage;

class BackupController extends Controller
{
     function __construct()
    {
         $this->middleware('permission:Backup-Database');
    }


    public function index()
    {
        $disk = Storage::disk(config('laravel-backup.backup.destination.disks')[0]);

        $files = $disk->files(config('laravel-backup.backup.name'));
        $backups = [];
        // echo json_encode($files);
        // exit();
        // make an array of backup files, with their filesize and creation date
        foreach ($files as $k => $f) {
            // only take the zip files into account
            if (substr($f, -4) == '.zip' && $disk->exists($f)) {
                $backups[] = [
                    'file_path' => $f,
                    'file_name' => str_replace(config('laravel-backup.backup.name') . '/', '', $f),
                    'file_size' => $disk->size($f),
                    'last_modified' => $disk->lastModified($f),
                ];
            }
        }
        // reverse the backups, so the newest one would be on top
        $backups = array_reverse($backups);

        // echo json_encode($backups);
        // exit();

        return view("backup.backups")->with(compact('backups'));
    }

    public function create()
    {
        try {
            // start the backup process
            Artisan::call('backup:run', [
            '--only-db' => true
            ]);
            $output = Artisan::output();
            // log the results
            Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n" . $output);
            // return the results as a response to the ajax call
            // Alert::success('New backup created');
            return redirect()
            ->back()->with('success','Data backup successfully');
        } catch (Exception $e) {
            Flash::error($e->getMessage());
            return redirect()->back();
        }
    }

    // /**
    //  * Downloads a backup zip file.
    //  *
    //  * TODO: make it work no matter the flysystem driver (S3 Bucket, etc).
    //  */
    // public function download($file_name)
    // {
    //     $file = config('crm_ml_realties.backup.name') . '/' . $file_name;
    //     $disk = Storage::disk(config('crm_ml_realties.backup.destination.disks')[0]);
    //     if ($disk->exists($file)) {
    //         $fs = Storage::disk(config('crm_ml_realties.backup.destination.disks')[0])->getDriver();
    //         $stream = $fs->readStream($file);

    //         return \Response::stream(function () use ($stream) {
    //             fpassthru($stream);
    //         }, 200, [
    //             "Content-Type" => $fs->getMimetype($file),
    //             "Content-Length" => $fs->getSize($file),
    //             "Content-disposition" => "attachment; filename=\"" . basename($file) . "\"",
    //         ]);
    //     } else {
    //         abort(404, "The backup file doesn't exist.");
    //     }
    // }

    // /**
    //  * Deletes a backup file.
    //  */
    // public function delete($file_name)
    // {
    //     $disk = Storage::disk(config('crm_ml_realties.backup.destination.disks')[0]);
    //     if ($disk->exists(config('crm_ml_realties.backup.name') . '/' . $file_name)) {
    //         $disk->delete(config('crm_ml_realties.backup.name') . '/' . $file_name);
    //         return redirect()->back();
    //     } else {
    //         abort(404, "The backup file doesn't exist.");
    //     }
    // }
}