<?php
/**
 * Copyright (C) 2009  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 3 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
 *
 */

/* Item Import section */
use SLiMS\Filesystems\Storage;
use SLiMS\DB;
use SLiMS\Csv\Writer;
use SLiMS\Csv\Reader;
use SLiMS\Csv\Row;
use SLiMS\Debug\VarDumper;

// key to authenticate
define('INDEX_AUTH', '1');
// key to get full database access
define('DB_ACCESS', 'fa');

// main system configuration
require '../../../sysconfig.inc.php';
// IP based access limitation
require LIB.'ip_based_access.inc.php';
do_checkIP('smc');
do_checkIP('smc-bibliography');
// start the session
require SB.'admin/default/session.inc.php';
require SIMBIO.'simbio_GUI/table/simbio_table.inc.php';
require SIMBIO.'simbio_GUI/form_maker/simbio_form_table_AJAX.inc.php';
require SIMBIO.'simbio_FILE/simbio_file_upload.inc.php';
require __DIR__ . '/biblio_utils.inc.php';

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

if (!$can_read) {
    die('<div class="errorBox">'.__('You are not authorized to view this section').'</div>');
}
# CHECK ACCESS
if ($_SESSION['uid'] != 1) {
    if (!utility::haveAccess('bibliography.biblio-item-import')) {
        die('<div class="errorBox">' . __('You are not authorized to view this section') . '</div>');
    }
}

// ========== 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 ==========

if (isset($_GET['action']) && $_GET['action'] === 'download_sample')
{
  // ========== PERBAIKAN: Sample dengan library_id ==========
  $csv = new Writer;
  $csv->add(new Row([
    'item_code','call_number','coll_type_name','inventory_code',
    'received_date','supplier_name','order_no','location_name',
    'order_date','item_status_name','site','source','invoice',
    'price','price_currency','invoice_date','input_date','last_update','title'
  ]));
  // ========== AKHIR PERBAIKAN ==========

  // Download CSV
  $csv->download('biblio_item_sample_import');
}

// max chars in line for file operations
$max_chars = 1024*100;

if (isset($_POST['doImport'])) {
    if ( empty($_FILES['importFile']['name']) && !isset($_SESSION['csv']['name']) ) {  
        utility::jsToastr(__('Import Tool'), __('No CSV file selected to import, please choose CSV file first!'), 'error');
        exit();        
    }

     // create upload object
    $files_disk = Storage::files();

    // ========== TAMBAHAN: Ambil library_id dari POST ==========
    if ($is_superadmin && isset($_POST['library_id']) && $_POST['library_id'] > 0) {
        $import_library_id = (int)$_POST['library_id'];
    } else {
        $import_library_id = $current_library_id;
    }
    $_SESSION['csv']['library_id'] = $import_library_id;
    // ========== AKHIR TAMBAHAN ==========
    
    // check for form validity
    if (!isset($_POST['process'])) {
        if (empty($_POST['fieldSep']) OR empty($_POST['fieldEnc'])) {
            utility::jsToastr('Item Import', __('Required fields (*)  must be filled correctly!'), 'error');
            exit;
        }

        // get system temporary directory location
        $_SESSION['csv'] = [];
        $_SESSION['csv']['name'] = md5($_FILES['importFile']['name'] . date('this'));
        
        if (!$files_disk->isExists('temp')) $files_disk->makeDirectory('temp');

        if ($files_disk->isExists('temp' . DS . $_SESSION['csv']['name'])) {
        $files_disk->delete('temp' . DS . $_SESSION['csv']['name']);
        }
        
        // set csv format
        $_SESSION['csv']['format'] = [
        'recordNum' => intval($_POST['recordNum']),
        'fieldEnc' => trim($_POST['fieldEnc']),
        'fieldSep' => trim($_POST['fieldSep']),
        'recordOffset' => intval($_POST['recordOffset'])
        ];

        $_SESSION['csv']['section'] = 'item';
        $_SESSION['csv']['action'] = $_SERVER['PHP_SELF'];

        // ========== TAMBAHAN: Simpan library_id di session ==========
        $_SESSION['csv']['library_id'] = $import_library_id;
        // ========== AKHIR TAMBAHAN ==========

        // create upload object
        $csv_upload = $files_disk->upload('importFile', function($files) use($sysconf) {
        // Extension check
        $files->isExtensionAllowed(['.csv']);

        // File size check
        $files->isLimitExceeded($sysconf['max_upload']*1024);

        // destroy it if failed
        if (!empty($files->getError())) $files->destroyIfFailed();

        })->as('temp' . DS . $_SESSION['csv']['name']);
        
        if (!$csv_upload->getUploadStatus())
        {
            toastr(__('Upload failed! File type not allowed or the size is more than').($sysconf['max_upload']/1024).' MB')->error(__('Import Tool'));
            exit;
        }

        // Redirect content
        redirect()->simbioAJAX(MWB . 'bibliography/import_preview.php');
    } else {
        $start_time = time();
        $row_count = 0;
        // check for import setting
        $record_num = intval($_SESSION['csv']['format']['recordNum']);
        $field_enc = trim($_SESSION['csv']['format']['fieldEnc']);
        $field_sep = trim($_SESSION['csv']['format']['fieldSep']);
        $record_offset = intval($_SESSION['csv']['format']['recordOffset']);
        $record_offset = $record_offset-1;
        // get current datetime
        $curr_datetime = date('Y-m-d H:i:s');
        $curr_datetime = '\''.$curr_datetime.'\'';
        // foreign key id cache
        $ct_id_cache = array();
        $loc_id_cache = array();
        $stat_id_cache = array();
        $spl_id_cache = array();
        // read file line by line
        $updated_row = 0;
        $file = $files_disk->readStream('temp' . DS . $_SESSION['csv']['name'] . '.csv');
        $fileNumber = $files_disk->readStream('temp' . DS . $_SESSION['csv']['name'] . '.csv');
        $n = 0;

        // get total line
        $lineNumber = 0;
        while (!feof($fileNumber)) {
            $line = fgets($fileNumber, $max_chars);
            if (empty($line)) continue;
            $lineNumber++;
        }

        // ========== TAMBAHAN: Ambil library_id dari session ==========
        $import_library_id = $_SESSION['csv']['library_id'] ?? $current_library_id;
        // ========== AKHIR TAMBAHAN ==========

        try {
            $pdo = DB::getInstance();
            
            // ========== PERBAIKAN: Tambah library_id di INSERT ==========
            $state_insert = $pdo->prepare(<<<SQL
                INSERT IGNORE INTO item (biblio_id, item_code, call_number, coll_type_id,
                    inventory_code, received_date, supplier_id,
                    order_no, location_id, order_date, item_status_id, site,
                    source, invoice, price, price_currency, invoice_date,
                    input_date, last_update, library_id)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
            SQL);
            // ========== AKHIR PERBAIKAN ==========

            $state_update = $pdo->prepare(<<<SQL
                UPDATE item SET call_number = ?, coll_type_id = ?,
                    inventory_code = ?, received_date = ?, supplier_id = ?,
                    order_no = ?, location_id = ?, order_date = ?, item_status_id = ?, site = ?,
                    source = ?, invoice = ?, price = ?, price_currency = ?, invoice_date = ?,
                    input_date = ?, last_update = ? WHERE item_code = ?;
            SQL);

            while (!feof($file)) {
                // record count
                if ($record_num > 0 AND $row_count == $record_num) {
                    break;
                }
                // go to offset
                if ($row_count < $record_offset) {
                    // pass and continue to next loop
                    $field = fgetcsv($file, $max_chars, $field_sep, $field_enc);
                    $row_count++;
                    continue;
                } else {
                    // get an array of field
                    $field = fgetcsv($file, $max_chars, $field_sep, $field_enc);
                    if ($field) {
                        // preprocess fields
                        $item_code = $field[0];
                        $title = $field[18];
                        $field[2] = utility::getID($dbs, 'mst_coll_type', 'coll_type_id', 'coll_type_name', $field[2], $ct_id_cache);
                        $field[5] = utility::getID($dbs, 'mst_supplier', 'supplier_id', 'supplier_name', $field[5], $spl_id_cache);
                        $field[7] = utility::getID($dbs, 'mst_location', 'location_id', 'location_name', $field[7], $loc_id_cache);
                        $field[9] = utility::getID($dbs, 'mst_item_status', 'item_status_id', 'item_status_name', $field[9], $stat_id_cache);
                        $field[4] = !empty($field[4])?$field[4]:date('Y-m-d H:i:s');
                        $field[8] = !empty($field[8])?$field[8]:date('Y-m-d H:i:s');
                        $field[15] = !empty($field[15])?$field[15]:date('Y-m-d H:i:s');
                        $field[16] = !empty($field[16])?$field[16]:date('Y-m-d H:i:s');
                        $field[17] = !empty($field[17])?$field[17]:date('Y-m-d H:i:s');
                        $field[13] = !empty($field[13])?$field[13]:0.0;

                        // get biblio_id
                        $b_q = $dbs->query(sprintf("select biblio_id from biblio where title = '%s'", $dbs->real_escape_string($title)));
                        if($b_q->num_rows < 1) continue;
                        $b_d = $b_q->fetch_row();
                        $biblio_id = $b_d[0];
        
                        // sql insert string
                        if (!isItemExists($item_code)) {
                            // prepend biblio id
                            array_unshift($field, $biblio_id);
                            // ========== TAMBAHAN: Tambah library_id ==========
                            $field[] = $import_library_id;
                            // ========== AKHIR TAMBAHAN ==========
                            $state_insert->execute($field);
                        } else {
                            unset($field[0], $field[18]);
                            $field[] = $item_code;
                            // reset array keys number
                            $field = array_values($field);
                            $state_update->execute($field);
                        }
                    }
                    
                    VarDumper::dump(
                        str_replace(['{item_code}', '{title}'], [$item_code, $title], __('Success importing item data : {item_code} ({title})'))
                    );
                    $row_count++;
                    importProgress(round($row_count/$lineNumber * 100));
                    usleep(2500);
                }
            }
        } catch (Exception $e) {
            // Reset session
            unset($_SESSION['csv']);
            $errorMessage = $e->getMessage();
            toastr($errorMessage)->error();
            exit(<<<HTML
            <script>
            parent.\$('.infoBox').html('{$errorMessage}')
            parent.\$('.infoBox').addClass('errorBox');
            parent.\$('.infoBox').removeClass('infoBox');
            </script>
            HTML);
        }

        // close file handle
        fclose($file);

        // set information variable before reset csv session
        $redirectTo = $_SESSION['csv']['action'];
        $fileName = $_SESSION['csv']['name'];

        // delete temp file
        $files_disk->delete('temp' . DS . $fileName . '.csv');

        // Reset session
        unset($_SESSION['csv']);

        $end_time = time();
        $import_time_sec = $end_time-$start_time;
        writeLog('staff', $_SESSION['uid'], 'bibliography', 'Importing '.$updated_row.' item records from file : '.$fileName);
        $label = str_replace(['{row_count}','{time_to_finish}'], [$row_count, $import_time_sec], __('Success imported <strong>{row_count}</strong> title in <strong>{time_to_finish}</strong> second'));
        exit(<<<HTML
        <script>
        parent.\$('.infoBox').html('{$label}')
        setTimeout(() => parent.\$('#mainContent').simbioAJAX('{$redirectTo}'), 2500)
        </script>
        HTML);
    }
}
?>
<div class="menuBox">
    <div class="menuBoxInner importIcon">
        <div class="per_title">
            <h2><?php echo __('Item Import tool'); ?></h2>
        </div>
            <div class="infoBox">
                <?php echo __('Import for item data from CSV file'); ?>
                &nbsp;<a href="<?= $_SERVER['PHP_SELF'] ?>?action=download_sample" class="s-btn btn btn-secondary notAJAX"><?= __('Download Sample') ?></a>
                <br><br>
                <strong><?php echo __('Library'); ?>:</strong> 
                <?php 
                if ($is_superadmin) {
                    echo __('Select library from dropdown below');
                } else {
                    $lib_query = "SELECT library_code, library_name FROM mst_library WHERE library_id = " . $current_library_id;
                    $lib_result = $dbs->query($lib_query);
                    $lib_data = $lib_result->fetch_assoc();
                    echo '<span style="color:#337AB7;font-weight:bold;">' . $lib_data['library_code'] . ' - ' . $lib_data['library_name'] . '</span>';
                }
                ?>
            </div>
    </div>
</div>
<div id="importInfo" class="infoBox" style="display: none;">&nbsp;</div><div id="importError" class="errorBox" style="display: none;">&nbsp;</div>
<?php
// create new instance
$form = new simbio_form_table_AJAX('mainForm', $_SERVER['PHP_SELF'], 'post');
$form->submit_button_attr = 'name="doImport" value="'.__('Process').'" class="btn btn-default"';
// form table attributes
$form->table_attr = 'id="dataList" class="s-table table"';
$form->table_header_attr = 'class="alterCell font-weight-bold"';
$form->table_content_attr = 'class="alterCell2"';

/* Form Element(s) */
// csv files
$str_input  = '<div class="container-fluid">';
$str_input .= '<div class="row">';
$str_input .= '<div class="custom-file col-6">';
$str_input .= simbio_form_element::textField('file', 'importFile','','class="custom-file-input"');
$str_input .= '<label class="custom-file-label" for="customFile">Choose file</label>';
$str_input .= '</div>';
$str_input .= '<div class="col">';
$str_input .= '<div class="mt-2">Maximum '.$sysconf['max_upload'].' KB</div>';
$str_input .= '</div>';
$str_input .= '</div>';
$str_input .= '</div>';
$form->addAnything(__('File To Import'), $str_input);

// ========== TAMBAHAN MULTI-LIBRARY: Field Library ID (hanya untuk Superadmin) ==========
if ($is_superadmin) {
    $lib_options = array();
    $lib_query = "SELECT library_id, library_code, library_name FROM mst_library WHERE is_active = 1 ORDER BY library_code ASC";
    $lib_result = $dbs->query($lib_query);
    while ($lib_row = $lib_result->fetch_assoc()) {
        $lib_options[] = array($lib_row['library_id'], $lib_row['library_code'] . ' - ' . $lib_row['library_name']);
    }
    $form->addSelectList('library_id', __('Library'), $lib_options, $current_library_id, 'class="form-control" style="width:50%;"', __('Pilih perpustakaan tujuan import'));
} else {
    // Non-superadmin: tampilkan informasi library
    $lib_query = "SELECT library_code, library_name FROM mst_library WHERE library_id = " . $current_library_id;
    $lib_result = $dbs->query($lib_query);
    $lib_data = $lib_result->fetch_assoc();
    $form->addAnything(__('Library'), '<strong>' . $lib_data['library_code'] . ' - ' . $lib_data['library_name'] . '</strong>');
    $form->addHidden('library_id', $current_library_id);
}
// ========== AKHIR TAMBAHAN ==========

// field separator
$form->addTextField('text', 'fieldSep', __('Field Separator').'*', ''.htmlentities(',').'', 'style="width: 10%;" maxlength="3" class="form-control"');
//  field enclosed
$form->addTextField('text', 'fieldEnc', __('Field Enclosed With').'*', ''.htmlentities('"').'', 'style="width: 10%;" class="form-control"');
// number of records to import
$form->addTextField('text', 'recordNum', __('Number of Records To Import (0 for all records)'), '0', 'style="width: 10%;" class="form-control"');
// records offset
$form->addTextField('text', 'recordOffset', __('Start From Record'), '1', 'style="width: 10%;" class="form-control"');
// header (column name)
$form->addCheckBox('header', __('The first row is the columns names'), array( array('1', __('Yes')) ), '');
// output the form
echo $form->printOut();
?>
<script>
$(document).on('change', '.custom-file-input', function () {
    let fileName = $(this).val().replace(/\\/g, '/').replace(/.*\//, '');
    $(this).parent('.custom-file').find('.custom-file-label').text(fileName);
});
</script>