<?php
$title = 'Ticket';
// require_once('../admin/lib/basic-function.php');
// require_once('../admin/lib/user-functions.php');
// require_once('../admin/lib/investment-plan-functions.php');
// require_once('../admin/lib/package-functions.php');
// require_once('../admin/lib/currency-functions.php');
require_once('header-links.php');

$current_pg = 7;
$ticket_reply = array_reverse($basicFunctn->selectWhereDlt($basicFunctn->support_tb, 'main_id', $_GET['ticket']));
$tickets = $basicFunctn->selectWhere2CondDlt($basicFunctn->support_tb, 'user_id', $user_logged['unique_id'], 'main', 1);
$messages = $basicFunctn->selectWhereDlt($basicFunctn->support_tb, 'user_id', $user_logged['unique_id']);

// print_r($user_details);
// die();



?>

<body>

    <!--  BEGIN NAVBAR  -->
    <?php require_once('header.php'); ?>
    <!--  END NAVBAR  -->

    <!--  BEGIN MAIN CONTAINER  -->
    <div class="main-container" id="container">

        <div class="overlay"></div>
        <div class="search-overlay"></div>

        <!--  BEGIN SIDEBAR  -->
        <?php require_once('sidebar.php'); ?>
        <!--  END SIDEBAR  -->

        <!--  BEGIN CONTENT AREA  -->
        <div id="content" class="main-content">
            <div class="layout-px-spacing">

                <div class="row layout-spacing">

                    <div class="col-xl-8 col-lg-6 col-md-7 col-sm-12 layout-top-spacing">
                        <div class="widget-content widget-content-area">
                            <!-- <form id="contact" class="section contact support_form"> -->
                                <div class="info">
                                    <div class="row">
                                        <div class="col-md-11 mx-auto">
                                            <div class="row">
                                                <div class="col-md-12">

                                                    <div class="accordion" id="hd-statistics">
                                                        <div class="card">
                                                            <div class="card-header" id="hd-statistics-2">
                                                                <div class="mb-0">
                                                                    <div class=" collapsed" role="navigation" aria-expanded="true" aria-controls="collapse-hd-statistics-2">
                                                                        <h6>TICKET ID: <?php print $ticket_reply[0]['unique_id']; ?> </h6>
                                                                    </div>
                                                                    <div class=" collapsed text-capitalize" role="navigation" aria-expanded="true" aria-controls="collapse-hd-statistics-2">
                                                                        <h6>TITLE: <?php print $ticket_reply[0]['title']; ?> </h6>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div id="collapse-hd-statistics-2" class="collapse show" aria-labelledby="hd-statistics-2" data-parent="#hd-statistics">
                                                            <?php foreach ($ticket_reply as $e) {
                                                            $the_user = $basicFunctn->getDataDlt($e['user_id'], $basicFunctn->user_tb); ?>
                                                                <div class="card-body border mb-2">
                                                                    <p>Email: <?php print $the_user['email']; ?></p>
                                                                    <p class="text-capitalize"><?php print $e['message']; ?></p>
                                                                    <p>@<?php print $e['created_at']; ?> </p>
                                                                </div>
                                                            <?php } ?>
                                                            </div>
                                                        </div>

                                                    </div>
                                                </div>
                                                <div class="col-md-12">
                                                    <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>
                                                        <div class="form-group">
                                                            <label for="">Message<span class="text-danger">*</span> </label>
                                                            <textarea name="message" class="form-control" cols="30" rows="5"></textarea>
                                                        </div>
                                                    </form>
                                                </div>
                                                <div class="col-md-3 text-right mb-5">
                                                    <!-- <a href="support"> -->
                                                    <a href="support" class="btn btn-dark ">New Ticket</a>
                                                    <!-- </a> -->
                                                </div>
                                                <div class="col-md-3 text-right mb-5">
                                                    <button id="add-work-platforms" class="btn btn-secondary reply_support_btn">Reply Ticket</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <!-- </form> -->
                        </div>
                    </div>
                    <!-- Content -->
                    <div class="col-xl-4 col-lg-6 col-md-5 col-sm-12 layout-top-spacing">

                        <div class="user-profile layout-spacing">
                            <div class="widget-content widget-content-area">
                                <div class="table-responsive">
                                    <table class="table table-hover mb-4">
                                        <thead>
                                            <tr>
                                                <th>ALL OLD TICKETS</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            if (count($tickets) > 0) {
                                                $i = 1;
                                                foreach ($tickets as $e) {
                                            ?>
                                                    <tr>
                                                        <td>
                                                            <a href="support-ticket?ticket=<?php print $e['unique_id']; ?>">
                                                                <h6 class="text-capitalize"><?php print $e['title']; ?></h6>
                                                            </a>
                                                            <p style="font-size: 14px;" class="lead text-info">
                                                                <?php print $e['created_at']; ?></p>
                                                        </td>
                                                    </tr>
                                                <?php $i++;
                                                }
                                            } else { ?>
                                                <tr>
                                                    <td class="text-center"> No Records Found </td>
                                                </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                    </div>



                </div>
            </div>
            <?php require_once('footer.php'); ?>
        </div>
        <!--  END CONTENT AREA  -->
    </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, 'support-ticket?ticket='+reply_support_form[0].value, 1);
        });

        // });
    </script>