<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
require_once __DIR__ . '/../../app/core/access_guards.php';
auth_require_login();

$pdo = crm_pdo();
gp_require_staff_access($pdo);
$page_title = 'Customer';

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

$item = [
    'name' => '',
    'contact_person' => '',
    'phone' => '',
    'email' => '',
    'address' => '',
    'postal_code' => '',
    'city' => '',
    'notes' => '',
    'active' => 1,
];

$errors = [];

if ($id) {
    $st = $pdo->prepare("
        SELECT name, contact_person, phone, email, address, postal_code, city, notes, active
        FROM customers
        WHERE id = :id
        LIMIT 1
    ");
    $st->execute([':id' => $id]);
    $row = $st->fetch();
    if ($row) {
        $item = array_merge($item, $row);
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    gp_require_csrf();
    $id = (int)($_POST['id'] ?? 0);

    $item['name'] = trim((string)($_POST['name'] ?? ''));
    $item['contact_person'] = trim((string)($_POST['contact_person'] ?? ''));
    $item['phone'] = trim((string)($_POST['phone'] ?? ''));
    $item['email'] = trim((string)($_POST['email'] ?? ''));
    $item['address'] = trim((string)($_POST['address'] ?? ''));
    $item['postal_code'] = trim((string)($_POST['postal_code'] ?? ''));
    $item['city'] = trim((string)($_POST['city'] ?? ''));
    $item['notes'] = trim((string)($_POST['notes'] ?? ''));
    $item['active'] = isset($_POST['active']) ? 1 : 0;

    if ($item['name'] === '') {
        $errors[] = 'Name is required.';
    }

    if (!$errors) {
        $params = [
            ':name' => $item['name'],
            ':contact_person' => $item['contact_person'] !== '' ? $item['contact_person'] : null,
            ':phone' => $item['phone'] !== '' ? $item['phone'] : null,
            ':email' => $item['email'] !== '' ? $item['email'] : null,
            ':address' => $item['address'] !== '' ? $item['address'] : null,
            ':postal_code' => $item['postal_code'] !== '' ? $item['postal_code'] : null,
            ':city' => $item['city'] !== '' ? $item['city'] : null,
            ':notes' => $item['notes'] !== '' ? $item['notes'] : null,
            ':active' => $item['active'],
        ];

        if ($id > 0) {
            $sql = "
                UPDATE customers
                SET
                    name = :name,
                    contact_person = :contact_person,
                    phone = :phone,
                    email = :email,
                    address = :address,
                    postal_code = :postal_code,
                    city = :city,
                    notes = :notes,
                    active = :active
                WHERE id = :id
            ";
            $params[':id'] = $id;
        } else {
            $sql = "
                INSERT INTO customers
                    (name, contact_person, phone, email, address, postal_code, city, notes, active)
                VALUES
                    (:name, :contact_person, :phone, :email, :address, :postal_code, :city, :notes, :active)
            ";
        }

        $st = $pdo->prepare($sql);
        $st->execute($params);

        redirect('/gopools/public/customers/index.php');
    }
}

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
    <div class="page-header-title">
        <h1><?php echo $id ? 'Edit customer' : 'New customer'; ?></h1>
    </div>
</div>

<div class="card">
    <?php if ($errors): ?>
        <div class="alert-danger"><?php echo h(implode(' ', $errors)); ?></div>
    <?php endif; ?>

    <form method="post">
      <?php echo csrf_field(); ?>
        <input type="hidden" name="id" value="<?php echo (int)$id; ?>">

        <div class="form-grid">
            <div class="form-group">
                <label>Name</label>
                <input name="name" value="<?php echo h($item['name']); ?>">
            </div>

            <div class="form-group">
                <label>Contact person</label>
                <input name="contact_person" value="<?php echo h($item['contact_person']); ?>">
            </div>

            <div class="form-group">
                <label>Phone</label>
                <input name="phone" value="<?php echo h($item['phone']); ?>">
            </div>

            <div class="form-group">
                <label>Email</label>
                <input name="email" value="<?php echo h($item['email']); ?>">
            </div>

            <div class="form-group">
                <label>Address</label>
                <input name="address" value="<?php echo h($item['address']); ?>">
            </div>

            <div class="form-group">
                <label>Postal code</label>
                <input name="postal_code" value="<?php echo h($item['postal_code']); ?>">
            </div>

            <div class="form-group">
                <label>City</label>
                <input name="city" value="<?php echo h($item['city']); ?>">
            </div>

            <div class="form-group">
                <label>Active</label>
                <div>
                    <input type="checkbox" name="active" <?php echo ((int)$item['active'] === 1) ? 'checked' : ''; ?>>
                </div>
            </div>

            <div class="form-group" style="grid-column:1/-1">
                <label>Notes</label>
                <textarea name="notes"><?php echo h($item['notes']); ?></textarea>
            </div>
        </div>

        <div class="form-actions">
            <button class="btn btn-primary" type="submit">Save</button>
            <a class="btn btn-secondary" href="/gopools/public/customers/index.php">Cancel</a>
        </div>
    </form>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
