jasan/models.php
2026-02-08 18:10:54 +09:00

594 lines
No EOL
35 KiB
PHP

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DB 관리 | FKI ASSET</title>
<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Pretendard:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/style.css">
<style>
body {
font-family: 'Pretendard', sans-serif;
}
[x-cloak] {
display: none !important;
}
.modal-bg {
background-color: rgba(15, 23, 42, 0.7);
backdrop-filter: blur(4px);
}
</style>
</head>
<body class="bg-[#f8fafc] text-slate-800" x-data="dbManagement()">
<?php include 'nav.php'; ?>
<main class="max-w-[1600px] mx-auto p-8 pt-10">
<header class="mb-10 flex flex-col md:flex-row md:items-center justify-between gap-4">
<div>
<h2 class="text-3xl font-extrabold text-slate-900 tracking-tight">DB 마스터 관리</h2>
<p class="text-slate-500 mt-1">시스템 운영을 위한 기초 자산 모델 및 조직 정보를 관리합니다.</p>
</div>
<div class="flex gap-3">
<button x-show="currentTab === 'models'" @click="openAddModal()"
class="bg-blue-600 text-white px-5 py-2.5 rounded-xl font-bold shadow-lg shadow-blue-200 hover:bg-blue-700 transition-all">신규
모델 등록</button>
<button x-show="currentTab === 'depts'" @click="openAddDeptModal()"
class="bg-blue-600 text-white px-5 py-2.5 rounded-xl font-bold shadow-lg shadow-blue-200 hover:bg-blue-700 transition-all">신규
부서 생성</button>
</div>
</header>
<!-- Tabs -->
<div class="flex items-center space-x-2 mb-8 bg-slate-100 p-1.5 rounded-2xl w-fit">
<button @click="currentTab = 'models'"
:class="currentTab === 'models' ? 'bg-white shadow-sm text-blue-600' : 'text-slate-500 hover:text-slate-700'"
class="px-6 py-2 rounded-xl text-sm font-black transition-all">노트북 모델</button>
<button @click="currentTab = 'depts'"
:class="currentTab === 'depts' ? 'bg-white shadow-sm text-blue-600' : 'text-slate-500 hover:text-slate-700'"
class="px-6 py-2 rounded-xl text-sm font-black transition-all">부서 관리</button>
</div>
<!-- Tab 1: Laptop Models -->
<div x-show="currentTab === 'models'" x-transition>
<!-- Bulk Actions -->
<div x-show="selectedIds.length > 0" x-transition x-cloak
class="mb-6 bg-slate-900 text-white px-6 py-3 rounded-2xl flex items-center justify-between shadow-xl">
<div class="flex items-center gap-4">
<div class="flex items-center gap-2">
<span class="text-blue-400 font-black" x-text="selectedIds.length"></span>
<span class="text-xs font-bold text-slate-400">개 선택됨</span>
</div>
<div class="h-4 w-px bg-slate-700"></div>
<div class="flex items-center gap-3">
<span class="text-xs font-bold text-slate-400 uppercase">일괄 변경:</span>
<input type="text" x-model="bulkManufacturer" placeholder="제조사 일괄 변경..."
class="bg-slate-800 border-none rounded-lg text-xs font-bold px-3 py-1.5 focus:ring-1 focus:ring-blue-500 w-40 text-white">
<input type="text" x-model="bulkProductName" placeholder="품명 일괄 변경..."
class="bg-slate-800 border-none rounded-lg text-xs font-bold px-3 py-1.5 focus:ring-1 focus:ring-blue-500 w-40 text-white">
<button @click="applyBulkUpdate"
class="bg-blue-500 hover:bg-blue-600 px-4 py-1.5 rounded-lg text-xs font-black transition-all">적용</button>
</div>
</div>
<button @click="selectedIds = []" class="text-slate-400 hover:text-white transition-colors">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<!-- Model List Table -->
<div class="bg-white rounded-3xl shadow-sm border border-slate-200 overflow-x-auto no-scrollbar">
<table class="min-w-full divide-y divide-slate-100">
<thead class="bg-slate-50/50">
<tr>
<th class="px-6 py-4 text-left w-10">
<input type="checkbox" @change="toggleSelectAll($event.target.checked)"
class="w-4 h-4 rounded border-slate-300 text-blue-600 focus:ring-blue-500">
</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider">
취득년도</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider">
취득일</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider">
제조사</th>
<th
class="px-6 py-4 text-left text-[10px] font-bold text-slate-500 uppercase tracking-wider">
모델명</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider font-mono">
CPU</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider font-mono">
RAM</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider font-mono text-blue-500">
HDD 0</th>
<th
class="px-4 py-4 text-left text-[10px] font-bold text-slate-400 uppercase tracking-wider">
정격입출력</th>
<th
class="px-6 py-4 text-right text-[10px] font-bold text-slate-500 uppercase tracking-wider whitespace-nowrap">
관리</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-50">
<template x-for="model in models" :key="model.id">
<tr class="hover:bg-slate-50/80 transition-colors group">
<td class="px-6 py-4">
<input type="checkbox" :value="model.id" x-model="selectedIds"
class="w-4 h-4 rounded border-slate-300 text-blue-600 focus:ring-blue-500">
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer text-xs font-medium text-slate-500"
@click="editModel(model)">
<span
x-text="model.purchase_date ? model.purchase_date.substring(0,4) : '-'"></span>
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer text-[11px] text-slate-400"
@click="editModel(model)">
<span x-text="model.purchase_date || '-'"></span>
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer" @click="editModel(model)">
<span
class="px-2 py-0.5 bg-slate-100 text-slate-600 rounded text-[10px] font-black uppercase tracking-widest"
x-text="model.manufacturer"></span>
</td>
<td class="px-6 py-4 whitespace-nowrap cursor-pointer" @click="editModel(model)">
<span class="text-sm font-bold text-slate-800" x-text="model.model_name"></span>
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer text-xs font-mono text-slate-500"
@click="editModel(model)">
<span x-text="model.cpu || '-'"></span>
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer text-xs font-mono text-slate-600 font-bold"
@click="editModel(model)">
<span x-text="model.ram || '-'"></span>
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer text-xs font-mono text-blue-600 font-bold"
@click="editModel(model)">
<span x-text="model.hdd0_capacity || '-'"></span>
</td>
<td class="px-4 py-4 whitespace-nowrap cursor-pointer text-[10px] text-slate-400"
@click="editModel(model)">
<span x-text="model.power_rating || '-'"></span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-right">
<button @click="editModel(model)"
class="text-[11px] font-black text-blue-600 hover:underline px-2 py-1 rounded-lg hover:bg-blue-50 transition-all uppercase">EDIT</button>
</td>
</tr>
</template>
</tbody>
</table>
</div>
</div>
<!-- Tab 2: Departments -->
<div x-show="currentTab === 'depts'" x-transition>
<div class="bg-white rounded-3xl shadow-sm border border-slate-200 overflow-hidden">
<table class="min-w-full divide-y divide-slate-100">
<thead class="bg-slate-50/50">
<tr>
<th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">
부서명 (계층구조)</th>
<th class="px-6 py-4 text-left text-xs font-bold text-slate-500 uppercase tracking-wider">
인원수</th>
<th class="px-6 py-4 text-right text-xs font-bold text-slate-500 uppercase tracking-wider">
관리</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-50">
<template x-for="dept in depts" :key="dept.id">
<tr class="hover:bg-slate-50/80 transition-colors group">
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center">
<div :style="'margin-left: ' + (getDeptLevel(dept.path) * 20) + 'px'"
class="flex items-center">
<div class="w-2 h-2 rounded-full mr-3"
:class="getDeptLevel(dept.path) === 0 ? 'bg-blue-600' : 'bg-slate-300'">
</div>
<span class="text-sm font-bold text-slate-900" x-text="dept.name"></span>
<span class="ml-2 text-[10px] text-slate-400 font-medium"
x-show="getDeptLevel(dept.path) > 0" x-text="dept.path"></span>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<button @click="viewDeptMembers(dept)"
class="text-sm font-black text-blue-600 hover:underline">
<span x-text="dept.member_count || 0"></span>명 확인
</button>
</td>
<td class="px-6 py-4 whitespace-nowrap text-right">
<button @click="editDept(dept)"
class="text-xs font-bold text-slate-400 hover:text-blue-600 px-3 py-1 rounded-lg hover:bg-blue-50 transition-all">수정</button>
</td>
</tr>
</template>
</tbody>
</table>
</div>
</div>
</main>
<!-- Model Modal -->
<div x-show="showModal" x-cloak class="fixed inset-0 z-[110] flex items-center justify-center p-4">
<div class="fixed inset-0 modal-bg" @click="showModal = false"></div>
<div class="bg-white rounded-3xl p-8 max-w-4xl w-full relative z-[111] shadow-2xl max-h-[90vh] overflow-y-auto">
<h3 class="text-2xl font-black mb-6" x-text="isEdit ? '모델 정보 수정' : '신규 모델 등록'"></h3>
<form @submit.prevent="submitModel" class="space-y-6">
<!-- 기본 정보 -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">제조사</label>
<input type="text" x-model="formData.manufacturer" required placeholder="예: SAMSUNG"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">모델명</label>
<input type="text" x-model="formData.model_name" required placeholder="예: NT750XEV"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">품명</label>
<input type="text" x-model="formData.product_name" placeholder="예: 노트북"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
</div>
<!-- 핵심 사양 -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">CPU (프로세서)</label>
<input type="text" x-model="formData.cpu" placeholder="예: i5-1135G7"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">NPU (AI Boost)</label>
<input type="text" x-model="formData.npu" placeholder="예: Intel AI Boost"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">RAM</label>
<input type="text" x-model="formData.ram" placeholder="예: 16GB"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
</div>
<!-- 저장 장치 -->
<div class="grid grid-cols-1 md:grid-cols-2 gap-4 bg-slate-50 p-4 rounded-2xl">
<div class="space-y-4">
<label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest">Storage 0
(Main)</label>
<div class="grid grid-cols-2 gap-2">
<input type="text" x-model="formData.hdd0_model" placeholder="타입/모델"
class="w-full px-4 py-2 bg-white border border-slate-200 rounded-lg text-sm outline-none">
<input type="text" x-model="formData.hdd0_capacity" placeholder="용량 (예: 512GB)"
class="w-full px-4 py-2 bg-white border border-slate-200 rounded-lg text-sm outline-none">
</div>
</div>
<div class="space-y-4">
<label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest">Storage 1
(Sub)</label>
<div class="grid grid-cols-2 gap-2">
<input type="text" x-model="formData.hdd1_model" placeholder="타입/모델"
class="w-full px-4 py-2 bg-white border border-slate-200 rounded-lg text-sm outline-none">
<input type="text" x-model="formData.hdd1_capacity" placeholder="용량"
class="w-full px-4 py-2 bg-white border border-slate-200 rounded-lg text-sm outline-none">
</div>
</div>
</div>
<!-- 자산 정보 -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">노트북 상태</label>
<input type="text" x-model="formData.asset_status" placeholder="예: 정상"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">고정 IP</label>
<input type="text" x-model="formData.fixed_ip" placeholder="192.168.x.x"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">배정 사용자</label>
<input type="text" x-model="formData.assigned_user" placeholder="성함"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2 text-blue-600">취득일</label>
<input type="date" x-model="formData.purchase_date"
class="w-full px-4 py-3 bg-blue-50 border border-blue-100 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
</div>
<!-- 기타 사양 -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">정격입출력</label>
<input type="text" x-model="formData.power_rating" placeholder="예: 20V / 3.25A"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">판매사</label>
<input type="text" x-model="formData.vendor" placeholder="예: 오픈마켓"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">옵션</label>
<input type="text" x-model="formData.options" placeholder="예: 지문인식"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">비고 (특기사항)</label>
<textarea x-model="formData.remarks" rows="2" placeholder="기타 참고할 사양 정보"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none resize-none"></textarea>
</div>
<div class="pt-4 flex gap-3">
<button type="button" @click="showModal = false"
class="flex-1 py-4 bg-slate-100 rounded-2xl font-bold">취소</button>
<button type="submit"
class="flex-1 py-4 bg-blue-600 text-white rounded-2xl font-bold shadow-lg shadow-blue-200"
x-text="isEdit ? '수정 완료' : '모델 등록'"></button>
</div>
</form>
</div>
</div>
<!-- Dept Modal -->
<div x-show="showDeptModal" x-cloak class="fixed inset-0 z-[110] flex items-center justify-center p-4">
<div class="fixed inset-0 modal-bg" @click="showDeptModal = false"></div>
<div class="bg-white rounded-3xl p-8 max-w-md w-full relative z-[111] shadow-2xl">
<h3 class="text-2xl font-black mb-6" x-text="isDeptEdit ? '부서 정보 수정' : '신규 부서 생성'"></h3>
<form @submit.prevent="submitDept" class="space-y-4">
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">부서명</label>
<input type="text" x-model="deptFormData.name" required
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-slate-500 uppercase mb-2">상위 부서</label>
<select x-model="deptFormData.parent_id"
class="w-full px-4 py-3 bg-slate-50 border border-slate-200 rounded-xl focus:ring-2 focus:ring-blue-500 outline-none">
<option value="">없음 (최상위)</option>
<template x-for="d in depts" :key="d.id">
<option :value="d.id" x-text="d.path" :disabled="d.id == deptFormData.id"></option>
</template>
</select>
</div>
<div class="pt-4 flex gap-3">
<button type="button" @click="showDeptModal = false"
class="flex-1 py-3 bg-slate-100 rounded-xl font-bold">취소</button>
<button type="submit" class="flex-1 py-3 bg-blue-600 text-white rounded-xl font-bold"
x-text="isDeptEdit ? '수정 완료' : '부서 생성'"></button>
</div>
</form>
</div>
</div>
<!-- Members Modal -->
<div x-show="showMembersModal" x-cloak class="fixed inset-0 z-[110] flex items-center justify-center p-4">
<div class="fixed inset-0 modal-bg" @click="showMembersModal = false"></div>
<div
class="bg-white rounded-3xl p-8 max-w-2xl w-full relative z-[111] shadow-2xl overflow-hidden flex flex-col max-h-[80vh]">
<div class="flex justify-between items-center mb-6">
<h3 class="text-2xl font-black text-slate-900" x-text="selectedDept?.name + ' 부서원'"></h3>
<span class="text-xs font-bold text-blue-600 bg-blue-50 px-3 py-1 rounded-full"><span
x-text="members.length"></span>명</span>
</div>
<div class="flex-1 overflow-y-auto pr-2 no-scrollbar">
<table class="min-w-full divide-y divide-slate-100">
<thead class="bg-slate-50 sticky top-0">
<tr>
<th class="px-4 py-3 text-left text-xs font-bold text-slate-500">이름</th>
<th class="px-4 py-3 text-left text-xs font-bold text-slate-500">사번/직위</th>
<th class="px-4 py-3 text-right text-xs font-bold text-slate-500">이동</th>
</tr>
</thead>
<tbody class="divide-y divide-slate-50">
<template x-for="user in members" :key="user.id">
<tr>
<td class="px-4 py-3 text-sm font-bold text-slate-900" x-text="user.name"></td>
<td class="px-4 py-3">
<div class="text-xs font-black text-slate-400" x-text="user.emp_id"></div>
<div class="text-[10px] text-blue-500 font-bold uppercase"
x-text="user.position || '-'"></div>
</td>
<td class="px-4 py-3 text-right">
<select @change="reassignMember(user.id, $event.target.value)"
class="text-xs bg-slate-100 border-none rounded-lg px-2 py-1 focus:ring-2 focus:ring-blue-500">
<option value="">부서 이동...</option>
<template x-for="d in depts" :key="d.id">
<option :value="d.id" x-text="d.name" :disabled="d.id == selectedDept.id">
</option>
</template>
</select>
</td>
</tr>
</template>
</tbody>
</table>
<div x-show="members.length === 0" class="py-10 text-center text-slate-400 italic text-sm">소속된 부서원이
없습니다.</div>
</div>
<div class="mt-6 pt-6 border-t border-slate-100 flex justify-end">
<button @click="showMembersModal = false"
class="px-6 py-2.5 bg-slate-900 text-white rounded-xl font-bold">닫기</button>
</div>
</div>
</div>
<script>
function dbManagement() {
return {
currentTab: 'models',
// Model Data
models: [],
showModal: false,
isEdit: false,
selectedIds: [],
bulkManufacturer: '',
bulkProductName: '',
formData: {
id: '', manufacturer: '', model_name: '', product_name: '',
cpu: '', npu: '', ram: '',
hdd0_model: '', hdd0_capacity: '',
hdd1_model: '', hdd1_capacity: '',
asset_status: '', fixed_ip: '', assigned_user: '', purchase_date: '',
power_rating: '', vendor: '', options: '', remarks: '', specs: ''
},
// Dept Data
depts: [],
showDeptModal: false,
showMembersModal: false,
isDeptEdit: false,
selectedDept: null,
members: [],
deptFormData: { id: '', name: '', parent_id: '', level: 1 },
init() {
this.fetchModels();
this.fetchDepts();
},
// Model Methods
fetchModels() {
const scrollPos = window.scrollY;
fetch('api.php?action=get_models').then(res => res.json()).then(data => {
this.models = data;
this.$nextTick(() => window.scrollTo(0, scrollPos));
});
},
toggleSelectAll(checked) {
this.selectedIds = checked ? this.models.map(m => m.id) : [];
},
applyBulkUpdate() {
if (this.selectedIds.length === 0) return;
if (!this.bulkManufacturer && !this.bulkProductName) { alert('변경할 항목을 선택해주세요.'); return; }
fetch('api.php?action=bulk_update_models', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
ids: this.selectedIds,
manufacturer: this.bulkManufacturer,
product_name: this.bulkProductName
})
}).then(res => res.json()).then(data => {
if (data.success) {
this.selectedIds = [];
this.bulkManufacturer = '';
this.bulkProductName = '';
this.fetchModels();
}
});
},
openAddModal() {
this.isEdit = false;
this.formData = {
id: '', manufacturer: '', model_name: '', product_name: '',
cpu: '', npu: '', ram: '', hdd0_model: '', hdd0_capacity: '', hdd1_model: '', hdd1_capacity: '',
asset_status: '', fixed_ip: '', assigned_user: '', purchase_date: '',
power_rating: '', vendor: '', options: '', remarks: '', specs: ''
};
this.showModal = true;
},
editModel(model) {
this.isEdit = true;
this.formData = { ...model };
this.showModal = true;
},
submitModel() {
const action = this.isEdit ? 'update_model' : 'add_model';
fetch(`api.php?action=${action}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(this.formData)
}).then(res => res.json()).then(data => {
if (data.success) {
this.showModal = false;
this.fetchModels();
}
});
},
// Dept Methods
fetchDepts() {
fetch('api.php?action=get_departments').then(res => res.json()).then(data => {
this.depts = data;
});
},
getDeptLevel(path) {
if (!path) return 0;
return (path.match(/>/g) || []).length;
},
openAddDeptModal() {
this.isDeptEdit = false;
this.deptFormData = { id: '', name: '', parent_id: '', level: 1 };
this.showDeptModal = true;
},
editDept(dept) {
this.isDeptEdit = true;
this.deptFormData = { id: dept.id, name: dept.name, parent_id: dept.parent_id || '', level: dept.level };
this.showDeptModal = true;
},
submitDept() {
const action = this.isDeptEdit ? 'update_dept' : 'add_dept';
if (this.deptFormData.parent_id) {
const parent = this.depts.find(d => d.id == this.deptFormData.parent_id);
this.deptFormData.level = this.getDeptLevel(parent.path) + 1;
} else {
this.deptFormData.level = 0;
}
fetch(`api.php?action=${action}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(this.deptFormData)
}).then(res => res.json()).then(data => {
if (data.success) {
this.showDeptModal = false;
this.fetchDepts();
}
});
},
viewDeptMembers(dept) {
this.selectedDept = dept;
fetch(`api.php?action=get_dept_users&dept_id=${dept.id}`).then(res => res.json()).then(data => {
this.members = data;
this.showMembersModal = true;
});
},
reassignMember(userId, newDeptId) {
if (!newDeptId) return;
fetch('api.php?action=reassign_user', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ user_ids: [userId], new_dept_id: newDeptId })
}).then(res => res.json()).then(data => {
if (data.success) {
this.viewDeptMembers(this.selectedDept);
this.fetchDepts();
}
});
}
}
}
</script>
</body>
</html>