<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Column;
use App\Models\ProductType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ColumnController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Column(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('product_type_id')->display(function (){
                $lang = request()->session()->get('admin.config.lang');
                if ($lang == 'ko'){
                    $data = ProductType::query()->whereIn('id',$this->product_type_id)->pluck('ko_name')->toArray();
                }else{
                    $data = ProductType::query()->whereIn('id',$this->product_type_id)->pluck('zh_name')->toArray();
                }
                return $data;
            })->label();
            $grid->column('zh_name');
            $grid->column('ko_name');
            $grid->column('sort');
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Column(), function (Show $show) {
            $show->field('id');
            $show->field('product_type_id');
            $show->field('zh_name');
            $show->field('ko_name');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Column(), function (Form $form) {
            $form->display('id')->width(4);
            $form->multipleSelect('product_type_id')->options(ProductType::selectOptions())->required();
            $form->text('zh_name')->required()->width(4);
            $form->text('ko_name')->required()->width(4);
            $form->number('sort')->width(4);
            $form->switch('status')->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
