<?php $sentinel = app('Cartalyst\Sentinel\Native\Facades\Sentinel'); ?>

<?php $__env->startSection('css_page_level'); ?>
<link rel="stylesheet" type="text/css" href="<?php echo e(asset('assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css')); ?>"/>
<style type="text/css">

</style>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('page-content'); ?>
    <div class="wrap-fluid" id="paper-bg">
        <div class="row">
            <div class="col-lg-12">
                <div class="box">
                    <div class="box-header">
                        <h3 class="box-title" style="padding-bottom:0px;"><i class="fontello-doc"></i>
                            <span style="font-size:130%;">自助訪客申請紀錄</span>
                        </h3>
                    </div>
                    <div class="box-body">
                        <?php if($errors->any()): ?>
                            <div style="margin-bottom: 15px; padding: 12px; background-color: #f8d7da; border: 1px solid #f5c6cb; border-radius: 4px; color: #721c24;">
                                <strong>錯誤！</strong>
                                <?php foreach($errors->all() as $error): ?>
                                    <div><?php echo e($error); ?></div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                        <div style="margin-bottom: 20px; padding: 15px; background-color: #f5f5f5; border-radius: 4px;">
                            <form id="filter_form" method="GET" action="<?php echo e(route('website::guest::guest_list')); ?>" style="margin: 0;">
                                <div style="display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap;">
                                    <div style="flex: 1; min-width: 200px;">
                                        <label style="display: block; margin-bottom: 5px; font-weight: bold;">開始日期</label>
                                        <input type="date" id="start_date" name="start_date" class="form-control"
                                            value="<?php echo e(request('start_date', date('Y-m-d', strtotime('-30 days')))); ?>">
                                    </div>
                                    <div style="flex: 1; min-width: 200px;">
                                        <label style="display: block; margin-bottom: 5px; font-weight: bold;">結束日期</label>
                                        <input type="date" id="end_date" name="end_date" class="form-control"
                                            value="<?php echo e(request('end_date', date('Y-m-d', strtotime('+30 days')))); ?>">
                                    </div>
                                    <button type="submit" class="btn btn-primary" style="min-width: 100px;">查詢</button>
                                    <a href="<?php echo e(route('website::guest::guest_list')); ?>" class="btn btn-default" style="min-width: 100px;">重置</a>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="box-body" style="padding-top: 0;">
                        <table class="table table-striped table-bordered table-hover" id="table_data">
                            <thead>
                                <tr>
                                    <td>日期</td>
                                    <?php /* <?php if(Sentinel::permission()->counter == 'admin'): ?>
                                    <td>申請人</td>
                                    <?php endif; ?> */ ?>
                                    <td>公司</td>
                                    <td>姓名</td>
                                    <td>來訪時段</td>
                                    <td>報到時間</td>
                                    <td>離開時間</td>
                                    <td>來訪人數</td>
                                    <td>車牌號碼</td>
                                    <td>車位</td>
                                    <td>狀態</td>
                                    <td>動作</td>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($data as $one): ?>
                                <tr>
                                    <td>
                                        <?php echo e($one->reserve_date); ?>

                                    </td>
                                    <?php /* <?php if(Sentinel::permission()->counter == 'admin'): ?>
                                    <td>
                                        <?php echo e($one->user_name); ?>

                                    </td>
                                    <?php endif; ?> */ ?>
                                    <td>
                                        <?php echo e($one->com_name); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->guest_name); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->time_start); ?> - <?php echo e($one->time_end); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->checkin); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->checkout); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->amount); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->code); ?>

                                    </td>
                                    <td>
                                        <?php echo e($one->location); ?>

                                    </td>
                                    <td>
                                        <?php if($one->status == 0): ?>
                                        <label class="label label-success" style="font-size: 12px;">尚未來訪</label>
                                        <?php elseif($one->status == 1): ?>
                                        <label class="label label-info" style="font-size: 12px;">已報到</label>
                                        <?php elseif($one->status == 2): ?>
                                        <label class="label label-info" style="font-size: 12px;">已報到</label>
                                        <?php elseif($one->status == 3): ?>
                                        <label class="label label-success" style="font-size: 12px;">已離開</label>
                                        <?php elseif($one->status == 4): ?>
                                        <label class="label label-default" style="font-size: 12px;">已取消</label>
                                        <?php elseif($one->status == 5): ?>
                                        <label class="label label-info" style="font-size: 12px;">已報到</label>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if($one->status == 0): ?>
                                        <div class="btn-group">
                                            <button class="btn btn_action btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown" aria-expand ed="false">
                                            <?php echo app('translator')->get('messages.action'); ?> <i class="fa fa-angle-down"></i>
                                            </button>
                                            <ul class="dropdown-menu pull-right" role="menu">
                                                <li>
                                                    <a class="btn_cancel" uid="<?php echo e($one->id); ?>" href="javascript:;"> 取消</a>
                                                </li>
                                                <li>
                                                    <a class="btn_qrcode" qrcode="<?php echo e($one->QRCode); ?>" visitcode="<?php echo e($one->visitCode); ?>" href="javascript:;"> 訪客QRCode</a>
                                                </li>
                                            </ul>
                                        </div>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="modal_show_qrcode" tabindex="-1" role="basic" aria-hidden="true">
        <div class="modal-dialog modal-md">
            <div class="modal-content text-center">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h4 class="modal-title">訪客QRCode：速通碼<span id="qrcode-number"></span></h4>
                </div>
                <div class="modal-body">
                    <div id="qrcode-img"></div>
                </div>
            </div>
        </div>
    </div>

<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/datatables/media/js/jquery.dataTables.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/jquery.qrcode.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/global/plugins/qrcode/qrcode.js')); ?>"></script>
<script type="text/javascript">
    $(document).on('click','.btn_qrcode',function(){
        $('#qrcode-number').text(`${$(this).attr("visitcode")}`);
        // $('#qrcode-img').qrcode(`${$(this).attr("qrcode")}`);
        // 建立 QRCode
        var qr = qrcode(0, 'L');
        qr.addData(`${$(this).attr("qrcode")}`);
        qr.make();

        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');

        var cellSize = 10;
        var margin = 40;
        var size = qr.getModuleCount() * cellSize + margin * 2;

        canvas.width = size;
        canvas.height = size;

        ctx.fillStyle = "#ffffff";
        ctx.fillRect(0, 0, size, size);

        ctx.fillStyle = "#000000";
        for (var r = 0; r < qr.getModuleCount(); r++) {
            for (var c = 0; c < qr.getModuleCount(); c++) {
                if (qr.isDark(r, c)) {
                    ctx.fillRect(c * cellSize + margin, r * cellSize + margin, cellSize, cellSize);
                }
            }
        }

        document.getElementById('qrcode-img').innerHTML = '<img src="' + canvas.toDataURL("image/png") + '" style="max-width: 100%; height: auto;">';
        $('#modal_show_qrcode').modal('show');
    });
    
    $('#modal_show_qrcode').on('hidden.bs.modal', function(){
        // 避免QRCode點選不同視窗後重複出現
        $('#qrcode-img').remove();
        $('.modal-body').append( "<div id='qrcode-img'></div>" );

    });

    $(document).ready(function(){
        var table = $('#table_data');
        var oTable = table.dataTable({
            // Internationalisation. For more info refer to http://datatables.net/manual/i18n
            "language": {
                "aria": {
                    "sortAscending": ": activate to sort column ascending",
                    "sortDescending": ": activate to sort column descending"
                },
                "emptyTable": "<?php echo app('translator')->get('messages.datatable_no_data'); ?>",
                "info": "<?php echo app('translator')->get('messages.display_from'); ?> _START_ <?php echo app('translator')->get('messages.display_to'); ?> _END_ <?php echo app('translator')->get('messages.then_total'); ?> _TOTAL_ <?php echo app('translator')->get('messages.records'); ?>",
                "infoEmpty": "<?php echo app('translator')->get('messages.no_match'); ?>",
                "infoFiltered": "(<?php echo app('translator')->get('messages.from'); ?> _MAX_ <?php echo app('translator')->get('messages.records_filter'); ?>)",
                "lengthMenu": "<?php echo app('translator')->get('messages.show'); ?> _MENU_ <?php echo app('translator')->get('messages.records'); ?>",
                "search": "<?php echo app('translator')->get('messages.quick_filter'); ?>:",
                "zeroRecords": "<?php echo app('translator')->get('messages.no_data'); ?>",
                "paginate": {
                    "previous":"<?php echo app('translator')->get('messages.last_page'); ?>",
                    "next": "<?php echo app('translator')->get('messages.next_page'); ?>",
                    "last": "<?php echo app('translator')->get('messages.lastest'); ?>",
                    "first": "<?php echo app('translator')->get('messages.most_previous'); ?>"
                }
            },
            "aaSorting": [[0,'asc']],
            "lengthMenu": [
                [10, 20, 50, -1],
                [10, 20, 50, "All"] // change per page values here
            ],
            // set the initial value
            "pageLength": 10,            
            "autoWidth": false,
            "pagingType": "bootstrap_full_number",
        });
    });

    $(document).on('click','.btn_cancel',function(){
        var uid = $(this).attr('uid');
        $.ajax({
            type : 'POST',
            url  : "<?php echo e(route('website::guest::cancel_guest_apply')); ?>",
            dataType : 'json',
            data : {
                id     : uid,
                _token : '<?php echo e(csrf_token()); ?>',
            },
            success: function(sendback){
                toastr['success']('資料已送出');
                window.location.reload();
            },
            error: function(){
                toastr['error']("<?php echo app('translator')->get('messages.error_need_refresh'); ?>");
            }
        });
    });

    // Date filter form submission
    $('#filter_form').on('submit', function(e) {
        e.preventDefault();

        var startDate = document.getElementById('start_date').value;
        var endDate = document.getElementById('end_date').value;

        // Validate dates
        if (!startDate || !endDate) {
            toastr['error']('請輸入開始日期和結束日期');
            return false;
        }

        var startDateTime = new Date(startDate).getTime();
        var endDateTime = new Date(endDate).getTime();

        // Check if end date is before start date
        if (endDateTime < startDateTime) {
            toastr['error']('結束日期必須晚於開始日期');
            return false;
        }

        // Check if date range exceeds 60 days
        var daysDifference = Math.floor((endDateTime - startDateTime) / (1000 * 60 * 60 * 24));
        if (daysDifference > 60) {
            toastr['error']('查詢日期區間最多只能查詢60天內資料');
            return false;
        }

        // If validation passes, submit the form
        this.submit();
    });
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.front', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>