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

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

$uid = $_SESSION['ecimsuser'];
$error = '';

if(isset($_POST['save_feedback'])) {

    $ucid = (int)$_POST['ucid'];
    $attended_status = mysqli_real_escape_string($conn, $_POST['attended_status'] ?? '');
    $user_ratings = mysqli_real_escape_string($conn, $_POST['user_ratings'] ?? '');
    $user_feedback = mysqli_real_escape_string($conn, $_POST['user_feedback'] ?? '');

    if($attended_status == 'Complaint Not Attended') {
        $user_ratings = '';
    }

    $update = "
        UPDATE users_complaint SET
        attended_status='$attended_status',
        user_ratings='$user_ratings',
        user_feedback='$user_feedback',
        feedback_status='1',
        updated=NOW()
        WHERE ucid='$ucid' AND uid='$uid'
    ";

    if($conn->query($update)){
        header("Location: feedback-complaint.php?msg=success");
        exit;
    } else {
        $error = $conn->error;
    }
}

// $list = $conn->query("
//     SELECT uc.*, cl.details AS complaint_name
//     FROM users_complaint uc
//     LEFT JOIN complaint_list cl ON cl.cid = uc.c_list
//     WHERE uc.uid='$uid'
//     AND uc.c_status='2'
//     AND IFNULL(uc.feedback_status,0)='0'
//     ORDER BY uc.ucid DESC
// ");


$list = $conn->query("
    SELECT 
        uc.*,
        cl.details AS complaint_name,
        cl.node_id,
        cc.name AS category_name,
        cs.name AS subcategory_name
    FROM users_complaint uc
    LEFT JOIN complaint_list cl ON cl.cid = uc.c_list
    LEFT JOIN complaint_category cc ON cc.cc_id = cl.cc_id
    LEFT JOIN complaint_subcategory cs ON cs.cs_id = cl.cs_id
    WHERE uc.uid='$uid'
    AND uc.c_status='2'
    AND IFNULL(uc.feedback_status,0)='0'
    ORDER BY uc.ucid DESC
");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>ECIMS USER | FEEDBACK</title>
    <?php include('includes/head.php'); ?>

    <style>
        .alert-success {
            color: #000;
        }

        #ratingBox {
            display: none;
        }

        .status-radio-box {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
            margin-top: 8px;
        }

        .status-radio-box input[type="radio"] {
            display: none;
        }

        .status-radio-box label {
            padding: 10px 16px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            cursor: pointer;
            background: #fff;
            font-weight: 600;
            color: #333;
            transition: 0.2s ease;
        }

        .status-radio-box input[type="radio"]:checked + label {
            background: #198754;
            color: #fff;
            border-color: #198754;
            box-shadow: 0 4px 10px rgba(25, 135, 84, 0.25);
        }

        .rating-radio-box {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 10px;
            margin-top: 8px;
        }

        .rating-radio-box input[type="radio"] {
            display: none;
        }

        .rating-radio-box label {
            padding: 12px 14px;
            border: 1px solid #ddd;
            border-radius: 10px;
            background: #fff;
            cursor: pointer;
            text-align: center;
            font-weight: 600;
            transition: 0.2s ease;
        }

        .rating-radio-box input[type="radio"]:checked + label {
            background: #0d6efd;
            color: #fff;
            border-color: #0d6efd;
            box-shadow: 0 4px 10px rgba(13, 110, 253, 0.25);
        }

        @media(max-width: 576px) {
            .rating-radio-box {
                grid-template-columns: 1fr;
            }
        }
    </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">

                <h3 class="mb-4">Provide Feedback</h3>
                <a href="user-dashboard.php" class="btn btn-secondary btn-sm mb-3">Back</a>

                <?php if(isset($_GET['msg'])){ ?>
                    <div class="alert alert-success">Feedback submitted successfully.</div>
                <?php } ?>

                <?php if(!empty($error)){ ?>
                    <div class="alert alert-danger"><?= safe($error); ?></div>
                <?php } ?>

                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">

                            <table class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>Sr No.</th>
                                    <th>Complaint No.</th>
                                    <th>Date</th>
                                    <th>Details of Complaint</th>
                                    <th>Completion On</th>
                                    <th>Feedback</th>
                                </tr>
                                </thead>

                                <tbody>
                                <?php
                                if($list && $list->num_rows > 0) {
                                    $i = 1;
                                    while($row = $list->fetch_assoc()) {
                                ?>
                                    <tr>
                                        <td><?= $i++; ?></td>
                                        <td><?= safe($row['docketNumber']); ?></td>
                                        <td><?= safe($row['date']); ?></td>
                                        <td>
    <?= complaint_html(complaint_full_path($conn, ['node_id' => $row['node_id'], 'complaint_name' => $row['complaint_name'], 'category_name' => $row['category_name'], 'subcategory_name' => $row['subcategory_name']])); ?>
</td>
                                        <td><?= safe($row['completiondate']); ?></td>
                                        <td>
                                            <button type="button"
                                                    class="btn btn-primary btn-sm openFeedbackModal"
                                                    data-ucid="<?= (int)$row['ucid']; ?>">
                                                Give Feedback
                                            </button>
                                        </td>
                                    </tr>
                                <?php } } else { ?>
                                    <tr>
                                        <td colspan="6" class="text-center">No Feedback Pending Complaint Found</td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>

                        </div>
                    </div>
                </div>

            </div>

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

<div class="modal fade" id="feedbackModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

            <form method="post" action="">
                <div class="modal-header">
                    <h5 class="modal-title">Submit Feedback</h5>
                    <button type="button" class="close" data-dismiss="modal" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">

                    <input type="hidden" name="ucid" id="feedback_ucid" value="">

                    <div class="form-group mb-3">
                        <label><b>Complaint Status</b></label>

                        <div class="status-radio-box">
                            <input type="radio" id="not_attended" name="attended_status" value="Complaint Not Attended" required>
                            <label for="not_attended">Not Attended</label>

                            <input type="radio" id="attended" name="attended_status" value="Complaint Attended" required>
                            <label for="attended">Attended</label>
                        </div>
                    </div>

                    <div class="form-group mb-3" id="ratingBox">
                        <label><b>Select Rating</b></label>

                        <div class="rating-radio-box">
                            <input type="radio" id="rating5" name="user_ratings" value="5">
                            <label for="rating5">Excellent</label>

                            <input type="radio" id="rating4" name="user_ratings" value="4">
                            <label for="rating4">Very Good</label>

                            <input type="radio" id="rating3" name="user_ratings" value="3">
                            <label for="rating3">Good</label>

                            <input type="radio" id="rating2" name="user_ratings" value="2">
                            <label for="rating2">Satisfactory</label>

                            <input type="radio" id="rating1" name="user_ratings" value="1">
                            <label for="rating1">Poor</label>

                            <input type="radio" id="rating0" name="user_ratings" value="0">
                            <label for="rating0">Very Poor</label>
                        </div>
                    </div>

                    <div class="form-group mb-3">
                        <label><b>Remarks</b></label>
                        <textarea name="user_feedback" id="user_feedback" class="form-control" rows="3" required></textarea>
                    </div>

                </div>

                <div class="modal-footer">
                    <button type="submit" name="save_feedback" class="btn btn-success">Save Feedback</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal" data-bs-dismiss="modal">Close</button>
                </div>
            </form>

        </div>
    </div>
</div>

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

<script>
$(document).ready(function(){

    $(document).on('click', '.openFeedbackModal', function(){

        var ucid = $(this).data('ucid');

        $('#feedback_ucid').val(ucid);
        $('input[name="attended_status"]').prop('checked', false);
        $('input[name="user_ratings"]').prop('checked', false);
        $('#ratingBox').hide();
        $('input[name="user_ratings"]').removeAttr('required');
        $('#user_feedback').val('');

        $('#feedbackModal').modal('show');
    });

    $(document).on('change', 'input[name="attended_status"]', function(){

        if($(this).val() === 'Complaint Attended') {
            $('#ratingBox').slideDown();
            $('input[name="user_ratings"]').attr('required', true);
        } else {
            $('#ratingBox').slideUp();
            $('input[name="user_ratings"]').prop('checked', false);
            $('input[name="user_ratings"]').removeAttr('required');
        }

    });

});
</script>

</body>
</html>
