<?php
namespace App\Admin\Controllers\Jobfair;
use App\Http\Controllers\Controller;
use App\Models\Jobfair\Floorplan;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;
class FloorplanController extends Controller
{
use HasResourceActions;
/**
* Index interface.
*
* @param Content $content
* @return Content
*/
public function index(Content $content)
{
return $content
->header('展位图列表')
->description('')
->body(view('admin.jobfair.floorplan_index')->with(['grid'=>$this->grid()]));
}
/**
* Show interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public function show($id, Content $content)
{
return $content
->header('展位图详情')
->description('')
->body($this->detail($id));
}
/**
* Edit interface.
*
* @param mixed $id
* @param Content $content
* @return Content
*/
public function edit($id, Content $content)
{
return $content
->header('展位图编辑')
->description('')
->body(view('admin.jobfair.floorplan')->with(['content'=>$this->formEdit($id)]));
}
/**
* Create interface.
*
* @param Content $content
* @return Content
*/
public function create(Content $content)
{
return $content
->header('创建展位图')
->description('')
->body(view('admin.jobfair.floorplan'));
}
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new Floorplan);
$grid->model()->orderBy('id', 'desc');
$grid->name('展位名称')->width(200);
$grid->images('展位平面图')->image('', 100, 70);
$grid->created_at('创建时间');
$grid->updated_at('更新时间');
$grid->disableExport();
$grid->actions(function ($actions) use ($grid) {
if (Admin::user()->can('jobfair_floorplans_edit')) {
$actions->disableEdit(false);
}
if (Admin::user()->can('jobfair_floorplans_delete')) {
$actions->disableDelete(false);
}
});
if (Admin::user()->can('jobfair_floorplans_delete')) {
$grid->tools(function ($tools) {
$tools->batch(function ($batch) {
$batch->disableDelete(false);
});
});
$grid->disableRowSelector(false);
}
$grid->filter(function ($filter) {
$filter->like('name', '展位名称');
});
if (Admin::user()->can('jobfair_floorplans_create')) {
$grid->disableCreateButton(false);
}
return $grid;
}
/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(Floorplan::findOrFail($id));
$show->id('ID');
$show->name('展位名称');
$show->images('展位平面图')->image('', 100, 70);
$show->created_at('创建时间');
$show->updated_at('更新时间');
return $show;
}
public function formEdit($id)
{
return Floorplan::findOrFail($id);
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new Floorplan);
$form->display('ID');
$form->display('创建时间');
$form->display('更新时间');
return $form;
}
public function floors(Request $request)
{
$floorplan_id = $request->floorplan_id;
if (!$request->imgName) {
admin_toastr('展位名称不能为空!', 'error');
return back();
}
$floor_position = json_decode($request->floor_position,true);
$new_equipment_array = array_column($floor_position,'equipment_id');
$floor = Floorplan::pluck('value')->toArray();
$old_equipment_array = [];
foreach ($floor as $value)
{
$floor_arr = json_decode($value,true);
foreach ($floor_arr as $val) {
$old_equipment_array[] = $val['equipment_id'];
}
}
$old_equipment_array = array_unique($old_equipment_array);
foreach ($new_equipment_array as $equipment_id)
{
if(in_array($equipment_id,$old_equipment_array)){
admin_toastr('设备ID '. $equipment_id .' 已存在!', 'error');
return back();
}
}
if ($floorplan_id) {
$Floorplan = Floorplan::findOrFail($floorplan_id);
$Floorplan->name = $request->imgName;
if ($request->has('uploadImg')) {
$Floorplan->images = $request->file('uploadImg')->store('admin/jobfair/images', 'public');
}
$Floorplan->value = $request->floor_position;
} else {
if (!$request->has('uploadImg')) {
admin_toastr('展位图不存在!', 'error');
return back();
}
$Floorplan = new Floorplan();
$Floorplan->name = $request->imgName;
$Floorplan->images = $request->file('uploadImg')->store('admin/jobfair/images', 'public');
$Floorplan->value = $request->floor_position;
}
if ($Floorplan->save()) {
admin_toastr('操作成功', 'success');
} else {
admin_toastr('操作失败', 'error');
}
return redirect(route('floorplans.index'));
}
}