<?php
declare(strict_types=1);

$tenant = ltrim((string)($_REQUEST['tenant'] ?? ''), '/');

if ($tenant !== '' && file_exists('../../../' . $tenant . '/config.php')) {
    require_once '../../../' . $tenant . '/config.php';
}

require_once __DIR__ . '/cron_functions.php';

if (!defined('TENANT_URL')) {
    echo '<div class="alert alert-danger">TENANT_URL is not defined. Tenant context missing.</div>';
    exit;
}

if (!isset($con) || !($con instanceof mysqli)) {
    echo '<div class="alert alert-danger">Database connection is not available.</div>';
    exit;
}

$filename = basename((string)($_GET['filename'] ?? ''));

if ($filename === '' || !cronIsValidAutomationFile($filename)) {
    echo '<div class="alert alert-danger">Invalid cron job.</div>';
    exit;
}

$db = cronDbGetJobByScript($con, $filename);
$emails = cronReadJobRecipients($filename);

$time = $db ? substr((string)$db['run_time'], 0, 5) : '06:00';
$name = cronPublicJobName($filename);
?>

<form id="cronEditForm">
    <input type="hidden" name="filename" value="<?= htmlspecialchars($filename, ENT_QUOTES, 'UTF-8') ?>">

    <div class="row g-3">
        <div class="col-12">
            <label class="form-label fw-bold">Job</label>
            <input
                type="text"
                class="form-control"
                value="<?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>"
                readonly>
        </div>

        <div class="col-md-4">
            <label class="form-label fw-bold" for="cron_time">Send time</label>
            <input
                type="time"
                id="cron_time"
                name="time"
                class="form-control"
                value="<?= htmlspecialchars($time, ENT_QUOTES, 'UTF-8') ?>">
        </div>

        <div class="col-md-8">
            <label class="form-label fw-bold">Script</label>
            <input
                type="text"
                class="form-control"
                value="<?= htmlspecialchars($filename, ENT_QUOTES, 'UTF-8') ?>"
                readonly>
        </div>

        <div class="col-12">
            <label class="form-label fw-bold" for="cron_recipients">Recipients</label>
            <textarea
                id="cron_recipients"
                name="recipients"
                class="form-control"
                rows="5"
                placeholder="One email per line"><?= htmlspecialchars(implode("\n", $emails['recipients']), ENT_QUOTES, 'UTF-8') ?></textarea>
            <div class="form-text">Use one email address per line, or separate addresses with commas.</div>
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold" for="cron_cc">CC</label>
            <textarea
                id="cron_cc"
                name="cc"
                class="form-control"
                rows="3"
                placeholder="Optional"><?= htmlspecialchars(implode("\n", $emails['cc']), ENT_QUOTES, 'UTF-8') ?></textarea>
        </div>

        <div class="col-md-6">
            <label class="form-label fw-bold" for="cron_bcc">BCC</label>
            <textarea
                id="cron_bcc"
                name="bcc"
                class="form-control"
                rows="3"
                placeholder="Optional"><?= htmlspecialchars(implode("\n", $emails['bcc']), ENT_QUOTES, 'UTF-8') ?></textarea>
        </div>
    </div>
</form>