<?php
$title = 'Reply Tickets';
require_once('header-links.php');
// require_once('../lib/basic-function.php');
// require_once('../lib/user-functions.php');
// require_once('../lib/investment-plan-functions.php');
// require_once('../lib/package-functions.php');
// require_once('../lib/currency-functions.php');
$all_tickets = $basicFunctn->selectWhereDlt($basicFunctn->support_tb, 'main', 1);
$ticket_reply = array_reverse($basicFunctn->selectWhereDlt($basicFunctn->support_tb, 'main_id', $_GET['ticket']));
$the_sender = $basicFunctn->getDataDlt($all_tickets[0]['user_id'], $basicFunctn->user_tb); ?>
?>

<body>
    <div class="app">
        <div class="layout">
            <?php require_once('sidebar.php'); ?>

            <!-- Page Container START -->
            <div class="page-container">
                <?php require_once('header.php'); ?>



                <!-- Content Wrapper START -->
                <div class="main-content">
                    <div class="container-fluid">
                        <div class="page-title">
                            <h4><?php print $title; ?></h4>
                        </div>
                        <div class="row">
                            <div class="col-lg-12">
                                <div class="card-block">
                                    <div style="width:60%;float:left;background-color:white;border:1px solid #e6ecf5;" class="col-lg-6">
                                            <div class="col-md-8">
                                                <p class="mrg-top-10 text-dark"> <b>
                                                        TICKET ID: <?php print $ticket_reply[0]['unique_id']; ?><br>
                                                        EMAIL: <?php print $the_sender['email']; ?><br>
                                                        TITLE: <?php print ucwords($ticket_reply[0]['title']); ?>
                                                    </b></p>
                                            </div>
                                            <hr>
                                            <?php foreach ($ticket_reply as $e) { ?>
                                                <div style="padding:10px 20px ;" class="card col-md-12">
                                                    <?php if($e['user_id'] !== 'ADMIN'){ 
                                                        $the_user = $basicFunctn->getDataDlt($e['user_id'], $basicFunctn->user_tb); ?>
                                                        Sender:&nbsp;<?php print $the_user['username']; ?><br>
                                                    <?php }else{ ?>
                                                        Sender:&nbsp;Administrator<br>
                                                    <?php } ?>
                                                    <p class="text-capitalize">
                                                    <?php print $e['message']; ?> <br>
                                                    </p>
                                                    Sent:&nbsp;<?php print $e['created_at']; ?>
                                                </div><br>
                                            <?php } ?>
                                        <form  class="reply_support_form">
                                            <input type="hidden" name="main_id" value="<?php print $_GET['ticket'] ; ?>"></input>
                                            <input type="hidden" name="reply_ticket_title" value="<?php print $ticket_reply[0]['title'] ; ?>"></input>
                                            <input type="hidden" name="user_id" value="<?php print $the_sender['unique_id'] ; ?>"></input>
                                            <div class="form-group col-lg-12">
                                                <textarea name="message" class="form-control" placeholder="Message"></textarea>
                                            </div>
                                        </form>
                                            <div class="  col-md-6">
                                                <a href="ticket" style="float:left;background-color:white;color:#515365;border:1px solid #515365;" class="btn btn-primary">Close Ticket
                                                </a>
                                            </div>
                                            <div style="float:right;" class=" text-right col-md-6">
                                                <button type="submit" name="a-reply" class="btn btn-primary reply_support_btn">Reply</button>
                                            </div>
                                    </div>
                                    <div class="col-md-8" style="float:right;width:40% !important;">
                                        <div class="card">
                                            <div class="card-heading border bottom">
                                                <h4 class=""><strong>ALL TICKETS</strong></h4>
                                            </div>
                                            <?php foreach ($all_tickets as $e) { ?>
                                                <div class="row">
                                                    <div style="padding:0px 20px ;" class="col-md-12">
                                                        <a style="text-decoration:underline;" href="reply-ticket.php?ticket=<?php print $e['unique_id']; ?>" class="text-dark text-capitalize">
                                                            <b><?php print strtolower($e['title']); ?></b>
                                                        </a>
                                                        <p class="text-right mt-4 "><?php print $e['created_at']; ?></p>
                                                    </div>
                                                </div>
                                                <hr>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Content Wrapper END -->
                <?php require_once('footer.php'); ?>
                <!-- Footer END -->

            </div>
            <!-- Page Container END -->

        </div>
    </div>

    <?php require_once('footer-links.php'); ?>
    <script>
        $('.reply_support_btn').click(async function(e) {
            e.preventDefault();
            let reply_support_form = $('.reply_support_form').serializeArray();
            // console.log(reply_support_form);return;
            let form_data = set_form_data(reply_support_form);
            let returned = await ajaxRequest('ajax.php', form_data);
            // console.log(returned);
            // return;
            throwSweetalert(returned, 'reply-ticket?ticket='+reply_support_form[0].value, 1);
        });
    </script>