<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DistInquiry;
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 App\Admin\Actions\Grid\InquiryAssignment;

class DistInquiryController extends AdminController
{
    public function title()
    {
        return admin_trans( 'admin.inquiry_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(DistInquiry::with(['distributor']), function (Grid $grid) {
            //默认分页条数
            $grid->paginate(config('admin.per_page'));
            //指定视图，去掉删除按钮
            $grid->view('admin.grid.table');
            //字段
            $grid->column('id')->display(function () {
                return $this->_index+1;
            });
            $grid->column('order_number')->width('15%');
            $grid->column('customer_name')->width('15%');
            $grid->column('company_name')->width('10%');
            $grid->column('whats_app')->width('10%');;
            $grid->column('consulting_products')->width('10%');
            $grid->column('distributor.client_code', admin_trans_label('distributor_code'))->width('12%');
            $grid->column('status')->using(admin_trans_array(config('dictionary.inquiryStatus')))->label([
                0 => 'default',
                1 => 'info',
                2 => 'success',
            ])->width('10%');
            $grid->column('created_at')->width('10%');
            //$grid->column('updated_at')->sortable();
            //筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('order_number')->width(2);
                $filter->equal('company_name')->width(2);
                $filter->equal('customer_name')->width(2);
                $filter->like('distributor.client_code',admin_trans_label('distributor_code'))->width(2);
                $inquiryStatus = admin_trans_array(config('dictionary.inquiryStatus'));
                $filter->equal('status')->select($inquiryStatus)->width(2);
            });
            //排序
            $grid->model()->orderBy("id",'desc');
            //按钮
            $grid->showQuickEditButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
            $grid->showBatchActions(true);
            //批量操作
            $grid->tools([new InquiryAssignment()]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DistInquiry(), function (Show $show) {
            $show->field('order_number');
            $show->field('customer_name');
            $show->field('company_name');
            $show->field('whats_app');
            $show->field('email');
            $show->field('consulting_products');
            $show->field('freight_forwarder')->using(admin_trans_array(config('dictionary.freight_forwarder')));
            $show->field('business_model');
            $show->field('referer_url');
            $show->field('content')->as(function ($content) {
                return "<pre>{$content}</pre>";
            })->unescape();
            $show->field('ip_address');
            $show->field('status')->using(admin_trans_array(config('dictionary.inquiryStatus')));
            $show->field('created_at');
            $show->field('updated_at');
            $show->divider();
            $show->field('allocate_time');
            $show->field('distributor.distributor_code',admin_trans_label('distributor_code'));
            //按钮
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistInquiry(), function (Form $form) {
            $form->display('order_number');
            $form->text('customer_name');
            $form->text('company_name');
            $form->text('whats_app');
            $form->email('email');
            $form->text('consulting_products');
            $form->select('freight_forwarder')->options(admin_trans_array(config('dictionary.freight_forwarder')));
            $form->text('business_model');
            $form->text('referer_url');
            $form->textarea('content')->rows(10);
        });
    }
}
