<?php

namespace App\Admin\Controllers;

use App\Dealer;
use App\Good;
use App\Trace;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class CodeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        $gid = $request->get('gid', false);
        if ($gid) {
            return Admin::content(function (Content $content) use ($gid) {

                $content->header('码库管理');
                $content->description('列表');

                $content->body($this->grid1($gid));
            });
        }
        return Admin::content(function (Content $content) {

            $content->header('码库管理');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('码库管理');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('码库管理');
            $content->description('创建');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Trace::class, function (Grid $grid) {
            $grid->disableCreation();
            $grid->disableExport();
            $grid->disableRowSelector();

            $grid->id('ID')->sortable();

            $grid->key('编码');
            $grid->slug_value('秘钥');
            $grid->good()->name('产品名');
            $grid->group('分组编号');
            $grid->dealer_id('经销商')->select(Dealer::getOption());

            $grid->created_at('创建日期');

            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });

            $grid->filter(function ($filter) {

                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
//                $filter->like('key', '编号');

                $filter->where(function ($query) {
                    $id = $this->input;
                    $id = Trace::get_key($id);
                    $query->where('id', 'like', $id);
                }, '编号');

                $filter->like('slug', '秘钥');

                $filter->in('gid', '产品')->multipleSelect(Good::pluck('name', 'id'));;

                $filter->equal('group', '分组');
            });
        });
    }

    protected function grid1($gid)
    {
        return Admin::grid(Trace::class, function (Grid $grid) use ($gid) {
            $grid->model()->where('gid', $gid)->groupBy('group');
            $grid->model()->orderBy('id', 'asc');

            $grid->disableCreation();
            $grid->disableExport();
            $grid->disableRowSelector();

            $grid->id('ID')->sortable();

            $grid->key('编码');
            $grid->slug_value('秘钥');
            $grid->good()->name('产品名');
            $grid->group('分组编号')->sortable();
            $grid->dealer_id('经销商')->select(Dealer::getOption());

            $grid->created_at('创建日期');

            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });

            $grid->filter(function ($filter) {

                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
//                $filter->like('key', '编号');

                $filter->where(function ($query) {
                    $id = $this->input;
                    $id = Trace::get_key($id);
                    $query->where('id', 'like', $id);
                }, '编号');

                $filter->like('slug', '秘钥');

                $filter->equal('group', '分组');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Trace::class, function (Form $form) {
            $form->setAction('admin/code');

            $form->display('id', 'ID');

            $form->text('key', '编码')->readOnly();
            $form->text('slug_value', '秘钥')->readOnly();
            $form->select('gid', '产品')->options(Good::pluck('name', 'id'))->readOnly();
            $form->text('group', '分组')->readOnly();
            $form->select('dealer_id', '供应商')->options(Dealer::getOption());

            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
