<?php
echo message_box('success');
echo message_box('error');
?>

<?php if (!empty($circular_details)) {
    $deadline = new DateTime(date('Y-m-d', strtotime($circular_details->last_date)));
    $today = new DateTime(date('Y-m-d'));
    if ($today > $deadline) {
        $stateClass = 'is-expired';
        $stateText = lang('expired');
    } elseif ($today == $deadline) {
        $stateClass = 'is-today';
        $stateText = lang('last_date');
    } else {
        $remainingDays = (int) $today->diff($deadline)->format('%a');
        $stateClass = $remainingDays <= 7 ? 'is-soon' : 'is-open';
        $stateText = $remainingDays . ' ' . lang('days');
    }

    $designation = '-';
    if (!empty($circular_details->designations_id)) {
        $design_info = $this->db->where('designations_id', $circular_details->designations_id)->get('tbl_designations')->row();
        if (!empty($design_info->designations)) {
            $designation = $design_info->designations;
        }
    }

    $show_custom_fields = custom_form_label(14, $circular_details->job_circular_id);
    $summaryItems = array(
        array('label' => lang('designation'), 'value' => $designation),
        array('label' => lang('employment_type'), 'value' => lang($circular_details->employment_type)),
        array('label' => lang('experience'), 'value' => $circular_details->experience),
        array('label' => lang('salary_range'), 'value' => $circular_details->salary_range),
        array('label' => lang('vacancy_no'), 'value' => $circular_details->vacancy_no),
        array('label' => lang('posted_date'), 'value' => display_date($circular_details->posted_date)),
        array('label' => lang('last_date'), 'value' => display_date($circular_details->last_date)),
    );
    ?>
    <style>
        .job-detail-page {
            --job-ink: #0f172a;
            --job-muted: #64748b;
            --job-line: #dbe3ee;
            --job-soft: #f6f8fb;
            --job-surface: #ffffff;
            --job-elevated: #f8fafc;
            --job-primary: #1d4ed8;
            --job-primary-strong: #173f9f;
            --job-success: #047857;
            --job-warning: #b45309;
            --job-danger: #b91c1c;
            --job-shadow: 0 18px 42px rgba(15, 23, 42, .07);
            margin: -20px -15px 0;
            background: var(--job-soft);
            color: var(--job-ink);
            font-family: Inter, "Source Sans Pro", Arial, sans-serif;
        }

        body.frontend-dark .job-detail-page {
            --job-ink: #e5edf8;
            --job-muted: #94a3b8;
            --job-line: #263244;
            --job-soft: #0b1120;
            --job-surface: #111827;
            --job-elevated: #0f172a;
            --job-primary: #60a5fa;
            --job-primary-strong: #3b82f6;
            --job-success: #34d399;
            --job-warning: #fbbf24;
            --job-danger: #f87171;
            --job-shadow: 0 22px 48px rgba(0, 0, 0, .25);
        }

        .job-detail-shell {
            width: min(1120px, calc(100% - 32px));
            margin: 0 auto;
        }

        .job-detail-hero {
            border-bottom: 1px solid var(--job-line);
            background: var(--job-surface);
        }

        .job-detail-hero-inner {
            padding: 34px 0 28px;
        }

        .job-detail-topline {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 14px;
            margin-bottom: 30px;
        }

        .job-detail-back {
            display: inline-flex;
            align-items: center;
            min-height: 36px;
            border: 1px solid var(--job-line);
            border-radius: 8px;
            background: var(--job-elevated);
            padding: 0 12px;
            color: var(--job-muted);
            font-size: 13px;
            font-weight: 800;
            text-decoration: none;
        }

        .job-detail-back:hover {
            color: var(--job-ink);
            text-decoration: none;
        }

        .job-status {
            display: inline-flex;
            align-items: center;
            border-radius: 999px;
            padding: 8px 11px;
            font-size: 12px;
            font-weight: 800;
            white-space: nowrap;
        }

        .job-status.is-open {
            background: rgba(16, 185, 129, .12);
            color: var(--job-success);
        }

        .job-status.is-soon,
        .job-status.is-today {
            background: rgba(245, 158, 11, .14);
            color: var(--job-warning);
        }

        .job-status.is-expired {
            background: rgba(239, 68, 68, .12);
            color: var(--job-danger);
        }

        .job-detail-heading {
            max-width: 860px;
        }

        .job-detail-kicker {
            margin: 0 0 10px;
            color: var(--job-muted);
            font-size: 12px;
            font-weight: 800;
            letter-spacing: .12em;
            text-transform: uppercase;
        }

        .job-detail-title {
            margin: 0;
            color: var(--job-ink);
            font-size: clamp(34px, 5vw, 58px);
            font-weight: 800;
            letter-spacing: 0;
            line-height: 1.02;
        }

        .job-detail-subtitle {
            margin-top: 14px;
            color: var(--job-muted);
            font-size: 16px;
            font-weight: 700;
            line-height: 1.6;
        }

        .job-summary-band {
            display: flex;
            flex-wrap: wrap;
            gap: 1px;
            margin-top: 30px;
            border: 1px solid var(--job-line);
            border-radius: 8px;
            background: var(--job-line);
            overflow: hidden;
        }

        .job-summary-item {
            flex: 1 1 190px;
            min-width: 0;
            background: var(--job-elevated);
            padding: 15px 16px;
        }

        .job-summary-item span {
            display: block;
            color: var(--job-muted);
            font-size: 11px;
            font-weight: 800;
            letter-spacing: .08em;
            text-transform: uppercase;
        }

        .job-summary-item strong {
            display: block;
            margin-top: 7px;
            overflow: hidden;
            color: var(--job-ink);
            font-size: 14px;
            font-weight: 800;
            line-height: 1.35;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .job-hero-actions {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 18px;
        }

        .job-action-primary,
        .job-action-secondary,
        .job-action-disabled {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-height: 42px;
            border-radius: 8px;
            padding: 0 16px;
            font-size: 14px;
            font-weight: 800;
            text-decoration: none;
        }

        .job-action-primary {
            border: 1px solid var(--job-primary);
            background: var(--job-primary);
            color: #ffffff;
        }

        .job-action-primary:hover {
            border-color: var(--job-primary-strong);
            background: var(--job-primary-strong);
            color: #ffffff;
            text-decoration: none;
        }

        .job-action-secondary {
            border: 1px solid var(--job-line);
            background: var(--job-surface);
            color: var(--job-ink);
        }

        .job-action-secondary:hover {
            color: var(--job-primary);
            text-decoration: none;
        }

        .job-action-disabled {
            border: 1px solid var(--job-line);
            background: var(--job-elevated);
            color: var(--job-muted);
            cursor: not-allowed;
        }

        .job-detail-main {
            padding: 30px 0 56px;
        }

        .job-detail-card {
            max-width: 900px;
            border: 1px solid var(--job-line);
            border-radius: 8px;
            background: var(--job-surface);
            box-shadow: var(--job-shadow);
        }

        .job-detail-card-header {
            border-bottom: 1px solid var(--job-line);
            padding: 22px 24px;
        }

        .job-detail-card-header h2 {
            margin: 0;
            color: var(--job-ink);
            font-size: 19px;
            font-weight: 800;
        }

        .job-detail-body {
            padding: 26px 24px 30px;
        }

        .job-detail-description {
            color: var(--job-ink);
            font-size: 15px;
            line-height: 1.85;
        }

        .job-detail-description p,
        .job-detail-description li {
            margin-bottom: 12px;
        }

        .job-detail-description ul,
        .job-detail-description ol {
            padding-left: 22px;
        }

        @media (max-width: 640px) {
            .job-detail-page {
                margin-left: -10px;
                margin-right: -10px;
            }

            .job-detail-topline {
                align-items: flex-start;
                flex-direction: column;
            }

            .job-summary-band {
                flex-direction: column;
            }

            .job-action-primary,
            .job-action-secondary,
            .job-action-disabled {
                width: 100%;
            }
        }
    </style>

    <div class="job-detail-page">
        <section class="job-detail-hero">
            <div class="job-detail-shell job-detail-hero-inner">
                <div class="job-detail-topline">
                    <a href="<?= base_url('frontend') ?>" class="job-detail-back"><?= lang('all_job_circular') ?></a>
                    <span class="job-status <?= html_escape($stateClass) ?>"><?= html_escape($stateText) ?></span>
                </div>

                <div class="job-detail-heading">
                    <p class="job-detail-kicker"><?= lang('view_circular_details') ?></p>
                    <h1 class="job-detail-title"><?= html_escape($circular_details->job_title) ?></h1>
                    <div class="job-detail-subtitle"><?= html_escape($designation) ?></div>
                </div>

                <div class="job-summary-band" aria-label="<?= html_escape(lang('job_summery')) ?>">
                    <?php foreach ($summaryItems as $item) {
                        if ((string) $item['value'] === '') {
                            continue;
                        } ?>
                        <div class="job-summary-item">
                            <span><?= html_escape($item['label']) ?></span>
                            <strong title="<?= html_escape(strip_tags((string) $item['value'])) ?>"><?= html_escape(strip_tags((string) $item['value'])) ?></strong>
                        </div>
                    <?php } ?>
                    <?php if (!empty($show_custom_fields)) {
                        foreach ($show_custom_fields as $c_label => $v_fields) {
                            if (!empty($v_fields)) { ?>
                                <div class="job-summary-item">
                                    <span><?= html_escape($c_label) ?></span>
                                    <strong title="<?= html_escape(strip_tags((string) $v_fields)) ?>"><?= html_escape(strip_tags((string) $v_fields)) ?></strong>
                                </div>
                            <?php }
                        }
                    } ?>
                </div>

                <div class="job-hero-actions">
                    <?php if ($stateClass === 'is-expired') { ?>
                        <span class="job-action-disabled">Applications closed</span>
                    <?php } else { ?>
                        <a href="<?= base_url('frontend/apply_jobs/' . (int) $circular_details->job_circular_id) ?>"
                           class="job-action-primary" data-toggle="modal" data-target="#myModal_lg"><?= lang('apply_now') ?></a>
                    <?php } ?>
                    <a href="<?= base_url('frontend/jobs_posted_pdf/' . (int) $circular_details->job_circular_id) ?>"
                       class="job-action-secondary"><?= lang('pdf') ?></a>
                </div>
            </div>
        </section>

        <main class="job-detail-shell job-detail-main">
            <article class="job-detail-card">
                <header class="job-detail-card-header">
                    <h2><?= lang('description') ?></h2>
                </header>
                <div class="job-detail-body">
                    <div class="job-detail-description">
                        <?= $circular_details->description ?>
                    </div>
                </div>
            </article>
        </main>
    </div>
<?php } ?>
