<?php
namespace App\Admin\Controllers;
use App\Company;
use App\CreditLog;
use App\Facades\CommonFacade;
use App\Facades\MessageFacade;
use App\Http\Controllers\Controller;
use App\UserCombo;
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\Http\Request;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\MessageBag;
class ChannelCustomerController extends Controller
{
use HasResourceActions;
public function index(Content $content)
{
return $content
->header('客户')
->description('管理')
->body($this->grid())
->body(view('modal/confirm'));
}
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->disableCreateButton();
$grid->model()->where('channel_id', CommonFacade::getCompanyId())->orderBy('created_at', 'desc');
$grid->logo('logo')->image('',30,30);
$grid->name('名称');
$grid->user()->name('管理员')->label();
$grid->actions(function ($actions) {
$actions->disableDelete();
$actions->disableEdit();
$actions->disableView();
$end_at = strtotime($this->row->user->combo->end_at);
if ($end_at - time() < 1296000 && !empty($end_at)) {
$url = url('admin/channel/customer/renew?id='.$actions->getKey());
$actions->append("<a href='#' class='confirm-modal-item' data-title='临期续费' data-content='为该用户续费' data-url='$url'>临期续费</a>");
}
});
$grid->tools(function ($tools) {
$tools->batch(function ($batch) {
$batch->disableDelete();
});
});
$grid->service_user()->name('法律顾问')->label();
$grid->contact_phone('联系电话');
$grid->address('地址');
$grid->mail('邮箱');
$grid->contact_name('联系人');
$grid->registration_number('注册号');
$grid->registration_amount('注册资金');
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');
$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->saving(function (Form $form) {
if ($form->model()->id === null && !empty(User::where('username',$form->contact_phone)->first()) && $form->contact_phone != $form->model()->username) {
$error = new MessageBag([
'title' => '错误',
'message' => '该联系人已被占用,请更换',
]);
return back()->with(compact('error'));
}
if ($form->model()->id !== null) {
if( $form->model()->channel_id != CommonFacade::getCompanyId()) {
$error = new MessageBag([
'title' => '错误',
'message' => '该客户不属于您!',
]);
return back()->with(compact('error'));
}
}
});
$form->saved(function (Form $form) {
if (empty($form->model()->service_user_id)) {
$users = User::whereHas('roles', function($query) {
$query->where('slug', 'CustomerService');
})->get();
if (count($users) > 0) {
$form->model()->service_user_id = $users[0]->id;
}
$form->model()->save();
}
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;
$users = User::whereHas('roles', function($query) {
$query->where('slug', 'CustomerService');
})->get();
if (count($users) > 0) {
$user->service_user_id = $users[0]->id;
$form->model()->service_user_id = $users[0]->id;
}
$user->save();
$role_id = 3;
Db::table('admin_role_users')->insert(['role_id' => $role_id,'user_id' => $user->id]);
$form->model()->admin_user_id = $user->id;
if(Admin::user()->isRole('ServiceProviderAdmin')) { $company = Company::where('admin_user_id', Admin::user()->id)->first();
$form->model()->channel_id = $company->id;
}
$form->model()->type = 2;
$form->model()->save();
MessageFacade::sendAgentRegisterNotify($form->contact_phone);
}
});
return $form;
}
public function renew(Request $request){
$user = Company::find($request->id)->user;
$combo = $user->combo;
if(empty($combo->id)){
return ['code' => 400, 'msg'=>'该用户没有可续费套餐'];
}
if (empty($user)) {
return ['code'=>404, 'msg' => '用户不存在'];
}
if (Admin::user()->credit < $combo->price) {
return ['code'=>400, 'msg' => '余额不足'];
}
$combo->end_at = date("Y-m-d H:i:s", strtotime("+1 year", strtotime($combo->end_at)));
$combo->save();
$admin_user = Admin::user();
$log = new CreditLog();
$log->user_id =$admin_user->id;
$log->operator_id = Admin::user()->id;
$log->price = $combo->price;
$log->type = "-";
$log->reason = '为用户'.$user->username.'临期续费';
$log->save();
return ['code'=>200, 'msg' => '操作成功'];
}
}