<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\AppearanceImport;
use App\Admin\Actions\Grid\ProductAudit;
use App\Admin\Repositories\DistAdminDistributor;
use App\Admin\Repositories\DistAppearance;
use App\Admin\Repositories\DistProduct;
use App\Admin\Repositories\DistProductCategory;
use App\Libraries\CommonHelper;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;

class DistProductAuditController extends AdminController
{

    protected function title()
    {
        return admin_trans('admin.product');
    }
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans( 'admin.products_list'))
            ->description(admin_trans('admin.all'))
            ->description('')
            //->breadcrumb(['text'=>'Product Management','url'=>''])
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(DistProduct::with(['distProductCategory','images']), function (Grid $grid)  {
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            //$grid->model()->where('dist_id', getDistributorId());
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('title');
            $grid->column('sku');
            $grid->column('dist_product_category.name',admin_trans_label('category_name'));
            $grid->column('images')->display(function ($images) {
                $images = $images->toArray();
                $dataImages = array_column($images, 'image_url');
                // 限制最多显示1个缩略图
                $dataImages = array_slice($dataImages, 0, 1);
                return CommonHelper::displayImage($dataImages,100);
            });

            $grid->column('dist_id',admin_trans_label('distributor_name')) ->display(function () {
                return DistAdminDistributor::getClientCodeById($this->dist_id);
            });

            $grid->column('status')->using(admin_trans_array(config('dictionary.dist_product_status')))->label([
                -1 => 'danger',
                0 => 'gray',
                1 => 'orange1',
                2 => 'success',
            ])->display(function($status) {
                if ($this->status == -1) {
                    return '<span  class="status-popover" data-container="body" data-toggle="popover" data-placement="top" data-content="'.$this->review_reply.'">'.$status.'</span>';
                }
                return $status;
            });
            $grid->column('created_at')->sortable();
            //$grid->column('updated_at')->sortable();
            // 筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('title')->width(2);
                $filter->equal('sku')->width(2);
                $filter->equal('status')->select(admin_trans_array(config('dictionary.dist_product_status')))->width(2);
                $filter->equal('dist_id',admin_trans_label('distributor_name'))->select(DistAdminDistributor::selectOptionsNew(300))->width(3);
            });
            //排序
            $grid->model()->orderBy('created_at', 'desc')->orderBy('id', 'desc');
            //操作
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($this->status == 1) {
                    $actions->append(new ProductAudit());
                }
            });
            //按钮
            //$grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableBatchDelete();
            $this->addGridJS();
        });
    }


    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {

        return Show::make($id, DistProduct::with(['distProductCategory','images']), function (Show $show) {
            $show->field('title');
            $show->field('sku');
            $show->field('dist_product_category.name',admin_trans_label('category_name'));
            $show->field('keywords');
            $show->field('description');
            $show->field('issuance_date');
            $show->field('parameters',admin_trans_label('attribute_name'))->as(function ($items) {
                if (is_array($items)) {
                    // 创建表格的表头
                    $table = '<table class="table table-bordered table-condensed">';
                    // 遍历数组并将数据填充到表格中
                    foreach ($items as $item) {
                        $table .= '<tr>';
                        $table .= '<td style="vertical-align: middle !important;width: 20%">' . $item['key'] . '</td>';    // 商品名称
                        $table .= '<td style="vertical-align: middle !important;">' . $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('order');
            $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableEditButton();
            $show->disableDeleteButton();
        });
    }

    private function addGridJS()
    {
        Admin::script(
            <<<JS
 $('.status-popover').popover({
    container: 'body',
     trigger: 'hover'
  })
JS
        );
    }
}
