<?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
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header('Inquiry Management')
            ->description('all')
            ->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->view('admin.grid.table');
            //字段
            $grid->column('id')->sortable();
            $grid->column('order_number');
            $grid->column('company_name');
            $grid->column('customer_name');
            $grid->column('email');
            $grid->column('consulting_products');
            //$grid->column('distributor.username', 'Distributor Username');
            $grid->column('distributor.company_name', 'Distributor Company Name');
            $grid->column('status')->using(config('dictionary.inquiryStatus'))->label([
                0 => 'default',
                1 => 'info',
                2 => 'success',
            ])->filter(Grid\Column\Filter\In::make(config('dictionary.inquiryStatus')));
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            //筛选
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('order_noumber')->width(2);
                $filter->equal('company_name')->width(2);
                $filter->equal('customer_name')->width(2);
                $filter->like('distributor.name','Distributor Company Name')->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('id');
            $show->field('order_number');
            $show->field('whats_app');
            $show->field('company_name');
            $show->field('customer_name');
            $show->field('email');
            $show->field('consulting_products');
            $show->field('freight_forwarder');
            $show->field('business_model');
            $show->field('ip_address');
            $show->field('status')->using(config('dictionary.inquiryStatus'));
            $show->field('allocate_time');
            $show->field('distributor.company_name','Distributor Company Name');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
            //按钮
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DistInquiry(), function (Form $form) {
            $form->display('id');
            $form->display('order_number');
            $form->text('whats_app');
            $form->text('company_name');
            $form->text('customer_name');
            $form->email('email');
            $form->text('consulting_products');
            $form->text('freight_forwarder');
            $form->text('business_model');
            $form->ip('ip_address');
            $form->textarea('content');
        });
    }
}
