<?php

require_once "includes/inc_all_guest.php";

//Initialize the HTML Purifier to prevent XSS
require "../libs/htmlpurifier/HTMLPurifier.standalone.php";

$purifier_config = HTMLPurifier_Config::createDefault();
$purifier_config->set('Cache.DefinitionImpl', null); // Disable cache by setting a non-existent directory or an invalid one
$purifier_config->set('URI.AllowedSchemes', ['data' => true, 'src' => true, 'http' => true, 'https' => true]);
$purifier = new HTMLPurifier($purifier_config);

if (!isset($_GET['ticket_id'], $_GET['url_key'])) {
    echo "<br><h2>Oops, something went wrong! Please raise a ticket if you believe this is an error.</h2>";
    require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
    exit();
}

// Company info
$company_sql_row = mysqli_fetch_assoc(mysqli_query($mysqli, "
    SELECT
        company_phone,
        company_phone_country_code,
        company_website
    FROM
        companies,
        settings
    WHERE
        companies.company_id = settings.company_id
        AND companies.company_id = 1"
));

$company_phone_country_code = escapeHtml($company_sql_row['company_phone_country_code']);
$company_phone = escapeHtml(formatPhoneNumber($company_sql_row['company_phone'], $company_phone_country_code));
$company_website = escapeHtml($company_sql_row['company_website']);

$url_key = escapeSql($_GET['url_key']);
$ticket_id = intval($_GET['ticket_id']);

$ticket_sql = mysqli_query($mysqli,
    "SELECT * FROM tickets
            LEFT JOIN users on ticket_assigned_to = user_id
            LEFT JOIN ticket_statuses ON ticket_status = ticket_status_id
            WHERE ticket_id = $ticket_id AND ticket_url_key = '$url_key'"
);

if (mysqli_num_rows($ticket_sql) !== 1) {
    // Invalid invoice/key
    echo "<br><h2>Oops, something went wrong! Please raise a ticket if you believe this is an error.</h2>";
    require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';

    exit();
}

$ticket_row = mysqli_fetch_assoc($ticket_sql);

if ($ticket_row) {

    $ticket_prefix = escapeHtml($ticket_row['ticket_prefix']);
    $ticket_number = intval($ticket_row['ticket_number']);
    $ticket_status = escapeHtml($ticket_row['ticket_status_name']);
    $ticket_priority = escapeHtml($ticket_row['ticket_priority']);
    $ticket_subject = escapeHtml($ticket_row['ticket_subject']);
    $ticket_details = $purifier->purify($ticket_row['ticket_details']);
    $ticket_assigned_to = escapeHtml($ticket_row['user_name']);
    $ticket_resolved_at = escapeHtml($ticket_row['ticket_resolved_at']);
    $ticket_closed_at = escapeHtml($ticket_row['ticket_closed_at']);
    $ticket_feedback = escapeHtml($ticket_row['ticket_feedback']);

    ?>

    <div class="card mt-3">
        <div class="card-header bg-dark text-center">
            <h4 class="mt-1">
                Ticket <?= $ticket_prefix, $ticket_number ?>
            </h4>
        </div>

        <div class="card-body prettyContent">
            <h5><strong>Subject:</strong> <?= $ticket_subject ?></h5>
            <hr>
            <p>
                <strong>State:</strong> <?= $ticket_status ?>
                <br>
                <strong>Priority:</strong> <?= $ticket_priority ?>
                <br>
                <?php if (!empty($ticket_assigned_to) && empty($ticket_closed_at)) { ?>
                    <strong>Assigned to: </strong> <?= $ticket_assigned_to ?>
                <?php } ?>
            </p>
            <?= $ticket_details ?>
        </div>
    </div>

    <hr>

    <!-- Either show the reply comments box, option to re-open ticket, show ticket smiley feedback or thanks for feedback -->

    <?php if (empty($ticket_resolved_at)) { ?>
        <!-- Reply - guest users should email or login so we know exactly who replied -->
        <h6><i>Please <a href="../client">log in</a> or reply to the ticket via email to respond</i></h6>

    <?php } elseif (empty($ticket_closed_at)) { ?>
        <!-- Re-open -->

        <h4>Your ticket has been resolved</h4>

        <div class="col-4">
            <div class="row">
                <div class="col">
                    <a href="guest_post.php?reopen_ticket&ticket_id=<?= $ticket_id ?>&url_key=<?= $url_key ?>" class="btn btn-secondary btn-lg"><i class="fas fa-fw fa-redo text-white"></i> Reopen ticket</a>
                </div>

                <div class="col">
                    <a href="guest_post.php?close_ticket=&ticket_id=<?= $ticket_id ?>&url_key=<?= $url_key ?>" class="btn btn-success btn-lg"><i class="fas fa-fw fa-gavel text-white"></i> Close ticket</a>
                </div>
            </div>
        </div>
        <br>

    <?php } elseif (empty($ticket_feedback)) { ?>

        <h4>Ticket closed. Please rate your ticket</h4>

        <div class="col-4">
            <div class="row">
                <div class="col">
                    <a href="guest_post.php?add_ticket_feedback&ticket_id=<?= $ticket_id ?>&url_key=<?= $url_key ?>&feedback=Good" class="btn btn-success btn-lg"><i class="fas fa-fw fa-smile text-white"></i> Good</a>
                </div>

                <div class="col">
                    <a href="guest_post.php?add_ticket_feedback&ticket_id=<?= $ticket_id ?>&url_key=<?= $url_key ?>&feedback=Bad" class="btn btn-danger btn-lg"><i class="fas fa-fw fa-frown text-white"></i> Bad</a>
                </div>
            </div>
        </div>
        <br>

    <?php } else { ?>

        <h4>Rated <?= $ticket_feedback ?> -- Thanks for your feedback!</h4>

    <?php } ?>

    <!-- End comments/reopen/feedback -->

    <hr>
    <br>

    <?php
    $sql = mysqli_query($mysqli, "SELECT contact_name, contact_photo, ticket_reply, ticket_reply_by, ticket_reply_created_at,
        ticket_reply_id, ticket_reply_type, ticket_reply_updated_at, user_avatar, user_id,
        user_name FROM ticket_replies LEFT JOIN users ON ticket_reply_by = user_id LEFT JOIN contacts ON ticket_reply_by = contact_id WHERE ticket_reply_ticket_id = $ticket_id AND ticket_reply_archived_at IS NULL AND ticket_reply_type != 'Internal' ORDER BY ticket_reply_id DESC");

    while ($row = mysqli_fetch_assoc($sql)) {
        $ticket_reply_id = intval($row['ticket_reply_id']);
        $ticket_reply = $purifier->purify($row['ticket_reply']);
        $ticket_reply_created_at = escapeHtml($row['ticket_reply_created_at']);
        $ticket_reply_updated_at = escapeHtml($row['ticket_reply_updated_at']);
        $ticket_reply_by = intval($row['ticket_reply_by']);
        $ticket_reply_type = $row['ticket_reply_type'];

        if ($ticket_reply_type == "Client") {
            $ticket_reply_by_display = escapeHtml($row['contact_name']);
            $user_initials = initials($row['contact_name']);
            $user_avatar = $row['contact_photo'];
            $avatar_link = "../uploads/clients/$ticket_reply_by/$user_avatar";
        } else {
            $ticket_reply_by_display = escapeHtml($row['user_name']);
            $user_id = intval($row['user_id']);
            $user_avatar = $row['user_avatar'];
            $user_initials = initials($row['user_name']);
            $avatar_link = "../uploads/users/$user_id/$user_avatar";
        }
        ?>

        <div class="card card-outline <?php if ($ticket_reply_type == 'Client') { echo "card-warning"; } else { echo "card-info"; } ?> mb-3">
            <div class="card-header">
                <h3 class="card-title">
                    <div class="d-flex">
                        <?php
                        if (!empty($user_avatar)) {
                            ?>
                            <img src="<?= $avatar_link ?>" alt="User Avatar" class="img-size-50 me-3 rounded-circle">
                            <?php
                        } else {
                            ?>
                            <span class="fa-stack fa-2x">
                                    <i class="fa fa-circle fa-stack-2x text-secondary"></i>
                                    <span class="fa fa-stack-1x text-white"><?= $user_initials ?></span>
                                </span>
                            <?php
                        }
                        ?>

                        <div class="flex-grow-1">
                            <?= $ticket_reply_by_display ?>
                            <br>
                            <small class="text-muted"><?= $ticket_reply_created_at ?> <?php if (!empty($ticket_reply_updated_at)) { echo "(edited: $ticket_reply_updated_at)"; } ?></small>
                        </div>
                    </div>
                </h3>
            </div>

            <div class="card-body prettyContent">
                <?= $ticket_reply ?>
            </div>
        </div>

        <?php

    }

    ?>

    <script src="/js/pretty_content.js"></script>

    <?php } else {
        echo "Ticket ID not found!";
    } ?>

<div class="card-footer">
    <?= "<i class='fas fa-phone fa-fw me-2'></i>$company_phone | <i class='fas fa-globe fa-fw me-2 ms-2'></i>$company_website" ?>
</div>

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php';
