<?php
namespace App\Admin\Controllers;
use App\Company;
use App\Facades\MessageFacade;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\User;
use Illuminate\Support\Facades\DB;
class ManageCompanyController extends Controller
{
use HasResourceActions;
public function index(Content $content)
{
return $content
->header('企业用户')
->description('管理')
->body($this->grid());
}
public function show($id, Content $content)
{
return $content
->header('企业用户')
->description('详情')
->body($this->detail($id));
}
public function edit($id, Content $content)
{
return $content
->header('编辑')
->description('企业用户')
->body($this->form()->edit($id));
}
public function create(Content $content)
{
return $content
->header('创建')
->description('企业用户')
->body($this->form());
}
protected function grid()
{
$grid = new Grid(new Company);
$grid->model()->where('type', 2);
$grid->name('名称');
$grid->user()->name('管理员')->label();
$grid->service_user()->name('法律顾问')->label();
$grid->channel_company()->name('渠道商')->label();
$grid->logo('logo')->image('',30,30);
$grid->address('地址');
$grid->mail('邮箱');
$grid->contact_name('联系人');
$grid->contact_phone('联系电话');
$grid->registration_number('注册号');
$grid->registration_amount('注册资金');
$grid->created_at('创建时间');
$grid->updated_at('更新时间');
return $grid;
}
protected function detail($id)
{
$show = new Show(Company::findOrFail($id));
$show->type('类型');
$show->name('名称');
$show->logo('logo')->image();
$show->address('地址');
$show->mail('邮箱');
$show->contact_name('联系人');
$show->contact_phone('联系电话');
$show->registration_number('注册号');
$show->registration_amount('注册资金');
$show->user('管理员', function ($user){
$user->setResource('admin/user');
$user->name('姓名');
});
$show->channel_id('所属渠道商');
$show->created_at('创建日期');
$show->updated_at('更新日期');
return $show;
}
protected function form()
{
$form = new Form(new Company);
$form->text('name', '名称')->rules('required');
$form->image('logo', 'logo');
$form->text('address', '地址')->rules('required');
$users = User::whereHas('roles', function($query) {
$query->where('slug', 'CustomerService');
})->get();
$form->select('service_user_id', '渠道顾问')->options($users->pluck('name', 'id'))->rules('required');
$form->email('mail', '邮箱')->rules('required');
$form->text('contact_name', '联系人')->rules('required');
$form->mobile('contact_phone', '联系电话')->options(['mask' => '99999999999'])->rules('required');
$form->text('registration_number', '注册号')->rules('required');
$form->text('registration_amount', '注册资金')->rules('required');
$form->saved(function (Form $form) {
if (empty($form->model()->admin_user_id)) { $user = new User();
$user->username = $form->contact_phone;
$user->name = $form->contact_name;
$user->password = bcrypt($form->contact_phone);
$user->avatar = $form->model()->logo;
$user->company_id = $form->model()->id;
$user->save();
Db::table('admin_role_users')->insert(['role_id' => 3,'user_id' => $user->id]);
$form->model()->admin_user_id = $user->id;
$form->model()->type =2 ;
$form->model()->save();
MessageFacade::sendAgentRegisterNotify($form->contact_phone);
}
DB::table('admin_users')->where('company_id', $form->model()->id)->update(['service_user_id'=> $form->service_user_id]);
});
return $form;
}
}