store-manage/app/Admin/Controllers/Hr/EmployeeController.php

98 lines
4.5 KiB
PHP

<?php
namespace App\Admin\Controllers\Hr;
use App\Admin\Services\EmployeeService;
use App\Enums\EmployeeStatus;
use App\Models\Employee;
use Illuminate\Http\Request;
use Slowlyo\OwlAdmin\Controllers\AdminController;
use Slowlyo\OwlAdmin\Renderers\Form;
use Slowlyo\OwlAdmin\Renderers\Page;
class EmployeeController extends AdminController
{
protected string $serviceName = EmployeeService::class;
public function list(): Page
{
$crud = $this->baseCRUD()
->tableLayout('fixed')
->headerToolbar([
$this->createButton(true),
...$this->baseHeaderToolBar(),
])
->filter($this->baseFilter()->body([
amis()->GroupControl()->mode('horizontal')->body([
amisMake()->TextControl()->name('name')->label(__('employee.name'))->columnRatio(3)->clearable(),
amisMake()->TextControl()->name('phone')->label(__('employee.phone'))->columnRatio(3)->clearable(),
amisMake()->SelectControl()->name('employee_status')->label(__('employee.employee_status'))->columnRatio(3)->clearable()->options(EmployeeStatus::options()),
]),
]))
->columns([
amisMake()->TableColumn()->name('id')->label(__('employee.id')),
amisMake()->TableColumn()->name('name')->label(__('employee.name')),
amisMake()->TableColumn()->name('phone')->label(__('employee.phone')),
amisMake()->TableColumn()->name('employee_status_text')->label(__('employee.employee_status'))->set('type', 'tag')->set('color', '${employee_status_color}'),
amisMake()->TableColumn()->name('created_at')->label(__('employee.created_at')),
$this->rowActions([
$this->rowShowButton(),
$this->rowEditButton(true),
$this->rowDeleteButton(),
amisMake()->AjaxAction()
->label(__('employee.leave'))
->level('link')
->icon('fa fa-sign-out')
->confirmText(__('employee.leave_confirm'))
->api('post:' . admin_url('hr/employees/${id}/leave')),
]),
]);
return $this->baseList($crud);
}
public function form($edit): Form
{
return $this->baseForm()->title('')->body([
amisMake()->TextControl()->name('name')->label(__('employee.name'))->required(),
amisMake()->TextControl()->name('phone')->label(__('employee.phone'))->required(),
amisMake()->TagControl()->name('jobs')->label(__('employee.jobs'))
->source(admin_url('api/keywords/tree-list') . '?parent_key=' . Employee::JOB_KEY)
->labelField('name')
->valueField('key')
->joinValues(),
amisMake()->DateControl()->name('join_at')->label(__('employee.join_at'))->format('YYYY-MM-DD'),
amisMake()->TextControl()->name('username')->label(__('admin.username'))->value('${admin_user.username}')->required(!$edit),
amisMake()->TextControl()->name('password')->set('type', 'input-password')->label(__('admin.password'))->required(!$edit),
amisMake()->TextControl()->name('confirm_password')->set('type', 'input-password')->label(__('admin.confirm_password'))->required(!$edit),
]);
}
public function detail(): Form
{
return $this->baseDetail()->title('')->body(amisMake()->Property()->items([
['label' => __('employee.name'), 'content' => '${name}'],
['label' => __('employee.phone'), 'content' => '${phone}'],
['label' => __('employee.jobs'), 'content' => amisMake()->Each()->name('jobs')->items(amisMake()->Tag()->label('${name}'))],
['label' => __('admin.username'), 'content' => '${admin_user.username}'],
['label' => __('employee.employee_status'), 'content' => amisMake()->Tag()->label('${employee_status_text}')->color('${employee_status_color}')],
['label' => __('employee.join_at'), 'content' => '${join_at}'],
['label' => __('employee.leave_at'), 'content' => '${leave_at}'],
]));
}
// 员工离职
public function leave($id, Request $request)
{
$user = Employee::findOrFail($id);
$user->update([
'leave_at' => $request->input('leave_at', now()),
'employee_status' => EmployeeStatus::Offline,
]);
return $this->response()->success(null, '操作成功');
}
}