prepare(" UPDATE clients SET company_name = ?, industry = ?, contact_person = ?, email = ?, phone = ?, address = ?, city = ?, country = ?, postal_code = ?, website = ?, notes = ?, status = ?, updated_at = NOW() WHERE id = ? "); $stmt->execute([ $company_name, $industry, $contact_person, $email, $phone, $address, $city, $country, $postal_code, $website, $notes, $status, $client_id ]); // Handle file uploads if (!empty($_FILES['attachments']['name'][0])) { $upload_dir = 'uploads/clients/'; if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } $allowed_types = ['application/pdf', 'image/jpeg', 'image/jpg', 'image/png', 'image/gif']; $max_size = 5 * 1024 * 1024; // Get client code $clientStmt = $pdo->prepare("SELECT client_code FROM clients WHERE id = ?"); $clientStmt->execute([$client_id]); $client_code = $clientStmt->fetchColumn(); for ($i = 0; $i < count($_FILES['attachments']['name']); $i++) { if ($_FILES['attachments']['error'][$i] === UPLOAD_ERR_OK) { $file_type = $_FILES['attachments']['type'][$i]; $file_size = $_FILES['attachments']['size'][$i]; if (in_array($file_type, $allowed_types) && $file_size <= $max_size) { $file_name = $_FILES['attachments']['name'][$i]; $file_tmp = $_FILES['attachments']['tmp_name'][$i]; $extension = pathinfo($file_name, PATHINFO_EXTENSION); $unique_name = $client_code . '_' . time() . '_' . $i . '.' . $extension; $file_path = $upload_dir . $unique_name; if (move_uploaded_file($file_tmp, $file_path)) { $attachStmt = $pdo->prepare(" INSERT INTO client_attachments (client_id, file_name, file_path, file_type, file_size, uploaded_at) VALUES (?, ?, ?, ?, ?, NOW()) "); $attachStmt->execute([$client_id, $file_name, $file_path, $file_type, $file_size]); } } } } } logActivity($_SESSION['admin_id'], 'update_client', "Updated client #$client_id", 'client', $client_id); $success = 'Client updated successfully!'; } catch (Exception $e) { $error = 'An error occurred. Please try again.'; error_log("Update client error: " . $e->getMessage()); } } } // Handle attachment deletion - SEPARATE ACTION if ($_POST['action'] === 'delete_attachment') { $attach_id = intval($_POST['attachment_id'] ?? 0); if ($attach_id) { try { $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT * FROM client_attachments WHERE id = ? AND client_id = ?"); $stmt->execute([$attach_id, $client_id]); $attach = $stmt->fetch(); if ($attach) { if (file_exists($attach['file_path'])) { unlink($attach['file_path']); } $stmt = $pdo->prepare("DELETE FROM client_attachments WHERE id = ?"); $stmt->execute([$attach_id]); $success = 'Attachment deleted successfully!'; } } catch (Exception $e) { $error = 'Error deleting attachment.'; } } } } // Fetch client details try { $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT * FROM clients WHERE id = ?"); $stmt->execute([$client_id]); $client = $stmt->fetch(); if (!$client) { header('Location: clients.php'); exit; } // Fetch attachments $attachStmt = $pdo->prepare("SELECT * FROM client_attachments WHERE client_id = ?"); $attachStmt->execute([$client_id]); $attachments = $attachStmt->fetchAll(); } catch (Exception $e) { error_log("Fetch client error: " . $e->getMessage()); header('Location: clients.php'); exit; } $industries = [ 'Technology', 'Healthcare', 'Finance', 'Retail', 'Manufacturing', 'Education', 'Real Estate', 'Hospitality', 'Consulting', 'Marketing', 'Construction', 'Transportation', 'Other' ]; $countries = [ 'India', 'United States', 'United Kingdom', 'Canada', 'Australia', 'Germany', 'France', 'Japan', 'China', 'Singapore', 'United Arab Emirates', 'Saudi Arabia', 'Malaysia', 'Thailand', 'Indonesia', 'Philippines', 'Vietnam', 'South Korea', 'Hong Kong', 'Taiwan', 'Bangladesh', 'Pakistan', 'Sri Lanka', 'Nepal', 'Bhutan', 'Afghanistan', 'Maldives', 'Myanmar', 'Cambodia', 'Laos', 'Other' ]; include 'includes/header.php'; ?>