requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $params = $this->getPaginationParams(); $builder = $this->getDB()->table('branch_managers bm'); $builder->select('bm.*, b.name as branch_name'); $builder->join('branches b', 'b.id = bm.branch_id', 'left'); // Filter by showroom (branch_id) $showroom = $this->request->getGet('showroom'); if ($showroom) { $builder->where('bm.branch_id', $showroom); } // Search $searchType = $this->request->getGet('search_type'); $searchKeyword = $this->request->getGet('search_keyword'); if ($searchType && $searchKeyword) { if ($searchType === 'branch_name') { $builder->like('b.name', $searchKeyword); } elseif ($searchType === 'name') { $builder->like('bm.name', $searchKeyword); } elseif ($searchType === 'username') { $builder->like('bm.username', $searchKeyword); } elseif ($searchType === 'email') { $builder->like('bm.email', $searchKeyword); } } $builder->orderBy('bm.id', 'DESC'); $result = $this->paginatedResponse($builder, $params); return $this->respondSuccess($result); } /** * Get single branch manager */ public function show($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('branch_managers bm'); $builder->select('bm.*, b.name as branch_name'); $builder->join('branches b', 'b.id = bm.branch_id', 'left'); $builder->where('bm.id', $id); $manager = $builder->get()->getRow(); if (!$manager) { return $this->respondError('지점장을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND); } // Remove password from response unset($manager->password); return $this->respondSuccess($manager); } /** * Check if user_id is available */ public function checkUserId() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $userId = $this->request->getGet('user_id'); if (empty($userId)) { return $this->respondError('아이디를 입력하세요.'); } $builder = $this->getDB()->table('branch_managers'); $existing = $builder->where('username', $userId)->get()->getRow(); return $this->respondSuccess([ 'available' => !$existing ]); } /** * Create branch manager */ public function create() { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); // Check if username already exists $builder = $this->getDB()->table('branch_managers'); $existing = $builder->where('username', $json->user_id)->get()->getRow(); if ($existing) { return $this->respondError('이미 사용 중인 아이디입니다.'); } $data = [ 'branch_id' => $json->branch_id ?? null, 'username' => $json->user_id ?? '', // 프론트에서 user_id로 전송 'password' => password_hash($json->password ?? '', PASSWORD_DEFAULT), 'name' => $json->name ?? '', 'email' => $json->email ?? '', 'greeting' => $json->greeting ?? '', 'photo_url' => $json->photo_url ?? '', 'created_at' => date('Y-m-d H:i:s') ]; $builder->insert($data); return $this->respondSuccess(['id' => $this->getDB()->insertID()], '지점장이 등록되었습니다.'); } /** * Update branch manager */ public function update($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $json = $this->request->getJSON(); $data = [ 'branch_id' => $json->branch_id ?? null, 'name' => $json->name ?? '', 'email' => $json->email ?? '', 'greeting' => $json->greeting ?? '', 'photo_url' => $json->photo_url ?? '', 'updated_at' => date('Y-m-d H:i:s') ]; // Update password only if provided if (!empty($json->password)) { $data['password'] = password_hash($json->password, PASSWORD_DEFAULT); } $builder = $this->getDB()->table('branch_managers'); $builder->where('id', $id)->update($data); return $this->respondSuccess(null, '지점장이 수정되었습니다.'); } /** * Delete branch manager */ public function delete($id = null) { $auth = $this->requireAuth(); if ($auth instanceof ResponseInterface) { return $auth; } $builder = $this->getDB()->table('branch_managers'); $builder->where('id', $id)->delete(); return $this->respondSuccess(null, '지점장이 삭제되었습니다.'); } /** * Get public branch manager list (No authentication required) * 공개 API - 인증 없이 지점장 정보 조회 */ public function publicList() { try { $builder = $this->getDB()->table('branch_managers bm'); $builder->select('bm.*, b.name as branch_name'); $builder->join('branches b', 'b.id = bm.branch_id', 'left'); // Filter by showroom (branch_id) $showroom = $this->request->getGet('showroom'); if ($showroom) { $builder->where('bm.branch_id', $showroom); } // Only active branches $builder->where('b.is_active', 1); $builder->orderBy('bm.id', 'DESC'); $managers = $builder->get()->getResult(); return $this->respondSuccess($managers); } catch (\Exception $e) { log_message('error', 'Branch manager public list error: ' . $e->getMessage()); return $this->respondError('서버 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR); } } }