<?php

namespace App\Distributor\Controllers;


use App\Distributor\Actions\Extensions\DistProductImportForm;
use App\Distributor\Repositories\BaseProduct;
use App\Admin\Repositories\BaseProductCategory;
use App\Distributor\Actions\BatchCopy;
use App\Distributor\Repositories\DistProductCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

use Dcat\Admin\Admin;

class ImportProductController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('产品导入')
            ->description('<span style="color: red; font-weight: bold;">'.admin_trans_label('select_products_to_import').'</span>')
            ->breadcrumb(['text'=>'列表','url'=>''])
            ->body($this->grid());
    }



    //屏蔽删除
    public function destroy($id)
    {
        abort(404);
    }

    //屏蔽创建
    public function create(Content $content)
    {
        abort(404);
    }

    //屏蔽编辑
    public function edit($id, Content $content)
    {
        abort(404);
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BaseProduct(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('keywords');
            $grid->column('description');
            $grid->column('sku');
            $grid->column('category_id');
            $grid->column('issuance_date');

            // 筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('sku')->width(2);
                $filter->like('title')->width(2);
                $filter->equal('category_id',admin_trans_label('category'))->select(BaseProductCategory::selectOptions())->width(2);
                $filter->equal('enabled', admin_trans_label('enabled'))->select(array_map('admin_trans_label', config('dictionary.enabled')))->width(2);
            });
            //$grid->column('order');
           //$grid->column('enabled');
            ///$grid->column('content');
            //$grid->column('parameters');
            //$grid->column('is_pinned');
            // 删除新增按钮
            $grid->disableCreateButton();
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();


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

            //$grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
            // 添加批量复制操作
            $grid->batchActions(function ($batch) {
                //$batch->add(new BatchCopy()); 只能2选1
            });

            $grid->tools([
                new DistProductImportForm(),
            ]);


        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BaseProduct(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('keywords');
            $show->field('description');
            $show->field('sku');
            $show->field('category_id');
            $show->field('issuance_date');
            $show->field('order');
            $show->field('enabled');
            $show->field('content');
            $show->field('parameters');
            $show->field('is_pinned');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BaseProduct(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('keywords');
            $form->text('description');
            $form->text('sku');
            $form->text('category_id');
            $form->text('issuance_date');
            $form->text('order');
            $form->text('enabled');
            $form->text('content');
            $form->text('parameters');
            $form->text('is_pinned');

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

        });
    }
}
