<?php
include("header.php"); 
if(isset($_POST['response_description'])){
    $id = $_POST['pass_id'];
    mysqli_query($conn,"UPDATE db_help_center SET response='".$_POST['response_description']."' WHERE id=".$id);
    $get_data = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM db_help_center WHERE id=".$id));
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $msg = "<b>Your Query :</b> ".$get_data['description']."<br><br> <b>Our Response :</b> ".$_POST['response_description']."<br>";
        
    //mail($get_data['email'],"Reponse of your submitted Query",$msg,$headers );
    send_zoho_mail($get_data['email'],"Reponse of your submitted Query",$msg);
}
?>
<style>
.card-body.timelog-body {
    padding: 0px !important;
}
table.manager_tbl tbody {
    border: 1px solid #cea254;
}
tr.top-bar-timelog th {
    color: #fff;
    padding: 10px 20px;
}
table.manager_tbl td {
    padding: 12px 20px;
}
tr.top-bar-timelog {
    background-color: #cea254;
}
table.manager_tbl tr:nth-child(even) {
    background-color: rgb(22 51 57 / 10%);
}
.pagination-div {
    margin-top: 20px;
}
button.prev-btn {
    border: 2px solid #000000;
    padding: 6px 35px;
    margin-right: 10px;
    border-radius: 5px;
    background-color: #fff;
    transition: all 0.4s ease-in-out;
    display: inline-block;
}
button.prev-btn:hover, button.next-btn:hover {
    background-color: #cea254;
    color: #fff;
}
button.next-btn {
    margin-left: 10px;
    padding: 6px 35px;
    border-radius: 5px;
    background-color: #fff;
    display: inline-block;
    transition: all 0.4s ease-in-out;
}
button.response_btn {
    padding: 5px 35px;
    border-radius: 5px;
    transition: all 0.4s ease-in-out;
}
button.response_btn:hover {
    background-color: #222529;
    color: #fff;
}
button.prev-btn:disabled {
    border: 2px solid #bcbcbc;
}

@media (max-width:1366px){
    table.manager_tbl {
    overflow-x: scroll;
    width: 100%;
    display: block;
}
}
</style>


<div class="body d-flex py-lg-3 py-md-2">
        <div class="container-xxl">
            <div class="row align-items-center">
                <div class="border-0 mb-4">
                    <div class="card-header py-3 no-bg bg-transparent d-flex align-items-center px-0 justify-content-between border-bottom flex-wrap">
                        <h3 class="h4 mb-0">Help Request</h3>
                           
                        
                            </div>
                        </div>
                    </div> <!-- Row end  -->
                    <div class="row clearfix g-3">
                    <div class="col-sm-12">
                        <div class="card mb-3">
                            <div class="card-body timelog-body">
                                <table class="manager_tbl" style="width: 100%;">
                                      <thead>
                                            <tr class="top-bar-timelog">
                                                <th>#</th>
                                                <th>Name</th>
                                                <th>Email</th>
                                                <th>Message</th>
                                                <th>Date</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $start = 0;
                                        $limit = 10;
                                        if(isset($_GET['page']) && !empty($_GET['page']) && $_GET['page'] > 1){
                                            $start = ($_GET['page']-1)*$limit;
                                        }
                                        $help_req = mysqli_query($conn, "SELECT * FROM db_help_center order by id desc limit $start, $limit");
                                    
                                        $help_req_all = mysqli_query($conn, "SELECT * FROM db_help_center order by id desc");
                                        if($help_req->num_rows > 0){
                                            $q = ($start+1);

                                            while($row = mysqli_fetch_assoc($help_req)){
                                                if(isset($_COOKIE['user_timezone'])){

                                                    $datetime = new DateTime();
                                                    $datetime->setTimestamp(strtotime($row['date_time']));
                                                    $la_time = new DateTimeZone($_COOKIE['user_timezone']);
                                                    $datetime->setTimezone($la_time);
                                                    $show_date = $datetime->format('Y-m-d h:i A');
                                                    
                                                } else {
                                                    $show_date = $row['date_time'];
                                                }

                                                $res_txt        = !empty($row['response']) ? "Responded" : "Respond";
                                                $res_disabled   = !empty($row['response']) ? "disabled" : "";
                                                
                                                echo "<tr><td>".$q."</td><td class='name-td'>".$row['name']."</td><td class='email-td'>".$row['email']."</td><td class='description-td'>".$row['description']."</td><td>".$show_date."</td><td><button class='response_btn' data-id='".$row['id']."' $res_disabled>$res_txt</button></td></tr>";
                                                $q++;
                                            }
                                        } else {
                                            echo "<tr><td colspan='5'>No Records Found.</td></tr>";
                                        }
                                        ?>
                                      </tbody>
                                </table>
                                <?php 
                                if($help_req_all->num_rows > $limit){

                                    $page_no = 1;
                                    if(isset($_GET['page']) && !empty($_GET['page'])){
                                        $page_no = $_GET['page'];
                                    }
                                    $disabled_prev = ($page_no == 1) ? "disabled" : ""; 
                                    $disabled_next = ($page_no == ceil($help_req_all->num_rows/$limit)) ? "disabled" : "";

                                    echo "<div class='pagination-div'><a href='?page=".($page_no-1)."' $disabled_prev><button class='prev-btn' $disabled_prev>Prev</button></a><span><b>$page_no</b></span> Out of <b>".ceil($help_req_all->num_rows/$limit)."</b><a href='?page=".($page_no+1)."' $disabled_next><button class='next-btn' $disabled_next>Next</button></a></div>";
                                } 
                                ?>

                            </div>
                        </div>
                    </div>
                    </div><!-- Row End -->
                </div>
            </div>

            <div class="modal fade" id="respond-model" tabindex="-1"  aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered modal-md modal-dialog-scrollable">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title  fw-bold" id="createprojectlLabel">Respond the Request</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form action="" method="post">
                        <div class="mb-3">
                            <label for="exampleFormControlInput77" class="form-label">Full Name</label>
                            <input type="text" class="form-control name_in_form" name="full_name" id="exampleFormControlInput77" placeholder="" required="" disabled="">
                        </div>
                        <div class="mb-3">
                            <label for="exampleFormControlInput77" class="form-label">Email</label>
                            <input type="text" class="form-control email_in_form" name="email" id="exampleFormControlInput77" placeholder="" required="" disabled="">
                        </div>
                        <div class="mb-3">
                            <label for="exampleFormControlInput77" class="form-label">Note</label>
                            <input type="text" class="form-control note_in_form" name="user_note" id="exampleFormControlInput77" placeholder="" required="" disabled="">
                            <input type="hidden" name="pass_id" class="pass_id" value="">
                        </div>
                        
                        <div class="mb-3">
                            <label for="exampleFormControlTextarea78" class="form-label">Response</label>
                            <textarea class="form-control" id="exampleFormControlTextarea78" name="response_description" rows="3" placeholder="" required=""></textarea>
                        </div>
                        <input type="submit" name="reponse_submit" class="reponse_submit" style="display:none;">
                        
                    </form>
                    </div>
                    <div class="modal-footer">
                        <!-- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Done</button> -->
                        <button type="button" class="btn btn-primary" onclick="$('.reponse_submit').click();">Send </button>
                        <button type="button" class="btn btn-primary clear-btn">Clear</button>
                    </div>
                </div>
                </div>
            </div>

<?php


include("footer.php"); 

?>
<script type="text/javascript">
    $(document).on("click", ".response_btn", function(){

            $("#respond-model").modal('show');
            var pass_id     = $(this).attr("data-id");
            var pass_email  = $(this).closest("tr").find('.email-td').text();
            var pass_name   = $(this).closest("tr").find('.name-td').text();
            var pass_desc   = $(this).closest("tr").find('.description-td').text();
            $(".email_in_form").val(pass_email);
            $(".name_in_form").val(pass_name);
            $(".note_in_form").val(pass_desc);
            $(".pass_id").val(pass_id);
    });
</script>