<?php

namespace App\Admin\Controllers\Content;

use App\Http\Controllers\Controller;
use App\Models\ArticleProperty;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ArticlePropertyController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('新闻属性')
            ->description('系统默认属性不能删除!')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('新闻属性')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('新闻属性')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新闻属性')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ArticleProperty);

        $grid->model()->orderBy('list_order', 'DESC')->orderBy('created_at', 'DESC');

        $grid->category_name('属性名称');
        $grid->admin_set('系统属性')->display(function ($admin_set) {
            return $admin_set?"是":"否";
        });
        $grid->list_order('排序');
        $grid->created_at('添加时间');
        $grid->actions(function ($actions) {
            //行操作
            if (Admin::user()->can('content_article_property_edit')) {
                $actions->disableEdit(false);
            }
            if (Admin::user()->can('content_article_property_delete') && $actions->row['admin_set']!=1) {
                $actions->disableDelete(false);
            }
        });
        $grid->disableFilter();

        //新增按钮
        if (Admin::user()->can('content_article_property_create')) {
            $grid->disableCreateButton(false);
        }
        //批量删除
        if (Admin::user()->can('content_article_property_delete')) {
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete(false);
                });
            });
        } else {
            $grid->disableRowSelector();
        }
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ArticleProperty::findOrFail($id));

        $show->id('ID');
        $show->category_name('属性名称');
        $show->admin_set('系统分类')->as(function ($admin_set) {
            return $admin_set?'是':'否';
        });
        $show->list_order('排序');
        $show->created_at('添加时间');
        $show->updated_at('更新时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ArticleProperty);
        $form->text('category_name', '属性名称')->rules('required|max:25', array('required'=>'属性名称不能为空。','max'=>'属性名称长度不能大于25个字符。'))->setWidth(4)->setMustMark();
        $form->number('list_order', '排序')->min(0)->default(0)->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)')->setMustMark();
        $form->hidden('admin_set')->value(0);

        $form->footer(function ($footer) {
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        return $form;
    }
    public function destroy($id)
    {
        $id_arr =  explode(',', $id);
        $admin_set_cates = ArticleProperty::where(array('admin_set'=>'1'))->select('id')->get()->pluck('id')->toArray();
        foreach ($id_arr as $k => $v) {
            if (in_array($v, $admin_set_cates)) {
                unset($id_arr[$k]);
            }
        }
        $filter_id = implode(',', $id_arr);
        if ($this->form()->destroy($filter_id)) {
            $data = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } else {
            $data = [
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ];
        }
        return response()->json($data);
    }
}