<?php
declare(strict_types=1);

require_once __DIR__ . '/cma-functions.php';

$fields = [
    'Photo',
    'Status',
    'MLS',
    'Street Address',
    'Municipality',
    'SubType',
    'Bed',
    'Bath',
    'AFSF',
    'TFSF',
    'Built',
    'Orig Price',
    'List Price',
    'Sale Price',
    'SP%LP',
    'Date',
    'DOM',
    'CDOM',
    '$/AFSF',
    '$/TFSF',
    'SC',
    'Garage',
    'longitude',
    'latitude',
];

$message = '';

$subject = cma_load_subject();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $subject = [];

    foreach ($fields as $field) {
        $subject[$field] = trim((string)($_POST[$field] ?? ''));
    }

    cma_save_subject($subject);
    $message = 'Subject property saved.';
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CMA Subject Property</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 30px;
            background: #f6f7fb;
        }
        .wrap {
            max-width: 900px;
            background: #fff;
            padding: 24px;
            border-radius: 10px;
            box-shadow: 0 8px 22px rgba(0,0,0,.08);
        }
        .grid {
            display: grid;
            grid-template-columns: 190px 1fr;
            gap: 10px;
            align-items: center;
        }
        label {
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 9px;
            font-size: 15px;
        }
        .button {
            background: #01377d;
            color: #fff;
            border: 0;
            padding: 10px 18px;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            margin-top: 16px;
        }
        .message {
            color: #116329;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div class="wrap">
    <h1>CMA Subject Property</h1>

    <?php if ($message !== ''): ?>
        <p class="message"><?php echo cma_h($message); ?></p>
    <?php endif; ?>

    <form method="post">
        <div class="grid">
            <?php foreach ($fields as $field): ?>
                <label for="<?php echo cma_h($field); ?>"><?php echo cma_h($field); ?></label>
                <input
                    id="<?php echo cma_h($field); ?>"
                    name="<?php echo cma_h($field); ?>"
                    value="<?php echo cma_h($subject[$field] ?? ''); ?>"
                >
            <?php endforeach; ?>
        </div>

        <button class="button" type="submit">Save Subject Property</button>
        <a class="button" href="cma-listings.php">Edit MLS List</a>
        <a class="button" href="cma-comp.php">View Comp Page</a>
    </form>
</div>
</body>
</html>