<?php

namespace app\controller;

use think\facade\Db;
use think\facade\Env;
use think\facade\View;
use think\Request;

class Backup extends Common
{
    public $backup,$backup_config;

    public function initialize()
    {
        error_reporting(0);
        $this->backup_config = config('app.backup');
        $this->backup = new \wamkj\thinkphp\Backup($this->backup_config);

        return parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index(Request $request)
    {
        $list = [];
        $filelist = $this->backup->fileList();
        foreach ($filelist as $v) {
            $filename = $this->backup->getFile('time',$v['time'])[0];
            $list[] = [
                'filename'=>substr($filename, strrpos($filename, "/") + 1, strlen($filename)),
                'size'=>ByteSize($v['size']),
                'create_time'=>date('Y-m-d H:i:s',$v['time']),
                'time'=>$v['time']
            ];
        }
        rsort($list);
        $data['list'] = $list;
        return View::fetch('index', $data);
    }

    // 备份
    public function backup()
    {
        $filename = $this->backup_config['path'] . 'runtime.lock';
        file_put_contents($filename, time());
        $time = time();
        $file = ['name' => date('Ymd-His', $time), 'part' => 1];
        foreach ($this->backup->dataList() as $v) {
            $this->backup->setFile($file)->backup($v['name'], 0);
        }
        return $this->success('数据库备份成功');
    }

    // 恢复
    public function recovery(Request $request)
    {
        echo '<pre>';
        $time = $request->param('time');
        $filename = $this->backup->getFile('time',$time)[0];
        if (!file_exists($filename)) {
            return $this->error('文件不存在');
        }
        exit;
        //$filename = substr($filename, strrpos($filename, "/") + 1, strlen($filename));
        $file['name'] = $filename;
        $file[] = $filename;
        $file[] = $filename;
        $result = $this->backup->setFile($file)->import(0);
        if ($result) {
            var_dump($result);exit;
            return $this->success('数据库恢复成功');
        } else {
            return $this->error('数据库恢复失败');
        }
    }

    // 下载
    public function download(Request $request)
    {
        $time = $request->param('time');
        if (!$time) {
            return $this->error('参数错误');
        }
        $filename = $this->backup->getFile('time',$time)[0];
        if (!file_exists($filename)) {
            return $this->error('文件不存在');
        }
        $filename = substr($filename, strrpos($filename, "/") + 1, strlen($filename));
        download($this->backup_config['path'],$filename);
        exit ();
    }

    // 删除
    public function del(Request $request)
    {
        $time = $request->param('time');
        if (!$time) {
            return $this->error('参数错误');
        }
        $result = $this->backup->delFile($time);
        if ($result == $time) {
            return $this->success('数据库备份文件删除成功');
        } else {
            return $this->error($result);
        }
    }
}