<?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 App\Libraries\CommonHelper;
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 AdminDistController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans( 'admin.product_import'))
            ->description('<span style="color: red; font-weight: bold;">'.admin_trans_label('select_products_to_import').'</span>')
            ->breadcrumb(['text'=>'list','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(BaseProduct::with(['baseProductCategory','images']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('sku');
            $grid->column('base_product_category.name',admin_trans_label('category_name'));
            $grid->column('keywords');
            $grid->column('description');
            $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(admin_trans_array( config('dictionary.enabled')))->width(2);
            });
            // 删除新增按钮
            $grid->disableCreateButton();
            //$grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
            // 添加批量复制操作
            $grid->batchActions(function ($batch) {
                //$batch->add(new BatchCopy()); 只能2选1
            });

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

            $grid->model()->where('enabled',1)->orderBy("is_pinned",'desc')->orderBy("order",'desc');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, BaseProduct::with(['baseProductCategory','images']), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('sku');
            $show->field('base_product_category.name',admin_trans_label('category_name'));
            $show->field('keywords');
            $show->field('description');
            $show->field('issuance_date');
            $show->field('parameters')->as(function ($items) {
                if (is_array($items)) {
                    // 创建表格的表头
                    $table = '<table class="table">';
                    $table .= '<tr><th>key</th><th>value</th></tr>';
                    // 遍历数组并将数据填充到表格中
                    foreach ($items as $item) {
                        $table .= '<tr>';
                        $table .= '<td>' . $item['key'] . '</td>';    // 商品名称
                        $table .= '<td>' . $item['value'] . '</td>'; // 数量
                        $table .= '</tr>';
                    }
                    $table .= '</table>';
                    return $table;
                }
                return ''; // 当没有数组数据时
            })->unescape();
            $show->field('images')->as(function ($images) {
                // 开始生成 HTML
                $dataImages = array_column($images, 'image_url');
                return CommonHelper::displayImage($dataImages,150);
            })->unescape();
            $show->field('content')->unescape();
            $show->field('seo_title');
            $show->field('seo_keywords');
            $show->field('seo_description');
            //$show->field('created_at');
            //$show->field('updated_at');
            // 禁用操作
            $show->disableEditButton();
            $show->disableDeleteButton();
        });
    }

}
