35050-vm/edit_deal.php
Flatlogic Bot b4397b355f Sfepro v1
2025-10-19 03:26:15 +00:00

117 lines
4.4 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'header.php';
$id = $_GET['id'] ?? null;
if (!$id) {
header("Location: index.php");
exit;
}
$pdo = db();
// Fetch all contacts for the dropdown
try {
$stmt = $pdo->query("SELECT id, name, company FROM contacts ORDER BY name ASC");
$contacts = $stmt->fetchAll();
} catch (PDOException $e) {
$error = "Error fetching contacts: " . $e->getMessage();
$contacts = [];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$value = $_POST['value'] ?? 0;
$stage = $_POST['stage'] ?? 'Lead';
$contact_id = $_POST['contact_id'] ?? null;
$close_date = $_POST['close_date'] ?? null;
if (empty($title) || empty($contact_id)) {
$error = "Title and contact are required.";
} else {
try {
$stmt = $pdo->prepare("UPDATE deals SET title = :title, value = :value, stage = :stage, contact_id = :contact_id, close_date = :close_date WHERE id = :id");
$stmt->execute([
':title' => $title,
':value' => $value,
':stage' => $stage,
':contact_id' => $contact_id,
':close_date' => !empty($close_date) ? $close_date : null,
':id' => $id
]);
header("Location: index.php?status=deal_updated");
exit;
} catch (PDOException $e) {
$error = "Database error: " . $e->getMessage();
}
}
}
try {
$stmt = $pdo->prepare("SELECT * FROM deals WHERE id = :id");
$stmt->execute([':id' => $id]);
$deal = $stmt->fetch();
if (!$deal) {
header("Location: index.php");
exit;
}
} catch (PDOException $e) {
echo "Error: " . $e->getMessage();
exit;
}
$deal_stages = ['Lead', 'Prospecting', 'Qualification', 'Proposal', 'Negotiation', 'Closed Won', 'Closed Lost'];
?>
<div class="container mt-4">
<h2><i class="fas fa-edit me-2"></i>Edit Deal</h2>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form action="edit_deal.php?id=<?php echo $id; ?>" method="post" class="card p-4">
<div class="row g-3">
<div class="col-md-6">
<label for="title" class="form-label">Deal Title</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($deal['title']); ?>" required>
</div>
<div class="col-md-6">
<label for="value" class="form-label">Value ($)</label>
<input type="number" step="0.01" class="form-control" id="value" name="value" value="<?php echo htmlspecialchars($deal['value']); ?>">
</div>
<div class="col-md-6">
<label for="stage" class="form-label">Stage</label>
<select class="form-select" id="stage" name="stage">
<?php foreach ($deal_stages as $stage_option): ?>
<option value="<?php echo $stage_option; ?>" <?php if ($deal['stage'] === $stage_option) echo 'selected'; ?>><?php echo $stage_option; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-6">
<label for="contact_id" class="form-label">Contact</label>
<select class="form-select" id="contact_id" name="contact_id" required>
<option value="">Select a contact</option>
<?php foreach ($contacts as $contact): ?>
<option value="<?php echo $contact['id']; ?>" <?php if ($deal['contact_id'] == $contact['id'] ) echo 'selected'; ?>>
<?php echo htmlspecialchars($contact['name']); ?> (<?php echo htmlspecialchars($contact['company']); ?>)
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-12">
<label for="close_date" class="form-label">Expected Close Date</label>
<input type="date" class="form-control" id="close_date" name="close_date" value="<?php echo htmlspecialchars($deal['close_date']); ?>">
</div>
</div>
<div class="mt-4">
<button type="submit" class="btn btn-primary">Save Changes</button>
<a href="index.php" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<?php require_once 'footer.php'; ?>