
<?php $__env->startSection('title','KM BackOffice'); ?>
<?php $__env->startSection('content'); ?>
<div class="main-content">
    <?php echo $__env->make('layouts/partials/back/header', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
    
    <main>
        <div class="banner">
            <span class="fas <?php echo e($page['data']['icon']); ?>"></span>
            <span><?php echo e($page['data']['name']); ?></span>
        </div>
        <div class="container" id="blade">
            <br>
            <div class="form-group">
                <!----------------- TAB ----------------->
                <ul class="nav navtab-pills" id="pills-tab" role="tablist">
                    <li class="nav-item" role="presentation">
                        <button class="navtab-link active" id="pills-question-tab" data-bs-toggle="pill"
                            data-bs-target="#pills-question" type="button" role="tab" aria-controls="pills-question"
                            aria-selected="true">
                            <i class="fa fa-question"></i>
                            question
                        </button>
                    </li>
                    <li class="nav-item" role="presentation">
                        <button class="navtab-link" id="pills-answer-tab" data-bs-toggle="pill"
                            data-bs-target="#pills-answer" type="button" role="tab" aria-controls="pills-answer"
                            aria-selected="false">
                            <i class="fas fa-comment"></i>
                            answer
                        </button>
                    </li>
                </ul>
                <!----------------- Content Tab ----------------->
                <div class="tab-content" id="pills-tabContent">
                    <div class="tab-pane fade show active" id="pills-question" role="tabpanel" aria-labelledby="pills-question-tab">
                        <div class="borders-green">
                            <div class="container" style="padding-top: 1rem">
                                <!----------------- data ----------------->
                                <div class="row">
                                    <div class="row justify-content-center">
                                        <div class="col-2">
                                            <button id="btn-add" ype=" button" class="btn btn-block btn-warning">
                                                ตั้งกระทู้ใหม่ <i class="fas fa-plus-circle"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>

                                <div class="table-responsive">
                                    <table id="datatable-list" class="table align-top" style="width:100%">
                                        <!-- Row of Column Name -->
                                        <thead>
                                            <tr class="bg-green">
                                                <th>ลำดับ</th>
                                                <th>ชื่อกระทู้</th>
                                                <th>ห้องความรู้</th>
                                                <th>ผู้ตั้งกระทู้</th>
                                                <th>จำนวนความคิดเห็น</th>
                                                <th>จำนวนผู้เข้าชม</th>
                                                <th>วันที่สร้าง</th>
                                                <th>สถานะการอนุมัติ</th>
                                                <th>สถานะการเผยแพร่</th>
                                                <th>id</th>
                                                <th>จัดการ</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php if($post['status'] == true): ?>
                                            <?php $__currentLoopData = $post['data']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr>
                                            <td width="50px"></td>
                                                <td width="300px"><?php echo e($item['topic']); ?></td>
                                                <td width="200px"><?php echo e($item['type']['name']); ?></td>
                                                <td width="150px"><?php echo e($item['user']['fname_th']); ?> <?php echo e($item['user']['lname_th']); ?></td>
                                                <td width="50px"><?php echo e($item['total_comment']); ?></td>
                                                <td width="50px"><?php echo e($item['visitor_count']); ?></td>
                                                <td width="100px"><?php echo e(formatDateThai($item['created_at'])); ?></td>
                                                <td width="100px">
                                                    <?php if($item['status'] == 1): ?>
                                                    <span style="color: SteelBlue;">
                                                        <i class="fa fa-circle"></i>
                                                    </span>
                                                    รออนุมัติ
                                                    <?php elseif($item['status'] == 2): ?>
                                                    <span style="color: YellowGreen;">
                                                        <i class="fa fa-circle"></i>
                                                    </span>
                                                    อนุมัติ
                                                    <?php elseif($item['status'] == 3): ?>
                                                    <span style="color: Tomato;">
                                                        <i class="fa fa-circle"></i>
                                                    </span>
                                                    ไม่อนุมัติ
                                                    <?php endif; ?>
                                                </td>
                                                <td width="100px">
                                                    <?php if($item['publish'] == 1): ?>
                                                    สาธารณะ
                                                    <?php elseif($item['publish'] == 2): ?>
                                                    เฉพาะสมาชิก
                                                    <?php elseif($item['publish'] == 3): ?>
                                                    ไม่เผยแพร่
                                                    <?php endif; ?>
                                                </td>
                                                <td width="10px"><?php echo e($item['id']); ?></td>
                                                <td width="100px"></td>
                                            </tr>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <?php else: ?>
                                            <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="pills-answer" role="tabpanel" aria-labelledby="pills-answer-tab">
                        <div class="borders-green">
                            <div class="container" style="padding-top: 1rem">
                                <!----------------- data ----------------->
                                <div class="table-responsive">
                                    <table id="datatable-ans" class="table align-top" style="width:100%">
                                        <!-- Row of Column Name -->
                                        <thead>
                                            <tr class="bg-green">
                                                <th>ลำดับ</th>
                                                <th>คำตอบ</th>
                                                <th>ชื่อกระทู้</th>
                                                <th>ผู้ตอบกระทู้</th>
                                                <th>วันที่สร้าง</th>
                                                <th>id</th>
                                                <th>จัดการ</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php if($comment['status'] == true): ?>
                                            <?php $__currentLoopData = $comment['data']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr>
                                                <td width="50px"></td>
                                                <td width="300px"><?php echo $item['detail']; ?></td>
                                                <td width="200px"><?php echo e($item['post']['topic']); ?></td>
                                                <td width="200px"><?php echo e($item['user']['fname_th']); ?> <?php echo e($item['user']['lname_th']); ?></td>
                                                <td width="100px"><?php echo e(formatDateThai($item['created_at'])); ?></td>                                                
                                                <td width="10px"><?php echo e($item['id']); ?></td>
                                                <td width="100px"></td>
                                            </tr>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <?php else: ?>
                                        <?php endif; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>

<script>
var tableList = $('#datatable-list').DataTable({
    "columnDefs": [{
            "targets": 0,
            "searchable": false,
            "orderable": false,
            "width": "5%",
            "className": "text-center",
        },
        {
            "targets": 9,
            "visible": false,
            "searchable": false
        },
        {
            "targets": -1,
            "data": null,
            "className": "text-center",
            "defaultContent": "<a class='on-default btn-edit' id='btn-edit' data-toggle='tooltip' title='แก้ไข'><i class='fa fa-edit'></i></a> " +
                "<a class='on-default btn-delete' id='btn-delete' data-toggle='tooltip' title='ลบ'><i class='fa fa-trash-alt'></i></a>"
        }
    ],
    "order": [
        [0, 'asc']
    ],
    "autoWidth": false,
    "searching": true,
    "destroy": false,
    "paging": true,
    "info": true,
    "responsive": false,
    "orderable": false,
    "fixedColumns": true
});

tableList.on('order.dt search.dt', function() {
    tableList.column(0, {
        search: 'applied',
        order: 'applied'
    }).nodes().each(function(cell, i) {
        cell.innerHTML = i + 1;
    });
}).draw();

$('#datatable-list tbody').on('click', '.btn-edit', function() {
    var RowIndex = $(this).closest('tr');
    var data = tableList.row(RowIndex).data();
    // var data = tableList.row( this ).data();
    var id = data[9];
    var form_data = new FormData();
    form_data.append('idpost', id);
    $.ajax({
        type: "post",
        url: 'q&aManagement/form',
        data: form_data,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        processData: false,
        contentType: false,
        cache: false,
        success: function(html) {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
            $('#blade').html(html);
        }
    });
});

$('#datatable-list tbody').on('click', '.btn-delete', function() {
    var RowIndex = $(this).closest('tr');
    var data = tableList.row(RowIndex).data();
    // var data = tableList.row( this ).data();
    var id = data[9];
    Swal.fire({
        icon: 'info',
        title: 'คุณแน่ใจแล้วใช่ไหมว่าจะลบข้อมูลที่เลือก',
        text: '',
        allowOutsideClick: false,
        focusConfirm: true,
        showCancelButton: true,
        confirmButtonText: 'ตกลง',
        cancelButtonText: 'ยกเลิก',
    }).then((result) => {
        if (result.value) {
            $(".loader").show();
            $.ajax({
                type: "post",
                url: 'posts/delete/' + id,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                processData: false,
                contentType: false,
                cache: false,
                success: function(data) {
                    $(".loader").hide();
                    if (data.status == true) {
                        location.reload();
                    } else {
                        Swal.fire({
                            icon: 'warning',
                            title: 'คำเตือน',
                            text: JSON.stringify(data.message),
                        });
                    }
                },
            });
        }
    });
});

$("#btn-add").click(function() {
    // console.log("click add");
    var form_data = new FormData();
    form_data.append('idpost', '0');
    $.ajax({
        type: "post",
        url: 'q&aManagement/form',
        data: form_data,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        processData: false,
        contentType: false,
        cache: false,
        success: function(html) {
            window.scrollTo({
                top: 0,
                behavior: 'smooth'
            });
            $('#blade').html(html);
        }
    });
});

var tableAns= $('#datatable-ans').DataTable({
    "columnDefs": [{
            "targets": 0,
            "searchable": false,
            "orderable": false,
            "width": "5%",
            "className": "text-center",
        },
        {
            "targets": 5,
            "visible": false,
            "searchable": false
        },
        {
            "targets": -1,
            "data": null,
            "className": "text-center",
            "defaultContent": "<a class='on-default btn-delete' id='btn-delete' data-toggle='tooltip' title='ลบ'><i class='fa fa-trash-alt'></i></a>"
        }
    ],
    "order": [
        [0, 'asc']
    ],
    "autoWidth": false,
    "searching": true,
    "destroy": false,
    "paging": true,
    "info": true,
    "responsive": true,
    "orderable": false,
    "fixedColumns": true
});

tableAns.on('order.dt search.dt', function() {
    tableAns.column(0, {
        search: 'applied',
        order: 'applied'
    }).nodes().each(function(cell, i) {
        cell.innerHTML = i + 1;
    });
}).draw();

$('#datatable-ans tbody').on('click', '.btn-edit', function() {
    // var RowIndex = $(this).closest('tr');
    // var data = tableAns.row(RowIndex).data();
    var data = tableAns.row( this ).data();
    var id = data[5];
    var form_data = new FormData();
    form_data.append('idans', id);
    // $.ajax({
    //     type: "post",
    //     url: 'posts/form',
    //     data: form_data,
    //     headers: {
    //         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    //     },
    //     processData: false,
    //     contentType: false,
    //     cache: false,
    //     success: function(html) {
    //         window.scrollTo({
    //             top: 0,
    //             behavior: 'smooth'
    //         });
    //         $('#blade').html(html);
    //     }
    // });
});

$('#datatable-ans tbody').on('click', '.btn-delete', function() {
    // var RowIndex = $(this).closest('tr');
    // var data = tableAns.row(RowIndex).data();
    var data = tableAns.row( this ).data();
    var id = data[5];
    // console.log(id);
    Swal.fire({
        icon: 'info',
        title: 'คุณแน่ใจแล้วใช่ไหมว่าจะลบข้อมูลที่เลือก',
        text: '',
        allowOutsideClick: false,
        focusConfirm: true,
        showCancelButton: true,
        confirmButtonText: 'ตกลง',
        cancelButtonText: 'ยกเลิก',
    }).then((result) => {
        if (result.value) {
            $(".loader").show();
            $.ajax({
                type: "post",
                url: 'posts/deleteAns/' + id,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                processData: false,
                contentType: false,
                cache: false,
                success: function(data) {
                    $(".loader").hide();
                    if (data.status == true) {
                        location.reload();
                    } else {
                        Swal.fire({
                            icon: 'warning',
                            title: 'คำเตือน',
                            text: JSON.stringify(data.message),
                        });
                    }
                },
            });
        }
    });
});
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.back.master', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /var/www/html/km-web/resources/views/backOffice/q&aManagement.blade.php ENDPATH**/ ?>