<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\InitAppearance;
use App\Admin\Repositories\BaseIso3166;
use App\Admin\Repositories\DistAdminDistributor;
use App\Admin\Repositories\DistAppearance;
use App\Admin\Repositories\RpcAlbumFolder;
use App\Libraries\CommonHelper;
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;
use Illuminate\Support\Facades\DB;

class DistAdminDistributorController extends AdminController
{
    public function title()
    {
        return admin_trans( 'admin.distro_list');
    }
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->title())
            ->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('client_code'))->width('15%');
            $grid->column('dist_email',admin_trans_label('email'));
            $grid->column('country',);
            $grid->column('enabled')->switch();
            $grid->column('created_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('secondary_domain')->width(2);
                $filter->equal('enabled')->select(admin_trans_array(config('dictionary.enabled')))->width(2);
            });
            // 排序
            $grid->model()->orderBy("id",'desc');
            //按钮
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            //批量操作
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                $batch->disableDelete();
                $batch->add(new InitAppearance());
            });
        });
    }

    /**
     * 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('client_code',admin_trans_label('client_code'));
            $show->field('dist_email',admin_trans_label('email'));
            $show->field('dist_contact_number',admin_trans_label('contact_number'));
            $show->field('country');
            $show->field('country_alpha_2');
            $show->field('country_lang');
            $show->field('address');
            $show->field('created_at');
            $show->field('updated_at');
            $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
            // 按钮
            $show->disableDeleteButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $allNodes = RpcAlbumFolder::allNodes();
        return Form::make(new DistAdminDistributor(), function (Form $form) use ($allNodes) {
            $form->text('client_code',admin_trans_label('client_code'))->width(9,3)->required();
            $form->select('country')
                ->options(BaseIso3166::selectOptions())
                ->width(9,3)->required();
            $form->text('country_lang')->width(9,3)->required()->help(admin_trans_label('country_lang_tips'));
            $form->tree('album_folder',admin_trans_label('album_folder'))
                ->setTitleColumn('title')
                ->nodes($allNodes)
                ->exceptParentNode(false)
                ->width(9,3)
                ->saving(function ($v) {
                    return json_encode($v);
                });

            $form->text('dist_email',admin_trans_label('email'))->width(9,3);
            $form->text('dist_contact_number',admin_trans_label('contact_number'))->width(9,3);
            $form->text('address')->width(9,3);
            $form->switch('enabled')->width(9,3)->default(1);
            $form->hidden('country_alpha_2');
            //保存前回调
            $form->saving(function (Form $form) {
                $form->country_alpha_2 = BaseIso3166::getAlpha2($form->country);
            });
            //保存后回调
            $form->saved(function (Form $form, $result) {
                if ($form->isCreating()) {
                    //创建后创建默认分类
                    $newId = $form->getKey();
                    //初始化分销商
                    DistAdminDistributor::initDist($newId);
                    return $form->response()->success(admin_trans_label('distributor_create_success'));
                }
            });
        });
    }

    /*
     * 新增事件事务
     */
    public function store()
    {
        try {
            DB::beginTransaction();
            $result =  parent::store();
            DB::commit();
            return $result;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }
}
