2026-01-16 17:36:37 +08:00
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
|
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
|
|
|
|
|
|
import { Head, router } from '@inertiajs/react';
|
|
|
|
|
|
import { PageProps } from '@/types/global';
|
|
|
|
|
|
import Pagination from '@/Components/shared/Pagination';
|
|
|
|
|
|
import { SearchableSelect } from "@/Components/ui/searchable-select";
|
2026-01-20 14:03:59 +08:00
|
|
|
|
import { FileText, Search, RotateCcw, Calendar, ChevronDown, ChevronUp } from 'lucide-react';
|
2026-01-19 15:32:41 +08:00
|
|
|
|
import LogTable, { Activity } from '@/Components/ActivityLog/LogTable';
|
|
|
|
|
|
import ActivityDetailDialog from '@/Components/ActivityLog/ActivityDetailDialog';
|
2026-01-19 17:07:45 +08:00
|
|
|
|
import { Button } from '@/Components/ui/button';
|
|
|
|
|
|
import { Input } from '@/Components/ui/input';
|
|
|
|
|
|
import { Label } from '@/Components/ui/label';
|
|
|
|
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/Components/ui/select";
|
2026-01-20 14:03:59 +08:00
|
|
|
|
import { getDateRange } from "@/utils/format";
|
2026-01-16 17:36:37 +08:00
|
|
|
|
|
|
|
|
|
|
interface PaginationLinks {
|
|
|
|
|
|
url: string | null;
|
|
|
|
|
|
label: string;
|
|
|
|
|
|
active: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface Props extends PageProps {
|
|
|
|
|
|
activities: {
|
|
|
|
|
|
data: Activity[];
|
|
|
|
|
|
links: PaginationLinks[];
|
|
|
|
|
|
current_page: number;
|
|
|
|
|
|
last_page: number;
|
|
|
|
|
|
total: number;
|
|
|
|
|
|
from: number;
|
|
|
|
|
|
};
|
|
|
|
|
|
filters: {
|
|
|
|
|
|
per_page?: string;
|
2026-01-19 09:30:02 +08:00
|
|
|
|
sort_by?: string;
|
|
|
|
|
|
sort_order?: 'asc' | 'desc';
|
2026-01-19 17:07:45 +08:00
|
|
|
|
search?: string;
|
|
|
|
|
|
date_start?: string;
|
|
|
|
|
|
date_end?: string;
|
|
|
|
|
|
event?: string;
|
|
|
|
|
|
subject_type?: string;
|
|
|
|
|
|
causer_id?: string;
|
2026-01-16 17:36:37 +08:00
|
|
|
|
};
|
2026-01-19 17:07:45 +08:00
|
|
|
|
subject_types: { label: string; value: string }[];
|
|
|
|
|
|
users: { label: string; value: string }[];
|
2026-01-16 17:36:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
export default function ActivityLogIndex({ activities, filters, subject_types, users }: Props) {
|
2026-01-16 17:36:37 +08:00
|
|
|
|
const [perPage, setPerPage] = useState<string>(filters.per_page || "10");
|
|
|
|
|
|
const [selectedActivity, setSelectedActivity] = useState<Activity | null>(null);
|
|
|
|
|
|
const [detailOpen, setDetailOpen] = useState(false);
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
// Filter States
|
|
|
|
|
|
const [search, setSearch] = useState(filters.search || '');
|
|
|
|
|
|
const [dateStart, setDateStart] = useState(filters.date_start || '');
|
|
|
|
|
|
const [dateEnd, setDateEnd] = useState(filters.date_end || '');
|
|
|
|
|
|
const [event, setEvent] = useState(filters.event || 'all');
|
|
|
|
|
|
const [subjectType, setSubjectType] = useState(filters.subject_type || 'all');
|
|
|
|
|
|
const [causer, setCauser] = useState(filters.causer_id || 'all');
|
2026-01-20 14:03:59 +08:00
|
|
|
|
const [dateRangeType, setDateRangeType] = useState('custom');
|
|
|
|
|
|
|
|
|
|
|
|
// Advanced Filter Toggle
|
|
|
|
|
|
const [showAdvancedFilter, setShowAdvancedFilter] = useState(
|
|
|
|
|
|
!!(filters.date_start || filters.date_end)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const handleDateRangeChange = (type: string) => {
|
|
|
|
|
|
setDateRangeType(type);
|
|
|
|
|
|
if (type === 'custom') return;
|
|
|
|
|
|
|
|
|
|
|
|
const { start, end } = getDateRange(type);
|
|
|
|
|
|
setDateStart(start);
|
|
|
|
|
|
setDateEnd(end);
|
|
|
|
|
|
};
|
2026-01-19 17:07:45 +08:00
|
|
|
|
|
|
|
|
|
|
const handleFilter = () => {
|
|
|
|
|
|
router.get(
|
|
|
|
|
|
route('activity-logs.index'),
|
|
|
|
|
|
{
|
|
|
|
|
|
...filters,
|
|
|
|
|
|
search: search,
|
|
|
|
|
|
date_start: dateStart,
|
|
|
|
|
|
date_end: dateEnd,
|
|
|
|
|
|
event: event === 'all' ? undefined : event,
|
|
|
|
|
|
subject_type: subjectType === 'all' ? undefined : subjectType,
|
|
|
|
|
|
causer_id: causer === 'all' ? undefined : causer,
|
|
|
|
|
|
page: 1 // Reset to first page on filter
|
|
|
|
|
|
},
|
|
|
|
|
|
{ preserveState: true, replace: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleReset = () => {
|
|
|
|
|
|
setSearch('');
|
|
|
|
|
|
setDateStart('');
|
|
|
|
|
|
setDateEnd('');
|
|
|
|
|
|
setEvent('all');
|
|
|
|
|
|
setSubjectType('all');
|
|
|
|
|
|
setCauser('all');
|
2026-01-20 14:03:59 +08:00
|
|
|
|
setDateRangeType('custom');
|
2026-01-19 17:07:45 +08:00
|
|
|
|
|
|
|
|
|
|
router.get(
|
|
|
|
|
|
route('activity-logs.index'),
|
|
|
|
|
|
{ per_page: perPage, sort_by: filters.sort_by, sort_order: filters.sort_order },
|
|
|
|
|
|
{ preserveState: true, replace: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-16 17:36:37 +08:00
|
|
|
|
const handleViewDetail = (activity: Activity) => {
|
|
|
|
|
|
setSelectedActivity(activity);
|
|
|
|
|
|
setDetailOpen(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handlePerPageChange = (value: string) => {
|
|
|
|
|
|
setPerPage(value);
|
|
|
|
|
|
router.get(
|
|
|
|
|
|
route('activity-logs.index'),
|
2026-01-19 09:30:02 +08:00
|
|
|
|
{ ...filters, per_page: value },
|
2026-01-16 17:36:37 +08:00
|
|
|
|
{ preserveState: false, replace: true, preserveScroll: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-19 09:30:02 +08:00
|
|
|
|
const handleSort = (field: string) => {
|
|
|
|
|
|
let newSortBy: string | undefined = field;
|
|
|
|
|
|
let newSortOrder: 'asc' | 'desc' | undefined = 'asc';
|
|
|
|
|
|
|
|
|
|
|
|
if (filters.sort_by === field) {
|
|
|
|
|
|
if (filters.sort_order === 'asc') {
|
|
|
|
|
|
newSortOrder = 'desc';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
newSortBy = undefined;
|
|
|
|
|
|
newSortOrder = undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
router.get(
|
|
|
|
|
|
route('activity-logs.index'),
|
|
|
|
|
|
{ ...filters, sort_by: newSortBy, sort_order: newSortOrder },
|
|
|
|
|
|
{ preserveState: true, replace: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-16 17:36:37 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<AuthenticatedLayout
|
|
|
|
|
|
breadcrumbs={[
|
|
|
|
|
|
{ label: '系統管理', href: '#' },
|
|
|
|
|
|
{ label: '操作紀錄', href: route('activity-logs.index'), isPage: true },
|
|
|
|
|
|
]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Head title="操作紀錄" />
|
|
|
|
|
|
|
|
|
|
|
|
<div className="container mx-auto p-6 max-w-7xl">
|
|
|
|
|
|
<div className="flex items-center justify-between mb-6">
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
|
|
|
|
|
|
<FileText className="h-6 w-6 text-primary-main" />
|
|
|
|
|
|
操作紀錄
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
<p className="text-gray-500 mt-1">
|
|
|
|
|
|
檢視系統內的所有操作活動,包含新增、修改與刪除紀錄
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-19 17:07:45 +08:00
|
|
|
|
{/* 篩選區塊 */}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="bg-white p-5 rounded-lg shadow-sm border border-grey-4 mb-6">
|
|
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-12 gap-4 mb-4">
|
2026-01-19 17:07:45 +08:00
|
|
|
|
{/* 關鍵字搜尋 */}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="md:col-span-4 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">關鍵字搜尋</Label>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<div className="relative">
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400" />
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Input
|
|
|
|
|
|
placeholder="搜尋描述、內容..."
|
|
|
|
|
|
value={search}
|
|
|
|
|
|
onChange={(e) => setSearch(e.target.value)}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
className="pl-10 h-9 block"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
onKeyDown={(e) => e.key === 'Enter' && handleFilter()}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 事件類型 */}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="md:col-span-2 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">事件類型</Label>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Select value={event} onValueChange={setEvent}>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<SelectTrigger className="h-9">
|
|
|
|
|
|
<SelectValue placeholder="所有事件" />
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</SelectTrigger>
|
|
|
|
|
|
<SelectContent>
|
|
|
|
|
|
<SelectItem value="all">所有事件</SelectItem>
|
|
|
|
|
|
<SelectItem value="created">新增 (Created)</SelectItem>
|
|
|
|
|
|
<SelectItem value="updated">更新 (Updated)</SelectItem>
|
|
|
|
|
|
<SelectItem value="deleted">刪除 (Deleted)</SelectItem>
|
|
|
|
|
|
</SelectContent>
|
|
|
|
|
|
</Select>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 操作對象 */}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="md:col-span-3 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">操作對象</Label>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<SearchableSelect
|
|
|
|
|
|
value={subjectType}
|
|
|
|
|
|
onValueChange={setSubjectType}
|
|
|
|
|
|
options={[
|
|
|
|
|
|
{ label: "所有對象", value: "all" },
|
|
|
|
|
|
...subject_types
|
|
|
|
|
|
]}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
placeholder="選擇對象"
|
|
|
|
|
|
className="w-full h-9"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
{/* 操作人員 */}
|
|
|
|
|
|
<div className="md:col-span-3 space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">操作人員</Label>
|
|
|
|
|
|
<SearchableSelect
|
|
|
|
|
|
value={causer}
|
|
|
|
|
|
onValueChange={setCauser}
|
|
|
|
|
|
options={[
|
|
|
|
|
|
{ label: "所有人員", value: "all" },
|
|
|
|
|
|
...users
|
|
|
|
|
|
]}
|
|
|
|
|
|
placeholder="選擇人員"
|
|
|
|
|
|
className="w-full h-9"
|
|
|
|
|
|
/>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</div>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
</div>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
{/* Row 2: Date Filters (Collapsible) */}
|
|
|
|
|
|
{showAdvancedFilter && (
|
|
|
|
|
|
<div className="grid grid-cols-1 md:grid-cols-12 gap-4 items-end animate-in fade-in slide-in-from-top-2 duration-200">
|
|
|
|
|
|
<div className="md:col-span-6 space-y-2">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-1">快速時間區間</Label>
|
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
{[
|
|
|
|
|
|
{ label: "今日", value: "today" },
|
|
|
|
|
|
{ label: "昨日", value: "yesterday" },
|
|
|
|
|
|
{ label: "本週", value: "this_week" },
|
|
|
|
|
|
{ label: "本月", value: "this_month" },
|
|
|
|
|
|
{ label: "上月", value: "last_month" },
|
|
|
|
|
|
].map((opt) => (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
key={opt.value}
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => handleDateRangeChange(opt.value)}
|
|
|
|
|
|
className={
|
|
|
|
|
|
dateRangeType === opt.value
|
|
|
|
|
|
? 'button-filled-primary h-9 px-4 shadow-sm'
|
|
|
|
|
|
: 'button-outlined-primary h-9 px-4 bg-white'
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
{opt.label}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="md:col-span-6">
|
|
|
|
|
|
<div className="grid grid-cols-2 gap-4 items-end">
|
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-2">開始日期</Label>
|
|
|
|
|
|
<div className="relative">
|
|
|
|
|
|
<Calendar className="absolute left-2.5 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400 pointer-events-none" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
type="date"
|
|
|
|
|
|
value={dateStart}
|
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
setDateStart(e.target.value);
|
|
|
|
|
|
setDateRangeType('custom');
|
|
|
|
|
|
}}
|
|
|
|
|
|
// block w-full to ensure it fills space
|
|
|
|
|
|
className="pl-9 block w-full h-9 bg-white"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
|
<Label className="text-xs font-medium text-grey-2">結束日期</Label>
|
|
|
|
|
|
<div className="relative">
|
|
|
|
|
|
<Calendar className="absolute left-2.5 top-1/2 transform -translate-y-1/2 h-4 w-4 text-gray-400 pointer-events-none" />
|
|
|
|
|
|
<Input
|
|
|
|
|
|
type="date"
|
|
|
|
|
|
value={dateEnd}
|
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
|
setDateEnd(e.target.value);
|
|
|
|
|
|
setDateRangeType('custom');
|
|
|
|
|
|
}}
|
|
|
|
|
|
className="pl-9 block w-full h-9 bg-white"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
)}
|
2026-01-19 17:07:45 +08:00
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
{/* Action Bar */}
|
|
|
|
|
|
<div className="flex items-center justify-end border-t border-grey-4 pt-5 gap-3 mt-4">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => setShowAdvancedFilter(!showAdvancedFilter)}
|
|
|
|
|
|
className="mr-auto text-gray-500 hover:text-gray-900 h-9"
|
|
|
|
|
|
>
|
|
|
|
|
|
{showAdvancedFilter ? (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<ChevronUp className="h-4 w-4 mr-1" />
|
|
|
|
|
|
收合篩選
|
|
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<ChevronDown className="h-4 w-4 mr-1" />
|
|
|
|
|
|
進階篩選
|
|
|
|
|
|
{(dateStart || dateEnd) && (
|
|
|
|
|
|
<span className="ml-2 w-2 h-2 rounded-full bg-primary-main" />
|
|
|
|
|
|
)}
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Button>
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={handleReset}
|
2026-01-20 14:03:59 +08:00
|
|
|
|
className="flex items-center gap-2 button-outlined-primary h-9"
|
2026-01-19 17:07:45 +08:00
|
|
|
|
>
|
|
|
|
|
|
<RotateCcw className="h-4 w-4" />
|
2026-01-20 14:03:59 +08:00
|
|
|
|
重置
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</Button>
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<Button onClick={handleFilter} className="button-filled-primary h-9 px-6 gap-2">
|
2026-01-19 17:07:45 +08:00
|
|
|
|
<Search className="h-4 w-4" />
|
2026-01-20 14:03:59 +08:00
|
|
|
|
查詢
|
2026-01-19 17:07:45 +08:00
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-19 15:32:41 +08:00
|
|
|
|
<LogTable
|
|
|
|
|
|
activities={activities.data}
|
|
|
|
|
|
sortField={filters.sort_by}
|
|
|
|
|
|
sortOrder={filters.sort_order}
|
|
|
|
|
|
onSort={handleSort}
|
|
|
|
|
|
onViewDetail={handleViewDetail}
|
|
|
|
|
|
from={activities.from}
|
|
|
|
|
|
/>
|
2026-01-16 17:36:37 +08:00
|
|
|
|
|
2026-01-20 14:03:59 +08:00
|
|
|
|
<div className="mt-6">
|
|
|
|
|
|
<Pagination
|
|
|
|
|
|
links={activities.links}
|
|
|
|
|
|
total={activities.total}
|
|
|
|
|
|
current_page={activities.current_page}
|
|
|
|
|
|
last_page={activities.last_page}
|
|
|
|
|
|
per_page={perPage}
|
|
|
|
|
|
onPerPageChange={handlePerPageChange}
|
|
|
|
|
|
/>
|
2026-01-16 17:36:37 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<ActivityDetailDialog
|
2026-01-20 14:03:59 +08:00
|
|
|
|
isOpen={detailOpen}
|
|
|
|
|
|
onClose={() => setDetailOpen(false)}
|
2026-01-16 17:36:37 +08:00
|
|
|
|
activity={selectedActivity}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</AuthenticatedLayout>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|