<?php

namespace App\Admin\Controllers;

use App\Models\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ServiceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Config(), function (Grid $grid) {
            $grid->model()->whereIn('id',[7,8,9]);
            $grid->column('id')->sortable();
            $grid->column('desc','标题');
            $grid->column('value', '值')->display(function ($item) use ($grid) {
                // 在这里通过 $this 获取当前行的数据
                $id = $this->id;
                // 根据条件判断是否显示值列
                if ($id == 9) {
                    return "<a target='_blank' href='https://zhengda.oss-cn-chengdu.aliyuncs.com/".$this->value."'>查看二维码</a>";
                } else {
                    return $item;
                }
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Config(), function (Show $show) {
            $show->field('id');
            $show->field('key');
            $show->field('value');
            $show->field('desc');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Config(), function (Form $form) {
            $form->display('id');
            if ($form->model()->id == 9){
                $form->image('value','二维码')->disk('oss')->autoUpload()->saving(function ($res) {
                    return $res;
                })->required();
            }else{
                $form->text('value','值')->required();
            }
            $form->display('desc','标题');
            $form->disableViewButton();
            $form->disableDeleteButton();
        });
    }
}
