<?php

namespace App\Distributor\Controllers;


use App\Distributor\Repositories\DistAppearance;
use App\Distributor\Actions\AppearanceEnable;
use App\Distributor\Repositories\DistAdminDistributor;
use App\Libraries\CommonHelper;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;

class DistAppearanceController extends AdminDistController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans( 'admin.site_appearance'))
            ->description('')
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $distInfo = DistAdminDistributor::getInfo();
        return Grid::make(new DistAppearance(), function (Grid $grid) use ($distInfo) {
            //指定视图，去掉删除按钮
            $grid->view('admin.grid.table');
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            // 标题
            $grid->column('title');
            // 封面图
            $grid->column('cover_image')->display(function ($image) {
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            })->width('15%');
            $grid->column('describe')->width('30%');
            $grid->column('isEnabled')->display(function () use ($distInfo) {
                if ($this->id == $distInfo->appearance_id) {
                    return '<span class="label" style="background:#21b978;">'.admin_trans_label('Yes').'</span>';
                } else {
                    return '<span class="label" style="background:#d2d6de;">'.admin_trans_label('No').'</span>';
                }

            });
            //按钮
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
            $grid->tools('<a href="'.getDistributorDomain().'" target="_blank"><button  class="btn btn-primary grid-refresh btn-mini" style="margin-right:3px"><i class="feather icon-chrome"></i><span class="d-none d-sm-inline">&nbsp; '.admin_trans_label('browse').'</span></button></a>');
            // 操作
            $grid->actions(function (Grid\Displayers\Actions $actions) use ($distInfo) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();
                // 启用
                $actions->append(new AppearanceEnable());
                //可视化编辑
                $isEnabled = $actions->row->id == $distInfo->appearance_id;
                if ($isEnabled) {
                    $edit = admin_trans_label('visual_edit');
                    $actions->append('<a style="cursor: pointer;"  href="/dist/visual-editor" target="_blank"><i class="feather icon-edit"></i>&nbsp;&nbsp;'.$edit.'</a>');
                }
            });
            //排序
            $grid->model()->where('enabled',1)->orderBy('order','desc')->orderBy('id','desc');
        });
    }


}
