<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DistAdminDistributor;
use App\Admin\Repositories\DistAppearance;
use App\Models\DistProductCategory;
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 DistAdminDistributorController extends AdminController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans( 'admin.distro_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->column('id')->sortable();
            $grid->column('company_name');
            $grid->column('site_name');
            $grid->column('domain_type',admin_trans_label('domain'))->display(function ($domainType) {
                $title = "";
                if ($domainType == 0) {
                    $title = $this->secondary_domain;
                } else {
                    $title = $this->custom_domain;
                }
                return "<span style='color:#586cb1'>$title</span>";
            });

            $grid->column('country',);
            $grid->column('appearance.title' ,admin_trans_field('appearance'));
            $grid->column('enabled')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // 过滤
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('company_name', )->width(2);
                $filter->equal('site_name')->width(2);
                $filter->equal('domain')->width(2);
                $filter->equal('enabled', )->select(config('dictionary.enabled'))->width(2);
            });
            // 排序
            $grid->model()->orderBy("id",'desc');
            //按钮
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
        });
    }

    /**
     * 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('id');
            $show->field('company_name',);
            $show->field('company_address');
            $show->field('site_name');
            $show->field('domain_type',admin_trans_label('domain'))->as(function ($domainType) {
                $title = "";
                if ($domainType == 0) {
                    $title = $this->secondary_domain;
                } else {
                    $title = $this->custom_domain;
                }
                return "<span style='color:#586cb1'>$title</span>";
            })->unescape();
            $show->field('appearance.title' ,admin_trans_field('appearance'));
            $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('remark');
            $show->field('enabled')->using(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->display('id');
            $form->text('company_name', )->required();
            $form->text('company_address',);
            $form->text('site_name')->required();

            $form->radio('domain_type')
                ->when(0, function (Form $form) {
                    $form->text('secondary_domain')->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
                })
                ->when(1, function (Form $form) {
                    $form->text('custom_domain')->help('Please enter the domain name, such as www.example.com,and bind the domain name to the IP address '.env('DIST_SITE_IP'));
                })
                ->default(0)
                ->options([0=>'second-level domain',1=>'custom domain'])
                ->required();


            $form->select('appearance_id',admin_trans_field('appearance'))->options(DistAppearance::selectOptions())->required();
            $form->text('country')->required();
            $form->text('contact_number');
            $form->text('service_hotline');
            $form->text('whats_app');
            $form->text('facebook');
            $form->text('instagram');
            $form->text('youtube');
            $form->text('linkedin');
            $form->text('tiktok');
            $form->textarea('remark');
            $form->switch('enabled')->default(1);
            //保存前回调
            $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) {
                if ($form->isCreating()) {
                    $newId = $form->getKey();
                    $distProductCategory = new DistProductCategory();
                    $distProductCategory->create([
                        'parent_id' => 0,
                        'name' => 'Default Category',
                        'order' => 0,
                        'enabled' => 1,
                        'dist_id'=>$newId,
                    ]);
                    //更新模版与变量
                    DistAppearance::switchTheme($form->input('appearance_id'), $newId);
                }
            });
        });
    }
}
