<?php

namespace App\Distributor\Controllers;

use App\Admin\Actions\Grid\InquiryAssignment;
use App\Admin\Actions\Tools\InquiryHandle;
use App\Distributor\Repositories\DistInquiry;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;

class DistInquiryController extends AdminDistController
{
    /**
     * page index
     */
    public function index(Content $content)
    {
        return $content
            ->header(admin_trans('admin.inquiry_management'))
            ->description('')
            ->breadcrumb(['text'=>'list','url'=>''])
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $status = isset($_GET['status']) ? intval($_GET['status']) : -1;
        return Grid::make(DistInquiry::with(['distributor']), function (Grid $grid) use ($status) {
            //默认分页条数
            $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('15%');
            $grid->column('email');
            $grid->column('consulting_products');
            $grid->column('status')->using(admin_trans_array(config('dictionary.inquiryStatus')))->label([
                0 => 'default',
                1 => 'info',
                2 => 'success',
            ]);
            //筛选
            $grid->filter(function (Grid\Filter $filter) {
                $inquiryStatus = admin_trans_array(config('dictionary.inquiryStatus'));
                unset($inquiryStatus[0]);
                $filter->panel();
                $filter->expand();
                $filter->equal('order_number')->width(2);
                $filter->equal('company_name')->width(2);
                $filter->equal('customer_name')->width(2);
                $filter->equal('status')->select($inquiryStatus)->width(2);
            });
            //按钮
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableBatchDelete();
            $grid->showViewButton();
          //  if ($status == 1) {
                //批量操作
                $grid->tools([new InquiryHandle()]);
          //  }
            //权限与排序
            $grid->model()->where('dist_id', getDistributorId())->where('status','>',0)->orderBy("id",'desc');
        });
    }

    /**
     * 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('content')->as(function ($content) {
                return "<pre>{$content}</pre>";
            })->unescape();
            $show->field('status')->using(config('dictionary.inquiryStatus'));
            //按钮
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

}
