<?php

namespace App\Admin\Controllers\Category;

use App\Models\Article;
use App\Models\Category;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Form as WidgetsForm;
use Illuminate\Support\MessageBag;

class IndexController extends AdminController
{

    protected $title = '分类管理';

    /**
     * Index interface.
     * @return \Closure
     */
    public function grid()
    {
        return function (Row $row) {
            $row->column(6, $this->treeView());

            $row->column(6, function (Column $column) {
                $form = new WidgetsForm();

                $form->select('parent_id', '上级分类')->options(Category::selectOptions(function ($model) {
                    return $model->where('status', 1);
                }, '一级分类'));
                $form->text('title', '分类名称')->rules('required');
                $form->select('type', '分类类型')
                     ->options(Category::TYPES)
                     ->when('show', function (WidgetsForm $form) {
                         //                         $form->text('article_id', '关联文章');

                         $form->select('article_id', '关联文章')
                              ->options(function ($option, $info) {
                                  return Article::whereHas('categories', function ($q) {
                                      $q->where('type', 'show');
                                  })->pluck('title', 'id');
                              })->help('当分类类型是文章详情的时候需要选择关联文章');
                     })
                     ->required();
                $form->textarea('description', '分类简介')
                     ->rules('nullable');
                $form->image('cover', '封面')
                     ->move('images/' . date('Y/m/d'))
                     ->removable()
                     ->uniqueName();
                $form->text('template', '模板');
                $form->number('order', '排序')->default(0);
                $form->switch('top_show', '顶部导航显示')->states()->default(0);
                $form->switch('status', '显示')->states()->default(1);
                $form->action(admin_url('categories'));

                $column->append((new Box('新增分类', $form))->style('success'));
            });
        };
    }

    /**
     * @return Tree
     */
    protected function treeView()
    {
        return Category::tree(function (Tree $tree) {
            $tree->disableCreate();

            $tree->branch(function ($branch) {
                if ($branch['status'] == 1) {
                    $payload = "<i class='fa fa-eye text-primary'></i> ";
                } else {
                    $payload = "<i class='fa fa-eye text-gray'></i> ";
                }
                $payload .= " [ID:{$branch['id']}] - ";
                $payload .= " <strong>{$branch['title']}</strong> ";
                $payload .= " <small>{$branch['type']}</small> ";
                $payload .= " <small style='color:#999'>{$branch['template']}</small>";

                return $payload;
            });
        });
    }

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

        $form->select('parent_id', '上级分类')->options(Category::selectOptions(function ($model) {
            return $model->where('status', 1);
        }, '一级分类'));
        $form->text('title', '分类名称')->rules('required');
        $form->select('type', '分类类型')
             ->options(Category::TYPES)
             ->when('show', function (Form $form) {
                 $form->select('article_id', '关联文章')
                      ->options(function ($option, $info) {
                          return Article::whereHas('categories', function ($q) {
                              $q->where('type', 'show');
                          })->pluck('title', 'id');
                      })->help('当分类类型是文章详情的时候需要选择关联文章');
             })
             ->required();

        $form->textarea('description', '分类简介')->rows(4)->rules('nullable');
        $form->image('cover', '封面')
             ->move('images/' . date('Y/m/d'))
             ->removable()
             ->uniqueName();
        $form->text('template', '模板');
        $form->number('order', '排序')->default(0);
        $form->switch('top_show', '顶部导航显示')->states()->default(0);

        $form->switch('status', '显示')->states()->default(1);
        $form->saving(function (Form $form) {

            if (request()->has('title')) {
                if (request()->type == Category::TYPE_SHOW && empty(request()->article_id)) {
                    //                    $error = new MessageBag([
                    //                        'title'   => '错误',
                    //                        'message' => '文章类型是文章详情的时候需要选择关联文章',
                    //                    ]);
                    //
                    //                    return back()->withInput()->with(compact('error'));
                }
            }

        });

        return $form;
    }

    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

}
