<?php

namespace App\Admin\Controllers;

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

class ProductTypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProductType(), function (Grid $grid) {
            $grid->model()->orderByDesc('sort');
            $grid->column('id')->bold()->sortable();
            $grid->column('icon')->image('',40);
            $lang = request()->session()->get('admin.config.lang');
            if ($lang == 'ko'){
                $grid->ko_name->tree(); // 开启树状表格功能
            }else{
                $grid->zh_name->tree(); // 开启树状表格功能
            }
            $grid->column('is_upload_display')->switch();
            $grid->column('is_filter_display')->switch();
            $grid->column('sort');
            $grid->column('status')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name',admin_trans_field('name'))->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProductType(), function (Show $show) {
            $show->field('id');
            $show->field('pid');
            $show->field('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 ProductType(), function (Form $form) {
            $form->display('id')->width(4);
            $form->image('icon')->autoUpload()->uniqueName()->width(4);
            $form->select('pid')->options(ProductType::selectOptions())->required()->width(4);
            $form->text('zh_name')->width(4)->required();
            $form->text('ko_name')->width(4)->required();
            $form->text('zh_alias')->width(4);
            $form->text('ko_alias')->width(4);
            $form->textarea('describe')->width(4);
            $form->number('sort');
            $form->switch('status')->default(1);
            $form->hidden('route_id');
            $form->hidden('filter_display_pid');
            $form->hidden('upload_display_pid');
            $form->hidden('is_filter_display')->default(1);
            $form->hidden('is_upload_display')->default(1);

            $form->saving(function (Form $form) {
                if ($form->isEditing()) {
                    if($form->is_filter_display == 0){ // 特殊筛选
                        $id = $form->getKey();
                        $ProductType = ProductType::query()->where('id',$id)->first();
                        ProductType::query()->where('pid','=',$id)
                            ->update(['filter_display_pid'=>$ProductType->pid]);
                    }elseif ($form->is_filter_display == 1){
                        $id = $form->getKey();
                        ProductType::query()->where('pid','=',$id)->update(['filter_display_pid'=>null]);
                    }
                    if($form->is_upload_display == 0){ // 特殊上传
                        $id = $form->getKey();
                        $ProductType = ProductType::query()->where('id',$id)->first();
                        ProductType::query()->where('pid','=',$id)
                            ->update(['upload_display_pid'=>$ProductType->pid]);
                    }elseif ($form->is_upload_display == 1){
                        $id = $form->getKey();
                        ProductType::query()->where('pid','=',$id)
                            ->update(['upload_display_pid'=>null]);
                    }
                    if(!empty($form->pid)){
                        $form->level = count(ProductType::level($form->pid)) + 1; // 加一级
                        $form->route_id = implode(',',ProductType::level($form->pid)); // 加一级
                    }else{
                        $form->level = 1; // 加一级
                        $form->route_id = 0;
                    }
                }
            });
        });
    }
}
