

<?php $__env->startSection('content'); ?>
    <?php echo $__env->make('components.notification', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

    <form action="">
        <div class="row">
            <div class="col-md-3">
                <label for="">Dari Tanggal</label>
                
                <input type="date" name="dari" id="" class="form-control"
                    value="<?php echo e(request()->get('dari', date('Y-m-d'))); ?>">
            </div>
            <div class="col-md-3">
                <label for="">Sampai Tanggal</label>
                
                <input type="date" name="sampai" id="" class="form-control"
                    value="<?php echo e(request()->get('sampai', date('Y-m-d'))); ?>">
            </div>
            <div class="col-md-3">
                <label for="">Cabang</label>
                <select class='form-control select2' name='branch'> 
                    <option value=''>Semua Cabang</option>
                    <?php $__currentLoopData = $branch; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value='<?php echo e($item->id); ?>' <?php echo e(request()->get('branch') == $item->id ? 'selected' : ''); ?>>
                            <?php echo e($item->cabang); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div class="col-md-3">
                <label for="">Tipe Member</label>
                
                <select class='form-control select2' name='kategori_member' id="kategori_member_select">
                    <option value=''>Semua Kategori</option>
                    <option value="komunitas" <?php echo e(request()->get('kategori_member') == 'komunitas' ? 'selected' : ''); ?>>
                        Komunitas</option>
                    <option value="universitas" <?php echo e(request()->get('kategori_member') == 'universitas' ? 'selected' : ''); ?>>
                        Universitas</option>
                    <option value="corporate" <?php echo e(request()->get('kategori_member') == 'corporate' ? 'selected' : ''); ?>>
                        Corporate</option>
                    <option value="sekolah" <?php echo e(request()->get('kategori_member') == 'sekolah' ? 'selected' : ''); ?>>Sekolah
                    </option>
                </select>
            </div>
        </div>

        
        

        <div class="row mt-3">
            <div class="col">
                <button type="submit" class="btn btn-primary"><span class="fa fa-filter"></span> Filter</button>
                
                <?php if(request()->has('dari')): ?>
                    
                    <a href="<?php echo e(url('transaction/redeem/export?dari=' . request()->get('dari') . '&sampai=' . request()->get('sampai') . '&branch=' . request()->get('branch', '') . '&kategori_member=' . request()->get('kategori_member', '') . '&nama_komunitas=' . request()->get('nama_komunitas', '') . '&nama_universitas=' . request()->get('nama_universitas', '') . '&nama_himpunan=' . request()->get('nama_himpunan', '') . '&nama_perusahaan=' . request()->get('nama_perusahaan', '') . '&nama_sekolah=' . request()->get('nama_sekolah', ''))); ?>"
                        class="btn btn-success"><span class="fa fa-file-excel"></span> Export</a>
                <?php endif; ?>
            </div>
        </div>
    </form>

    <br>
    
    <?php if(request()->has('dari')): ?>
        <div class="table-responsive">
            <table class="table table-hover myTable">
                <thead>
                    <tr class="table-primary">
                        <th class="text-center">#</th>
                        <th>No Nota</th>
                        <th>Nama Member</th>
                        <th>Tanggal Transaksi</th>
                        <th>Cabang</th>
                        <th>Kode Voucher</th>
                        <th>Nominal Voucher</th>
                        <th>Nominal Transaksi (Before PB1 10%)</th>
                        
                        <th>Keterangan</th> 
                        <th>Tipe Member</th> 
                        <th>Detail Tipe Member</th> 
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    
                </tbody>
            </table>
        </div>
    <?php endif; ?>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('custom-script'); ?>
    
    <?php if(request()->has('dari')): ?>
        <script type="text/javascript">
            $(function() {
                // Build the AJAX URL safely by checking for each parameter
                let ajaxUrl = "<?= url('redeem') ?>?"; // Ensure this URL points to your redeem method
                const params = {
                    dari: "<?= request()->get('dari', date('Y-m-d')) ?>",
                    sampai: "<?= request()->get('sampai', date('Y-m-d')) ?>",
                    branch: "<?= request()->get('branch', '') ?>", // Use 'branch' for consistency
                    kategori_member: "<?= request()->get('kategori_member', '') ?>",
                    nama_komunitas: "<?= request()->get('nama_komunitas', '') ?>",
                    nama_universitas: "<?= request()->get('nama_universitas', '') ?>",
                    nama_himpunan: "<?= request()->get('nama_himpunan', '') ?>",
                    nama_perusahaan: "<?= request()->get('nama_perusahaan', '') ?>",
                    nama_sekolah: "<?= request()->get('nama_sekolah', '') ?>",
                };

                // Filter out empty parameters and build the query string
                const queryStrings = Object.keys(params)
                    .filter(key => params[key] !== '') // Only include non-empty parameters
                    .map(key => `${key}=${params[key]}`)
                    .join('&');

                ajaxUrl += queryStrings;

                var table = $('.myTable').DataTable({
                    processing: true,
                    serverSide: true,
                    orderable: false,
                    ajax: ajaxUrl, // Use the safely constructed URL
                    columns: [{
                            data: 'DT_RowIndex',
                            name: 'DT_RowIndex',
                            className: 'text-center'
                        }, // #
                        {
                            data: 'trx_code',
                            name: 'trx_code'
                        }, // No Nota
                        {
                            data: 'nama',
                            name: 'customer.nama'
                        }, // Nama Member
                        {
                            data: 'tgl_trx',
                            name: 'tgl_trx',
                            render: function(data, type, row) {
                                return indoDate(data);
                            }
                        }, // Tanggal Transaksi
                        {
                            data: 'cabang',
                            name: 'branch.cabang',
                            render: function(data, type, row) {
                                return data === '' || data === null ? 'Semua Cabang' : data;
                            }
                        }, // Cabang
                        {
                            data: 'voucher_code',
                            name: 'voucher_code',
                            render: function(data, type, row) {
                                return data === '' || data === null ? '-' : data;
                            }
                        }, // Kode Voucher
                        {
                            data: 'voucher_price',
                            name: 'voucher_price',
                            render: function(data, type, row) {
                                return row.voucher_code === '' || row.voucher_code === null ? '-' :
                                    new Intl.NumberFormat("id-ID", {
                                        style: "currency",
                                        currency: "IDR"
                                    }).format(data);
                            }
                        }, // Nominal Voucher
                        {
                            data: 'amount',
                            name: 'amount',
                            render: function(data, type, row) {
                                return new Intl.NumberFormat("id-ID", {
                                    style: "currency",
                                    currency: "IDR"
                                }).format(data);
                            }
                        }, // Nominal Transaksi (Before PB1 10%)
                        // { data: 'poin_earned', name: 'poin_earned' }, // Poin Didapat (commented out in HTML and here)
                        {
                            data: 'tipe',
                            name: 'tipe'
                        }, // Keterangan (from DB::raw in controller)
                        {
                            data: 'kategori_member',
                            name: 'customer.kategori_member',
                            orderable: false,
                            searchable: false
                        }, // Tipe Member
                        {
                            data: 'specific_member_data',
                            name: 'specific_member_data',
                            orderable: false,
                            searchable: false
                        }, // Detail Tipe Member
                        {
                            data: 'action',
                            name: 'action',
                            orderable: false,
                            searchable: false
                        } // Aksi
                    ],
                    order: [
                        [0, 'asc']
                    ],
                });
            });

            function indoDate(newDate) {
                const today = new Date(newDate);
                const yyyy = today.getFullYear();
                let mm = today.getMonth() + 1; // Months start at 0!
                let dd = today.getDate();
                let hh = newDate.substr(11, 2);
                let ii = newDate.substr(14, 2);

                if (dd < 10) dd = '0' + dd;
                if (mm < 10) mm = '0' + mm;

                const formattedToday = dd + '-' + mm + '-' + yyyy + ' ' + hh + ':' + ii;
                return formattedToday
            }
        </script>
    <?php endif; ?> 

    
    <script type="text/javascript">
        $(document).ready(function() {
            function toggleMemberFields() {
                const selectedType = $('#kategori_member_select').val();

                // Hide all specific member fields first using a combined selector
                $('#komunitas_field, #universitas_field, #himpunan_field, #corporate_field, #sekolah_field').hide();

                // Show fields based on the selected type
                if (selectedType === 'komunitas') {
                    $('#komunitas_field').show();
                } else if (selectedType === 'universitas') {
                    $('#universitas_field').show();
                    $('#himpunan_field').show(); // Himpunan is also related to Universitas
                } else if (selectedType === 'corporate') {
                    $('#corporate_field').show();
                } else if (selectedType === 'sekolah') {
                    $('#sekolah_field').show();
                }
            }

            // Call the function on page load to set initial state based on request parameters
            toggleMemberFields();

            // Attach change event listener to the select box
            $('#kategori_member_select').change(function() {
                toggleMemberFields();
            });
        });
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.template', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\member_new\resources\views/transaction/redeem.blade.php ENDPATH**/ ?>