<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 1);


$table_name = 'questions';
$redirection_page = "index.php?action=$page_name";
$action_name = "action=$page_name";
$type = $_REQUEST['type'];
$edit_id = $_REQUEST['edit_id'];
$delete_id = $_REQUEST['delete_id'];
$forward_id = $_REQUEST['forward_id'];


// For Submitting The Form


if (isset($_POST['submit'])) {
    $question_id = $_POST['question_id'];
    $expert_name = $_POST['expert_name'];

    $insert_bookings32 = "UPDATE `questions` SET
    expert_id   = '" . addslashes($expert_name) . "',   
    status   = 'Assigned To Expert'
    where id='" . $question_id . "'";
    $sql_insert32 = $dbconn->prepare($insert_bookings32);
    $sql_insert32->execute();
    $message = "Details Transfered updated.";
    $status = "success";
}


// For Displaying the table

if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
} else {
    $pageno = 1;
}
$no_of_records_per_page = 7;
$offset = ($pageno - 1) * $no_of_records_per_page;


$select_enquiry = "SELECT * FROM $table_name where status = 'Active' order by id desc";
$sql = $dbconn->prepare($select_enquiry);
$sql->execute();


$total_rows = $sql->fetchColumn();
$total_pages = ceil($total_rows / $no_of_records_per_page);
// $expert = $_GET['id'];
$select_enquiry = "SELECT * FROM $table_name where status = 'Assigned To Expert' AND expert_id = '$master_user_id' order by id desc LIMIT $offset, $no_of_records_per_page";
$sql = $dbconn->prepare($select_enquiry);
$sql->execute();
$wlvd = $sql->fetchAll(PDO::FETCH_OBJ);


$select_bookings = "SELECT * FROM `$table_name`";
$sql1 = $dbconn->prepare($select_bookings);
$sql1->execute();
$wlvd1 = $sql1->fetchAll(PDO::FETCH_OBJ);
foreach ($wlvd1 as $rows1);


?>


<div class="container-fluid">

    <div class="content-wrapper">
        <div class="container-fluid">
            <!-- Breadcrumb-->
            <div class="row pt-2 pb-2">
                <div class="col-sm-9">
                    <h4 class="page-title"><?php echo $page_name; ?></h4>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php?action=Welcome">Dashboard</a></li>

                        <li class="breadcrumb-item active" aria-current="page"><a href="index.php?action=<?php echo $page_name; ?>"><?php echo $page_name; ?></a></li>
                    </ol>
                </div>

                <div class="col-sm-3">
                    <?php include('plugins/alert.php'); ?>
                    <!-- Button trigger modal -->
                </div>

            </div>
            <!-- End Breadcrumb-->
            <?php if ($type == 'Delete') {

                $delete_statement = "UPDATE `$table_name` SET
                status   = 'Inactive'
                where id='" . $delete_id . "'";


                $sql_insert = $dbconn->prepare($delete_statement);
                $sql_insert->execute();


                $message = "Details successfully updated.";
                $status = "success";
                header("Location: $redirection_page&id=$delete_id");
            } else {
            ?>
                <div class="row">
                    <div class="col-lg-12 col-md-12">
                        <div class="card">
                            <div class="card-header"><i class="fa fa-user"></i> All Records</div>
                            <div class="card-body">
                                <div class="table-responsive">

                                    <table class="table table-hover" id="sample-table-1">
                                        <thead class="thead-dark">
                                            <tr>
                                                <th class="center"> ID</th>
                                                <th class="center"> Question Category</th>
                                                <th class="center"> Question Sub-Category</th>
                                                <th class="center"> Question Question Description</th>
                                                <th class="center"> Status</th>

                                                <th class="center"> Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php

                                            //while($rows = mysql_fetch_array($aResult,MYSQL_ASSOC))
                                            //{ 
                                            if ($sql->rowCount() > 0) {
                                                foreach ($wlvd as $rows) {
                                                    $field1 = $rows->id;
                                                    $field2 = $rows->question_cat;
                                                    $field3 = $rows->question_subcat;
                                                    $field4 = $rows->question_question_description;
                                                    $field5 = $rows->status;

                                            ?>
                                                    <tr>
                                                        <td class="center"><?php echo $field1; ?></td>
                                                        <td class="center"><?php echo $field2; ?></td>
                                                        <td class="center"><?php echo $field3; ?></td>
                                                        <td class="center">
                                                            <?php
                                                            
                                                                                                                       $advice = $field4;
                                                            $short_advice = substr($advice, 0, 50);
                                                            $showMoreLink = '<a href="index.php?action=Show_full_question&question=' . $field1 .'">... (Show More)</a>';
                                                            echo $short_advice . $showMoreLink;
                                                            ?>
                                                        
                                                        </td>
                                                        <td class="center"><?php echo $field5; ?></td>

                                                        <td class="center">
                                                            <a href="index.php?action=Expert-reply&edit_id=<?php echo $field1; ?>" target="_self"><i class="fa fa-reply"></i> Reply</a>
                                                            <a href="javascript:void(0);" onclick="confirmDelete(<?php echo $field1; ?>)"><i class="fa fa-trash"></i> Delete</a>
                                                
                                        </td>
                                    </tr>            
                                 </div>

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

                </div>


        <?php }
                                            } ?>
        </tbody>
        </table>

        <?php include('plugins/pagination.php'); ?>

        </div>

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

<?php
            };
?>


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


</div><!--End content-wrapper-->
<!--Start Back To Top Button-->
<a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>
<!--End Back To Top Button-->
<script>
function confirmDelete(id) {
    if (confirm("Are you sure you want to delete this record?")) {        
        window.location.href = "index.php?action=<?php echo $page_name; ?>&delete_id=" + id + "&type=Delete";
    }
}
</script>