<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DistAdminDistributor;
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('Distro Management')
            ->description('all')
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DistAdminDistributor(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('company_name','Company Name');
            $grid->column('site_name');
            $grid->column('level_domain');
            $grid->column('country');
            $grid->column('contact_number');
            $grid->column('service_hotline');
            $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', 'company name')->width(2);
                $filter->equal('site_name')->width(2);
                $filter->equal('level_domain')->width(2);
                $filter->equal('enabled', '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, new DistAdminDistributor(), function (Show $show) {
            $show->field('id');
            $show->field('company_name','Company Name');
            $show->field('company_address','Company Address');
            $show->field('site_name');
            $show->field('level_domain');
            $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->width(6)->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', 'Company Name')->required();
            $form->text('company_address','Company Address');
            $form->text('site_name')->required();
            $form->text('level_domain')->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->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,
                    ]);
                }
            });
        });

    }
}
