<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SiteAlbumFolder;
use App\Libraries\CommonHelper;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;

class SiteAlbumFolderController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans( 'admin.folder'))
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SiteAlbumFolder(), function (Grid $grid) {
            $grid->view('admin.grid-custom.table');
            $grid->column('id')->sortable();
            $grid->column('title')->display(function ($title) {
                $level = $this->level; // 获取当前行的 level 字段值
                $has_children = $this->has_children; // 获取当前行的 has_children 字段值
                if ($has_children) {
                    $prefix = str_repeat('&nbsp; ', $level * 2). ' <i class="fa fa-angle-right"></i>&nbsp; '; // 生成 level * 3 个加号
                } else {
                    $prefix = str_repeat('&nbsp; ', $level * 2). ' &nbsp; '; // 生成 level * 3 个加号
                }

                return $prefix . ' ' . html_entity_decode($title) . '&nbsp; ('.html_entity_decode($this->title_en) .')'; // 拼接加号和名称
            });
            $grid->column('cover') ->display(function ($images) {
                return CommonHelper::displayImage([$images],80);
            });
            //$grid->column('folder_type')->using(admin_trans_array(config('dictionary.folder_type')))->label([0 => 'primary', 1 => 'success',]);
            $grid->column('parent_id');
            $grid->column('order')->sortable();
            $grid->column('enabled')->switch();
            $grid->column('created_at')->sortable();

            $grid->disableViewButton();
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->disablePerPages();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SiteAlbumFolder(), function (Form $form) {
            $form->select('parent_id', admin_trans_field('parent'))
                ->options(SiteAlbumFolder::selectMainOptions())
                ->saving(function ($v) {
                    return (int) $v;
                })->default(0);
            $form->text('title')->required();
            $form->text('title_en')->required();
            $form->image('cover')
                ->retainable()//禁止删OSS图
                ->autoUpload()
                ->uniqueName()
                ->accept(config('admin.upload.oss_image.accept'))
                ->maxSize(config('admin.upload.oss_image.max_size'))
                ->dir(config("admin.upload.directory.image").'/uploads/'.date("Ym"))
                ->required();
//            $form->radio('folder_type')
//                ->options(admin_trans_array(config('dictionary.folder_type')))
//                ->default(0)->required();
            $form->listbox('show_tabs')->options(admin_trans_array(config('dictionary.album_tabs')))->default([0, 1, 2, 3, 4, 5, 6, 7])->saving(function ($v) { return json_encode($v); });
            $form->number('order')
                ->default(0)
                ->rules('numeric');
            $form->switch('enabled')->default(1);
        });
    }
}
