<?php

namespace App\Admin\Controllers\Tool;

use App\Http\Controllers\Controller;
use App\Models\Oauth;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class OauthController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('合作账号')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('合作账号')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('合作账号')
            ->description('编辑')
            ->body($this->editForm($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('合作账号')
            ->description('创建')
            ->body($this->createForm());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Oauth);

        $grid->id('ID');
        $grid->name('名称');
        $grid->alias('别名');
        $states = [
            'on'  => ['value' => 1, 'text' => '开启', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
        ];
        $grid->status('状态')->switch($states);
        $grid->order('排序');
        $grid->created_at('添加时间');
        $grid->updated_at('更新时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Oauth::findOrFail($id));

        $show->id('ID');

        $show->name('名称');
        $show->alias('别名');
        $show->app_id('appid');
        $show->app_key('appkey')->as(function () {
            return '***********';
        });
        $show->status('状态')->as(function ($status) {
            return $status ? '开启' : '关闭';
        });
        $show->order('排序');
        $show->created_at('添加时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Oauth);

        $form->display('ID');
        $form->display('添加时间');
        $form->display('更新时间');

        return $form;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function editForm($id)
    {
        $form = new Form(new Oauth);
        $form->text('name', '名称');
        $form->display('alias', '别名');
        $form->text('app_id', 'appid')->rules([
            'required',
        ]);
        $form->password('app_key', 'appkey')->rules([
            'required',
        ]);
        $form->radio('status', '状态')->options([1=>'开启',0=>'关闭']);
        $form->number('order', '排序')->min(0);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
        });
        $form->saving(function (Form $form) {
            if ($form->status=='on') {
                $form->status=1;
            } elseif ($form->status=='off') {
                $form->status=0;
            }
        });
        return $form;
    }

    protected function createForm()
    {
        $form = new Form(new Oauth);
        $form->text('name', '名称')->rules([
            'required',
        ]);
        $form->text('alias', '别名')->rules([
            'required',
            'unique:oauths',
        ]);
        $form->text('app_id', 'appid')->rules([
            'required',
        ]);
        $form->text('app_key', 'appkey')->rules([
            'required',
        ]);
        $form->radio('status', '状态')->options([1=>'开启',0=>'关闭'])->default(1);
        $form->number('order', '排序')->min(0);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
        });
        return $form;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return mixed
     */
    public function store()
    {
        return $this->createForm()->store();
    }
    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function update($id)
    {
        return $this->editForm($id)->update($id);
    }
}