<?php 
include('usersession.php');

function safe($value) {
    return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}

function formatDateTime($date, $time = '') {
    if (empty($date)) return 'N/A';

    $timestamp = strtotime($date);
    if ($timestamp === false) {
        $timestamp = strtotime(str_replace('/', '-', $date));
    }

    if ($timestamp === false) return 'N/A';

    $output = date('d-m-y', $timestamp);

    if (!empty($time)) {
        $timeStamp = strtotime($time);
        if ($timeStamp !== false) {
            $output .= '<br>(' . date('H:i:s', $timeStamp) . ')';
        }
    }

    return $output;
}

function showStars($rating) {
    $rating = floatval($rating);
    $whole = intval($rating);
    $rem = 5 - $whole;
    $decimal = $rating - $whole;

    for ($i = 0; $i < $whole; $i++) {
        echo '<span class="star on"></span>';
    }

    if ($decimal > 0) {
        $rem--;
        echo '<span class="star half"></span>';
    }

    for ($i = 0; $i < $rem; $i++) {
        echo '<span class="star"></span>';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>ECIMS USER | ALL COMPLAINTS</title>
    <?php include('includes/head.php'); ?>

    <style>
        th, td {
            text-align: center;
            vertical-align: middle !important;
        }

        .table th {
            background: #1f2937;
            color: #fff;
            font-size: 14px;
            white-space: nowrap;
        }

        .table td {
            font-size: 14px;
        }

        .complaint-desc {
            text-align: left;
            white-space: normal;
            min-width: 250px;
        }

        .status-badge {
            padding: 6px 12px;
            border-radius: 20px;
            color: #fff;
            font-size: 13px;
            display: inline-block;
        }

        .bg-pending {
            background: #17a2b8;
        }

        .bg-confirmed {
            background: #ffc107;
            color: #000;
        }

        .bg-complete {
            background: #28a745;
        }

        .bg-cancelled {
            background: #dc3545;
        }

        .stars {
            display: inline-block;
        }

        .star {
            display: inline-block;
            width: 18px;
            height: 18px;
            background: #ccc;
            clip-path: polygon(
                50% 0%,
                61% 35%,
                98% 35%,
                68% 57%,
                79% 91%,
                50% 70%,
                21% 91%,
                32% 57%,
                2% 35%,
                39% 35%
            );
        }

        .star.on {
            background: #ffc107;
        }

        .star.half {
            background: linear-gradient(90deg, #ffc107 50%, #ccc 50%);
        }

        .feedback-text {
            font-size: 13px;
            margin-top: 4px;
            color: #444;
        }

        .remarks-text {
            white-space: normal;
            min-width: 180px;
        }
    </style>
</head>

<body>
<div class="container-scroller">

    <div class="horizontal-menu">
        <?php $activePage = basename($_SERVER['PHP_SELF'], ".php"); ?>

        <nav class="navbar top-navbar col-lg-12 col-12 p-0">
            <div class="container-fluid">
                <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-start">
                    <a class="navbar-brand brand-logo" href="index.php">
                        <img style="width:115px;height:auto;" src="assets/images/logo.png" alt="logo" />
                    </a>
                    <a class="navbar-brand brand-logo-mini" href="index.php">
                        <img style="width:115px;height:auto;" src="assets/images/logo.png" alt="logo" />
                    </a>
                </div>

                <div class="text-center navbar-menu-wrapper d-none d-lg-block d-flex align-items-center justify-content-center">
                    <span style="text-transform:uppercase;font-size:30px;font-weight:bold;color:#000;">
                        military engineer services <br> HISAR/ALWAR
                    </span>
                </div>

                <div class="navbar-brand-wrapper d-flex align-items-center justify-content-end">
                    <a class="navbar-brand brand-logo d-none d-lg-block" href="index.php">
                        <img style="width:115px;height:auto;" src="assets/images/logo-right.png" alt="logo" />
                    </a>
                    <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="horizontal-menu-toggle">
                        <i class="fa fa-bars text-white"></i>
                    </button>
                </div>
            </div>
        </nav>

        <p class="text-center p-0 m-1 fw-bolder">
            Login Date : <?php echo date('d/m/Y'); ?>
        </p>

        <nav class="bottom-navbar">
            <div class="container-fluid">
                <?php include('includes/nav.php'); ?>
            </div>
        </nav>
    </div>

    <div class="container-fluid page-body-wrapper">
        <div class="main-panel">
            <div class="content-wrapper">

                <div class="card grid-margin">
                    <div class="card-body">

                        <h4 class="card-title mb-4">My Complaint Register List</h4>

                        <div class="table-responsive">
                            <table class="table table-striped table-bordered" width="100%">
                                <thead>
                                    <tr>
                                        <th>Complaint No.</th>
                                        <th>Complaint Date</th>
                                        <th>Complaint Description</th>
                                        <th>Date of Completion</th>
                                        <th>Complaint Status</th>
                                        <th>Rating Awarded</th>
                                        <th>Remarks</th>
                                    </tr>
                                </thead>

                                <tbody>
                                <?php
                                $uid = $_SESSION['ecimsuser'];

                                $sql = "SELECT * FROM users_complaint 
                                        WHERE uid = '$uid' 
                                        ORDER BY ucid DESC";

                                $users_complaint = $conn->query($sql);

                                if ($users_complaint && $users_complaint->num_rows > 0) {
                                    while ($row = $users_complaint->fetch_assoc()) {

                                        if ($row['c_status'] == '0') {
                                            $statusClass = 'bg-pending';
                                            $statusText = 'Pending';
                                        } elseif ($row['c_status'] == '1') {
                                            $statusClass = 'bg-confirmed';
                                            $statusText = 'Confirmed';
                                        } elseif ($row['c_status'] == '2') {
                                            $statusClass = 'bg-complete';
                                            $statusText = 'Complete';
                                        } elseif ($row['c_status'] == '3') {
                                            $statusClass = 'bg-cancelled';
                                            $statusText = 'Cancelled';
                                        } else {
                                            $statusClass = 'bg-pending';
                                            $statusText = 'Pending';
                                        }

                                        $completionDate = 'N/A';
                                        if ($row['c_status'] == '2' && !empty($row['completiondate'])) {
                                            $completionDate = date('d-m-y H:i:s', strtotime($row['completiondate']));
                                        }
                                ?>
                                    <tr>
                                        <td>
                                            <?php echo safe($row['docketNumber']); ?>
                                        </td>

                                        <td>
                                            <?php echo formatDateTime($row['date'], $row['time']); ?>
                                        </td>

                                        <td class="complaint-desc">
                                            <?php echo nl2br(safe($row['details'])); ?>
                                        </td>

                                        <td>
                                            <?php echo $completionDate; ?>
                                        </td>

                                        <td>
                                            <span class="status-badge <?php echo $statusClass; ?>">
                                                <?php echo $statusText; ?>
                                            </span>
                                        </td>

                                        <td>
                                            <?php 
                                            if (!empty($row['user_ratings']) && $row['user_ratings'] > 0) {
                                                showStars($row['user_ratings']);
                                                echo '<div class="feedback-text">' . safe($row['user_feedback']) . '</div>';
                                            } else {
                                                echo 'N/A';
                                            }
                                            ?>
                                        </td>

                                        <td class="remarks-text">
                                            <?php 
                                            if (!empty($row['remarks'])) {
                                                echo nl2br(safe($row['remarks']));
                                            } else {
                                                echo 'N/A';
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                <?php 
                                    }
                                } else {
                                    echo '<tr><td colspan="7"><center>No Records Found!</center></td></tr>';
                                }
                                ?>
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>

            </div>

            <?php include('includes/footer.php'); ?>
        </div>
    </div>
</div>

<?php include('includes/foot.php'); ?>

</body>
</html>