<?php

/**
 *
 * Copyright (C) 2007,2008  Arie Nugraha (dicarve@yahoo.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* Member Fines Report */

// key to authenticate
define('INDEX_AUTH', '1');

// main system configuration
require '../../../../sysconfig.inc.php';
// IP based access limitation
require LIB . 'ip_based_access.inc.php';
do_checkIP('smc');
do_checkIP('smc-reporting');
// start the session
require SB . 'admin/default/session.inc.php';
require SB . 'admin/default/session_check.inc.php';

// ========== TAMBAHAN MULTI-LIBRARY ==========
$current_library_id = isset($_SESSION['library_id']) ? (int)$_SESSION['library_id'] : 1;
$is_superadmin = isset($_SESSION['uid']) && $_SESSION['uid'] == 1;
// ========== AKHIR TAMBAHAN ==========

// privileges checking
$can_read = utility::havePrivilege('reporting', 'r');
$can_write = utility::havePrivilege('reporting', 'w');

if (!$can_read) {
    die('<div class="errorBox">' . __('You don\'t have enough privileges to access this area!') . '</div>');
}

require SIMBIO . 'simbio_GUI/table/simbio_table.inc.php';
require SIMBIO . 'simbio_GUI/form_maker/simbio_form_element.inc.php';
require SIMBIO . 'simbio_GUI/paging/simbio_paging.inc.php';
require SIMBIO . 'simbio_DB/datagrid/simbio_dbgrid.inc.php';
require MDLBS . 'reporting/report_dbgrid.inc.php';

$membershipTypes = membershipApi::getMembershipType($dbs);
$page_title = 'Members Fines Detail Report';
$reportView = false;
$num_recs_show = 20;
if (isset($_GET['reportView'])) {
    $reportView = true;
}

// ========== TAMBAHAN: Informasi Library di Header ==========
$library_info = '';
if (!$is_superadmin) {
    $lib_q = $dbs->query("SELECT library_name FROM mst_library WHERE library_id = " . $current_library_id);
    if ($lib_q->num_rows > 0) {
        $lib_d = $lib_q->fetch_assoc();
        $library_info = ' - ' . $lib_d['library_name'];
    }
}
// ========== AKHIR TAMBAHAN ==========

if (!$reportView) {
?>
    <div class="menuBox">
        <div class="menuBoxInner">
            <!-- filter -->
            <div class="per_title">
                <h2><?php echo __('Fines List by Member'); ?></h2>
            </div>
            <div class="infoBox">
                <?php echo __('Report Filter'); ?>
                <?php if (!$is_superadmin) { echo '<strong> - ' . $library_info . '</strong>'; } ?>
            </div>
            <div class="sub_section">
                <div>&nbsp;</div>
                <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" target="reportView">
                    <div id="filterForm">
                        <!-- ========== TAMBAHAN: Filter Library untuk Superadmin ========== -->
                        <?php if ($is_superadmin) { ?>
                        <div class="form-group divRow">
                            <div class="divRowLabel"><?php echo __('Library'); ?></div>
                            <div class="divRowContent">
                                <?php
                                $lib_q = $dbs->query('SELECT library_id, library_name FROM mst_library ORDER BY library_name');
                                $lib_options = array();
                                $lib_options[] = array('0', __('ALL Libraries'));
                                while ($lib_d = $lib_q->fetch_row()) {
                                    $lib_options[] = array($lib_d[0], $lib_d[1]);
                                }
                                echo simbio_form_element::selectList('library_id', $lib_options, '', 'style="width: 50%" class="form-control"');
                                ?>
                            </div>
                        </div>
                        <?php } ?>
                        <!-- ========== AKHIR TAMBAHAN ========== -->
                        <div class="form-group divRow">
                            <div class="divRowLabel"><?php echo __('Member ID') . '/' . __('Member Name'); ?></div>
                            <div class="divRowContent">
                                <?php
                                echo simbio_form_element::textField('text', 'id_name', '', 'style="width: 50%" class="form-control"');
                                ?>
                            </div>
                        </div>

                        <div class="form-group divRow">
                            <div class="divRowLabel"><?php echo __('Membership Type'); ?></div>
                            <div class="divRowContent">
                                <select name="membershipType" class="form-control col-3" style="width: 50%">
                                    <?php
                                    foreach ($membershipTypes as $key => $membershipType) {
                                        echo '<option value="' . $key . '">' . $membershipType['member_type_name'] . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="form-group divRow">
                            <div class="divRowLabel"><?php echo __('Fines Date'); ?></div>
                            <div class="divRowContent">
                                <div id="range">
                                    <input type="text" name="finesDateStart">
                                    <span><?= __('to') ?></span>
                                    <input type="text" name="finesDateEnd">
                                </div>
                            </div>
                        </div>
                        <div class="form-group form-group divRow">
                            <label><?php echo __('Record each page'); ?></label>
                            <input type="text" name="recsEachPage" size="3" maxlength="3" class="form-control col-1" value="<?php echo $num_recs_show; ?>" />
                            <small class="text-muted"><?php echo __('Set between 20 and 200'); ?></small>
                        </div>
                    </div>
                    <input type="button" class="s-btn btn btn-default" name="moreFilter" value="<?php echo __('Show More Filter Options'); ?>" />
                    <input type="submit" class="s-btn btn btn-primary" name="applyFilter" value="<?php echo __('Apply Filter'); ?>" />
                    <input type="hidden" name="reportView" value="true" />
                </form>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function() {
            const elem = document.getElementById('range');
            const dateRangePicker = new DateRangePicker(elem, {
                language: '<?= substr($sysconf['default_lang'], 0, 2) ?>',
                format: 'yyyy-mm-dd',
            });
        })
    </script>
    <!-- filter end -->
    <div class="paging-area">
        <div class="pt-3 pr-3" id="pagingBox"></div>
    </div>
    <iframe name="reportView" id="reportView" src="<?php echo $_SERVER['PHP_SELF'] . '?reportView=true'; ?>" frameborder="0" style="width: 100%; height: 500px;"></iframe>
<?php
} else {
    ob_start();
    
    // ========== PERBAIKAN: Table spec dengan JOIN library ==========
    // Gunakan subquery untuk mendapatkan member yang memiliki fines
    $table_spec = '(SELECT DISTINCT m.member_id, m.member_name, m.member_type_id 
                    FROM member m 
                    INNER JOIN fines f ON m.member_id = f.member_id 
                    WHERE 1=1) AS m';
    // ========== AKHIR PERBAIKAN ==========

    // create datagrid
    $reportgrid = new report_datagrid();
    $reportgrid->setSQLColumn('m.member_id AS \'' . __('Member ID') . '\'');
    $reportgrid->setSQLorder('m.member_id ASC');
    $reportgrid->sql_group_by = 'm.member_id';

    // ========== PERBAIKAN: Filter library_id ==========
    $fines_criteria = '1=1 ';
    
    // Filter Library untuk Superadmin
    if ($is_superadmin && isset($_GET['library_id']) && $_GET['library_id'] > 0) {
        $selected_library_id = (int)$_GET['library_id'];
        $fines_criteria .= " AND EXISTS (SELECT 1 FROM fines f WHERE f.member_id = m.member_id AND f.library_id = " . $selected_library_id . ")";
    } elseif (!$is_superadmin) {
        $fines_criteria .= " AND EXISTS (SELECT 1 FROM fines f WHERE f.member_id = m.member_id AND f.library_id = " . $current_library_id . ")";
    }
    // ========== AKHIR PERBAIKAN ==========
    
    // is there any search
    if (isset($_GET['id_name']) and $_GET['id_name']) {
        $keyword = $dbs->escape_string(trim($_GET['id_name']));
        $words = explode(' ', $keyword);
        if (count($words) > 1) {
            $concat_sql = ' (';
            foreach ($words as $word) {
                $concat_sql .= " (m.member_id LIKE '%$word%' OR m.member_name LIKE '%$word%') AND";
            }
            // remove the last AND
            $concat_sql = substr_replace($concat_sql, '', -3);
            $concat_sql .= ') ';
            $fines_criteria .= ' AND ' . $concat_sql;
        } else {
            $fines_criteria .= " AND (m.member_id LIKE '%$keyword%' OR m.member_name LIKE '%$keyword%')";
        }
    }

    if ((isset($_GET['membershipType'])) and ($_GET['membershipType'] != '0')) {
        $membershipType = (int)$_GET['membershipType'];
        $fines_criteria .= ' AND m.member_type_id=' . $membershipType;
    }

    // ========== TAMBAHAN: Filter tanggal di callback ==========
    $date_criteria = '';
    if (isset($_GET['finesDateStart']) && !empty($_GET['finesDateStart']) && isset($_GET['finesDateEnd']) && !empty($_GET['finesDateEnd'])) {
        $date_criteria = ' AND f.fines_date >=\'' . $dbs->escape_string($_GET['finesDateStart']) . '\' AND f.fines_date <=\'' . $dbs->escape_string($_GET['finesDateEnd']) . '\' ';
        $dateInput = '?finesDateStart=' . $_GET['finesDateStart'] . '&finesDateEnd=' . $_GET['finesDateEnd'];
    } else if (isset($_GET['singleDate']) && isset($_GET['finesDate'])) {
        $date_criteria = ' AND f.fines_date=\'' . $dbs->escape_string($_GET['finesDate']) . '\' ';
        $dateInput = '?finesDate=' . $_GET['finesDate'];
    } else {
        // Default: tampilkan semua data tanpa filter tanggal
        $date_criteria = '';
        $dateInput = '';
    }
    // ========== AKHIR TAMBAHAN ==========

    if (isset($_GET['recsEachPage'])) {
        $recsEachPage = (int)$_GET['recsEachPage'];
        $num_recs_show = ($recsEachPage >= 5 && $recsEachPage <= 200) ? $recsEachPage : $num_recs_show;
    }
    $reportgrid->setSQLCriteria($fines_criteria);

    // set table and table header attributes
    $reportgrid->table_attr = 'align="center" class="dataListPrinted" cellpadding="5" cellspacing="0"';
    $reportgrid->table_header_attr = 'class="dataListHeaderPrinted"';
    $reportgrid->column_width = array('1' => '80%');
    $reportgrid->show_spreadsheet_export = true;
    $reportgrid->spreadsheet_export_btn = '<a href="' . AWB . 'modules/reporting/customs/member_fines_list.csv.php' . simbio_security::xssFree($dateInput) . '" class="s-btn btn btn-default">' . __('Export to spreadsheet format') . '</a>';

    // ========== PERBAIKAN: Callback showFinesList ==========
    function showFinesList($obj_db, $array_data)
    {
        global $date_criteria, $current_library_id, $is_superadmin;
        
        // member name
        $member_q = $obj_db->query('SELECT member_name, member_email, member_phone FROM member WHERE member_id=\'' . $array_data[0] . '\'');
        $member_d = $member_q->fetch_row();
        $member_name = $member_d[0];
        unset($member_q);

        if (!isset($_SESSION['csvData'])) $_SESSION['csvData'] = [];

        // ========== PERBAIKAN: Query fines ==========
        $library_filter = '';
        
        // Filter Library untuk Superadmin
        if ($is_superadmin && isset($_GET['library_id']) && $_GET['library_id'] > 0) {
            $library_filter = " AND f.library_id = " . (int)$_GET['library_id'];
        } elseif (!$is_superadmin) {
            $library_filter = " AND f.library_id = " . $current_library_id;
        }
        
        $fines_q = $obj_db->query('SELECT f.debet, f.credit, f.discount_amount, f.description, f.fines_date, f.status, lib.library_name
            FROM fines AS f 
            LEFT JOIN mst_library AS lib ON f.library_id = lib.library_id
            WHERE f.member_id=\'' . $array_data[0] . '\' ' . $date_criteria . $library_filter);
        // ========== AKHIR PERBAIKAN ==========
        
        // Jika tidak ada data fines, tampilkan pesan
        if ($fines_q->num_rows == 0) {
            return '<div style="padding: 10px; color: #999;">' . __('No fines data found for this member') . '</div>';
        }
        
        $_buffer = '<div style="font-weight: bold; color: black; font-size: 10pt; margin-bottom: 3px; border-bottom:solid 5px #eaeaea;">' . $member_name . ' (' . $array_data[0] . ')</div>';

        $_SESSION['csvData'][$array_data[0]] = ['data' => $member_d, 'dateCriteria' => $date_criteria];

        $_buffer .= '<table width="100%" cellspacing="0">';
        $debet  = 0;
        $credit = 0;
        $discount = 0;
        
        $_buffer .= '<tr><td>' . __('Fines Date') . '</td><td width="25%"><b>' . __('Description') . '</b></td><td>' . __('Debit') . '</td><td>' . __('Credit') . '</td><td>' . __('Discount') . '</td><td>' . __('Library') . '</td><td>' . __('Status') . '</td></tr>';
        
        while ($fines_d = $fines_q->fetch_assoc()) {
            $status_badge = '';
            switch ($fines_d['status'] ?? 'unpaid') {
                case 'paid':
                    $status_badge = '<span style="color: green; font-weight: bold;">PAID</span>';
                    break;
                case 'partial':
                    $status_badge = '<span style="color: orange; font-weight: bold;">PARTIAL</span>';
                    break;
                case 'waived':
                    $status_badge = '<span style="color: blue; font-weight: bold;">WAIVED</span>';
                    break;
                default:
                    $status_badge = '<span style="color: red; font-weight: bold;">UNPAID</span>';
                    break;
            }
            
            $_buffer .= '<tr style="background-color:#adadad4d;">';
            $_buffer .= '<td>' . $fines_d['fines_date'] . '</td>';
            $_buffer .= '<td valign="top" width="25%">' . $fines_d['description'] . '</td>';
            $_buffer .= '<td>' . currency($fines_d['debet']) . '</td>';
            $_buffer .= '<td>' . currency($fines_d['credit']) . '</td>';
            $_buffer .= '<td>' . currency($fines_d['discount_amount'] ?? 0) . '</td>';
            $_buffer .= '<td>' . ($fines_d['library_name'] ?? '-') . '</td>';
            $_buffer .= '<td>' . $status_badge . '</td>';
            $_buffer .= '</tr>';
            
            $debet  = $debet + $fines_d['debet'];
            $credit = $credit + $fines_d['credit'];
            $discount = $discount + ($fines_d['discount_amount'] ?? 0);
        }
        
        $remaining = $debet - $credit - $discount;
        $clr = '#b9d2a5';
        if ($remaining > 0) {
            $clr = '#f9b8b8';
        }
        
        $_buffer .= '<tr style="border-top:solid 5px #eaeaea; font-weight:bold;background-color:' . $clr . ';">';
        $_buffer .= '<td colspan="2"><i>Total</i></td>';
        $_buffer .= '<td>' . currency($debet) . '</td>';
        $_buffer .= '<td>' . currency($credit) . '</td>';
        $_buffer .= '<td>' . currency($discount) . '</td>';
        $_buffer .= '<td colspan="2"><i>Remaining: ' . currency($remaining) . '</i></td>';
        $_buffer .= '</tr>';
        
        $_buffer .= '</table>';
        return $_buffer;
    }
    // ========== AKHIR CALLBACK ==========
    
    // modify column value
    $reportgrid->modifyColumnContent(0, 'callback{showFinesList}');

    // put the result into variables
    echo $reportgrid->createDataGrid($dbs, $table_spec, $num_recs_show);

    echo '<script type="text/javascript">' . "\n";
    echo 'parent.$(\'#pagingBox\').html(\'' . str_replace(array("\n", "\r", "\t"), '', $reportgrid->paging_set ?? '') . '\');' . "\n";
    echo '</script>';

    $content = ob_get_clean();
    // include the page template
    require SB . '/admin/' . $sysconf['admin_template']['dir'] . '/printed_page_tpl.php';
}