<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\InitAppearance;
use App\Admin\Repositories\DistAdminDistributor;
use App\Admin\Repositories\DistAppearance;
use App\Libraries\CommonHelper;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\DB;

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

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(DistAdminDistributor::with(['appearance']), function (Grid $grid) {
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            //指定视图,去掉删除按钮
            $grid->view('admin.grid.table');

            $grid->column('id')->sortable();
            $grid->column('client_code',admin_trans_label('distributor_code'))->width('15%');
            $grid->column('site_name');
            $grid->column('appearance.title' ,admin_trans_field('appearance'));
            $grid->column('domain_type')->using(admin_trans_array(config('dictionary.domain_type')));
            $grid->column('secondary_domain');
            $grid->column('custom_domain');
            $grid->column('updated_at')->sortable();
            // 过滤
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('dist_code',admin_trans_label('client_code') )->width(2);
                $filter->equal('site_name' )->width(2);
            });
            // 排序
            $grid->model()->where('enabled',1)->orderBy("id",'desc');
            //按钮
            $grid->enableDialogCreate();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            //批量操作
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                $batch->disableDelete();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, DistAdminDistributor::with(['appearance']), function (Show $show) {
            $show->field('domain_type',admin_trans_label('domain_type'))->using(admin_trans_array(config('dictionary.domain_type')));
            $show->field('secondary_domain');
            $show->field('custom_domain');
            $show->divider();
            $show->field('site_name');
            $show->field('appearance.title' ,admin_trans_field('appearance'));
            $show->field('copy_right');
            $show->field('logo')->as(function ($image) {
                // 开始生成 HTML
                $dataImages = [$image];
                return CommonHelper::displayImage($dataImages,100);
            })->unescape();
            $show->field('seo_title');
            $show->field('seo_keywords');
            $show->field('seo_description');

            $show->divider();
            $show->field('company_name');
            $show->field('company_address');
            $show->field('country');
            $show->field('contact_number');
            $show->field('service_hotline');
            $show->field('whats_app');
            $show->field('facebook');
            $show->field('instagram');
            $show->field('youtube');
            $show->field('linkedin');
            $show->field('tiktok');
            $show->field('copy_right');
            $show->field('statistics_js');
            $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
            $show->field('created_at');
            $show->field('updated_at');

            // 按钮
            $show->disableDeleteButton();


        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistAdminDistributor(), function (Form $form) {
            $form->column(6, function (Form $form) {
                $form->radio('domain_type')->width(9,3)
                    ->options([0=>admin_trans_field('secondary_domain'),1=>admin_trans_field('custom_domain')])
                    ->default(0)
                    ->required();
                $form->text('secondary_domain')->width(9,3)
                    ->required()
                    ->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
                $form->text('custom_domain')->width(9,3)->help('Please enter the domain name, such as www.example.com');
                $form->text('site_name')->width(9,3)->required();
                $form->text('seo_title')->width(9,3)->required();
                $form->text('seo_keywords')->width(9,3);
                $form->textarea('seo_description')->rows(2)->width(9,3);
                $form->select('appearance_id',admin_trans_field('appearance'))->width(9,3)->options(DistAppearance::selectOptions())->required();
                $form->cutImage("logo")
                    ->aspectRatio(130/50)// 设置裁剪框比例
                    ->retainable()//禁止删OSS图
                    ->autoUpload()
                    ->uniqueName()
                    ->accept(config('distributor.upload.oss_image.accept'))
                    ->maxSize(config('distributor.upload.oss_image.max_size'))
                    ->dir(config("distributor.upload.directory.image").'/logo')
                    ->width(4,3);
            });
            $form->column(6, function (Form $form) {
                $form->text('company_name')->width(9,3);
                $form->text('company_address',)->width(9,3);
                $form->text('service_hotline')->width(9,3);
                $form->text('whats_app')->width(9,3);
                $form->text('facebook')->width(9,3);
                $form->text('instagram')->width(9,3);
                $form->text('youtube')->width(9,3);
                $form->text('linkedin')->width(9,3);
                $form->text('tiktok')->width(9,3);
                $form->text('copy_right')->width(9,3);
                $form->textarea('statistics_js')->width(9,3);
            });

            //保存前回调
            $form->saving(function (Form $form) {
                if (!$form->isCreating()) {
                    //如果appearance_id有变化,则更新模版与变量
                    if ($form->model()->appearance_id != $form->input('appearance_id')) {
                        $id = $form->model()->id;
                        //更新模版与变量
                        DistAppearance::switchTheme($form->input('appearance_id'), $id);
                    }
                }
            });

            //
            //保存后回调
            $form->saved(function (Form $form, $result) {
                $newId = $form->getKey();
                if ($form->isCreating()) {
                    //创建后创建默认分类
                    $appearanceId = $form->input('appearance_id');
                    //更新模版与变量
                    DistAppearance::switchTheme($appearanceId, $newId);
                }
                //更新缓存
                DistAdminDistributor::clearCache($newId);
            });

            $form->disableDeleteButton();
        });
    }


}