<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ProductRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

/**
 * Class ProductCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class ProductCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
    //use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

    /**
     * Configure the CrudPanel object. Apply settings to all operations.
     * 
     * @return void
     */
    public function setup()
    {
        CRUD::setModel(\App\Models\Product::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/product');
        CRUD::setEntityNameStrings(trans('backend.product.content.item'), trans('backend.product.content.items'));
    }

    /**
     * Define what happens when the List operation is loaded.
     * 
     * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
     * @return void
     */
    protected function setupListOperation()
    {
        $this->crud->addColumns([
            [
                'name' => 'product_catalog_id',
                'label' => trans('backend.columnName.catalog'),
                'type' => 'select',
                'entity' => 'productCatalog',
                'attribute' => 'name',
                'model' => 'App\Models\ProductCatalog',
            ],
            [
                'name'  => 'title',
                'label' => trans('backend.columnName.title'),
                'type'  => 'text'
            ],
            [
                'name'  => 'is_front_show',
                'label' => trans('backend.columnName.is_front_show'),
                'type'  => 'checkbox'
            ],
            [
                'name'  => 'tip',
                'label' => trans('backend.columnName.tip'),
                'type' => 'select_from_array',
                'options' => [
                    'none' => trans('backend.product.tip_options.none'), 
                    'new' => trans('backend.product.tip_options.new'), 
                    'on_sale' => trans('backend.product.tip_options.on_sale'), 
                ],
            ],
            [
                'name'  => 'description',
                'label' => trans('backend.columnName.description'),
                'type'  => 'text'
            ],
        ]);

        $this->crud->addFilter(
            [
                'name'  => 'product_catalog_id',
                'type'  => 'dropdown',
                'label' => trans('backend.columnName.catalog'),
            ],
            \App\Models\ProductCatalog::all()->pluck('name', 'id')->toArray(),
            function ($value) { // if the filter is active
                $this->crud->addClause('whereHas', 'productCatalog', function ($query) use ($value) {
                    $query->where('product_catalog_id', '=', $value);
                });
            }
        );
    }

    /**
     * Define what happens when the Create operation is loaded.
     * 
     * @see https://backpackforlaravel.com/docs/crud-operation-create
     * @return void
     */
    protected function setupCreateOperation()
    {
        CRUD::setValidation(ProductRequest::class);
        $this->crud->addFields([
            [
                'name' => 'product_catalog_id',
                'label' => trans('backend.columnName.catalog'),
                'type' => 'select2_nested',
                'entity' => 'productCatalog',
                'attribute' => 'name',
                'model' => 'App\Models\ProductCatalog',
            ],
            [
                'name' => 'productApplications',
                'label' => trans('backend.columnName.application'),
                'type' => 'select2_multiple',
                'entity' => 'productApplications',
                'attribute' => 'name',
                'model' => 'App\Models\ProductApplication',
                'pivot'     => true,
                'allows_null' => true,
                'select_all' => true,
            ],
            [
                'name' => 'slug',
                'lable' => trans('backend.columnName.slug'),
                'type' => 'text',
            ],
            [
                'name'  => 'title',
                'label' => trans('backend.columnName.title'),
                'type'  => 'text'
            ],
            [
                'name'  => 'is_front_show',
                'label' => trans('backend.columnName.is_front_show'),
                'type'  => 'boolean'
            ],
            [
                'name'  => 'tip',
                'label' => trans('backend.columnName.tip'),
                'type' => 'select2_from_array',
                'options' => [
                    'none' => trans('backend.product.tip_options.none'), 
                    'new' => trans('backend.product.tip_options.new'), 
                    'on_sale' => trans('backend.product.tip_options.on_sale'), 
                ],
            ],
            [
                'name'    => 'photos',
                'label'   => trans('backend.columnName.photos'),
                'type'    => 'upload_img_multiple',
                'upload'  => true,
                'disk'    => 'public',
                'hint'    => '',
                'qty'     => 0, // 0=no limit, >0=limit
                'showSingleChoise' => '1', // 0=hidden, 1=show(default)
                'showComment' => '1', // 0=hidden, 1=show(default)
            ],
            [
                'name'  => 'description',
                'label' => trans('backend.columnName.description'),
                'type'  => 'textarea'
            ],
            [
                'name' => 'body',
                'lable' => trans('backend.columnName.body'),
                'type'  => 'tinymce',
                // optional overwrite of the configuration array
                 'options' => [
                    //'selector' => 'textarea.tinymce',
                    //'skin' => 'dick-light',
                    'plugins' => 'lists advlist image link media anchor table hr imagetools importcss insertdatetime paste searchreplace textcolor textpattern help',
                    'menubar' => 'edit insert view format help',
                    'toolbar' => 'undo redo bold italic alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image media link',
                    'language' => str_replace('-', '_', app()->getLocale()),
                    'height' => '500px',
                ],
            ],
            [
                'name' => 'feature_overview',
                'lable' => trans('backend.columnName.feature_overview'),
                'type'  => 'tinymce',
                // optional overwrite of the configuration array
                 'options' => [
                    //'selector' => 'textarea.tinymce',
                    //'skin' => 'dick-light',
                    'plugins' => 'lists advlist image link media anchor table hr imagetools importcss insertdatetime paste searchreplace textcolor textpattern help',
                    'menubar' => 'edit insert view format help',
                    'toolbar' => 'undo redo bold italic alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image media link',
                    'language' => str_replace('-', '_', app()->getLocale()),
                    'height' => '500px',
                ],
            ],

            [
                'name' => 'feature_spec',
                'lable' => trans('backend.columnName.feature_spec'),
                'type'  => 'tinymce',
                // optional overwrite of the configuration array
                 'options' => [
                    //'selector' => 'textarea.tinymce',
                    //'skin' => 'dick-light',
                    'plugins' => 'lists advlist image link media anchor table hr imagetools importcss insertdatetime paste searchreplace textcolor textpattern help',
                    'menubar' => 'edit insert view format help',
                    'toolbar' => 'undo redo bold italic alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image media link',
                    'language' => str_replace('-', '_', app()->getLocale()),
                    'height' => '500px',
                ],
            ],
            
            [
                'name'  => 'seo_keyword',
                'label' => trans('backend.columnName.seo_keyword'),
                'type'  => 'text'
            ],
            [
                'name'  => 'seo_description',
                'label' => trans('backend.columnName.seo_description'),
                'type'  => 'textarea'
            ],
        ]);
    }

    /**
     * Define what happens when the Update operation is loaded.
     * 
     * @see https://backpackforlaravel.com/docs/crud-operation-update
     * @return void
     */
    protected function setupUpdateOperation()
    {
        $this->setupCreateOperation();
    }
}
