<?php
namespace App\Admin\Actions\User;

use App\Admin\Imports\User;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class UserImport extends Action
{
    public $name = '导入网点编号';

    protected $selector = '.import-post';
    public $modelName   = '';

    public function handle(Request $request)
    {
        $res = Excel::import(new User, $request->file('file'), 'excel');
        if ($res === true) {
            return $this->response()->success('导入完成！')->refresh();
        } else {
            return $this->response()->error('失败' . $res)->refresh();
        }
    }

    public function form()
    {
        $this->file('file', '请选择文件');
    }

    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-default import-post"><i class="fa fa-upload"></i>导入数据</a>
HTML;
    }
}
