<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CategoryRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

/**
 * Class CategoryCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class CategoryCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation {
        store as traitStore;
    }
    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\Category::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/category');
        CRUD::setEntityNameStrings(
            __('entities.category.singular'),
            __('entities.category.plural'),
        );
        $this->crud->addClause(function (Builder $builder) {
            $builder->where('user_id', backpack_user()->id);
        });
    }

    /**
     * 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->addColumn([
            'name' => 'icon',
            'label' => 'آیکون',
            'type'  => 'custom_html',
            'value' => function($category){
                $icon = $category->icon != ''
                    ? asset("icons/{$category->icon}")
                    : asset('icons/icon-default.png');
                return "<img src='{$icon}' class='mx-auto mb-2' style='width: 40px;'>";
            }
        ]);

        CRUD::setFromDb(); // set columns from db columns.
        $this->crud->removeColumn('user_id');
        $this->crud->removeColumn('user_id');

        /**
         * Columns can be defined using the fluent syntax:
         * - CRUD::column('price')->type('number');
         */
    }

    /**
     * Define what happens when the Create operation is loaded.
     *
     * @see https://backpackforlaravel.com/docs/crud-operation-create
     * @return void
     */
    protected function setupCreateOperation()
    {
        CRUD::setFromDb(); // set fields from db columns.
        $this->crud->removeField('user_id');
        CRUD::field([   // select_from_array
            'name' => 'icon',
            'label' => "آیکون",
            'type' => 'select_from_array',
            'options' => [
                "icon-appetizer.png" => 'پیش غذا',
                "icon-beverage.png" => 'نوشیدنی',
                "icon-bread.png" => 'نان',
                "icon-breakfast.png" => 'صبحانه',
                "icon-burger.png" => 'برگر',
                "icon-calendar.png" => 'تقویم',
                "icon-candy.png" => 'شیرینی، کیک و بستنی',
                "icon-cereal.png" => 'غلات',
                "icon-chicken.png" => 'جوجه',
                "icon-chickenstrips.png" => 'چیکن استریپس',
                "icon-cup.png" => 'فنجان',
                "icon-default.png" => 'پیش فرض',
                "icon-dessert.png" => 'دسر',
                "icon-dizi.png" => 'دیزی',
                "icon-doner.png" => 'دونر',
                "icon-doughnut.png" => 'دونات',
                "icon-food.png" => 'غذا',
                "icon-fruites.png" => 'میوه ها',
                "icon-hotdog.png" => 'هات داگ',
                "icon-international.png" => 'غذای بین‌المللی',
                "icon-juice.png" => 'آب میوه',
                "icon-kalle.png" => 'کله',
                "icon-kebab.png" => 'کباب',
                "icon-kebabrice.png" => 'چلوکباب',
                "icon-lettuce.png" => 'کاهو',
                "icon-marmalade.png" => 'مارمالاد',
                "icon-pasta.png" => 'پاستا',
                "icon-pistachio.png" => 'پسته',
                "icon-pizza.png" => 'پیتزا',
                "icon-pot.png" => 'قابلمه',
                "icon-rice.png" => 'برنج',
                "icon-salad.png" => 'سالاد',
                "icon-sandwich.png" => 'ساندویچ',
                "icon-seafood.png" => 'غذای دریایی',
                "icon-soup.png" => 'سوپ',
                "icon-spice.png" => 'ادویه',
                "icon-steak.png" => 'استیک',
                "icon-sushi.png" => 'سوشی',
                "icon-vegetables.png" => 'سبزیجات',
                "icon-wrap.png" => 'ساندویچ و لقمه',
            ],
            'allows_null' => false,
            'default' => 'one',
            // 'allows_multiple' => true, // OPTIONAL; needs you to cast this to array in your model;
        ]);
        /**
         * Fields can be defined using the fluent syntax:
         * - CRUD::field('price')->type('number');
         */
    }

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

    public function store(Request $request)
    {
        $request->merge(['user_id' => backpack_user()->id]);
        $this->crud->addField('user_id');
//        dd('ads');
        return $this->traitStore();
    }
}
