<?php

/**
 * F-K report.
 */
global $current_user, $db, $mod_strings, $app_list_strings;

$prefs = $current_user->getPreference('EcmFkVatRegistersQ');
   
// $prefs['type_basic'];
// $prefs['ecmfkvatkind_id_basic']; 

// dump($prefs);
// exit;
    
$stop = new DateTime('NOW');

$start = clone $stop;
$start->setDate($stop->format('Y'), 1, 1);

// dump($start, $stop);
// exit;

// $date_from = $current_user->getPreference('date_from', 'fk_report_p') ? : $start->format('d.m.Y');
// $date_to = $current_user->getPreference('date_to', 'fk_report_p') ? : $start->format('d.m.Y');

$date_from = @$prefs['dat_dok_from_basic'] ? : $start->format('d.m.Y');
$date_to = @$prefs['dat_dok_to_basic'] ? : $stop->format('d.m.Y');

// dump($date_from, $date_to);
// exit;

if (true == isset($_REQUEST['process'])) {
    $date_from = @$_GET['date_from'] ? : $start->format('d.m.Y');
    $date_to = @$_GET['date_to'] ? : $stop->format('d.m.Y');
    
    $current_user->setPreference('date_from', $date_from, 0, 'fk_report_p');
    $current_user->setPreference('date_to', $date_to, 0, 'fk_report_p');
}

$types = array_flip($app_list_strings['ecmfkvatregisters_type_dom']);
$kinds = array();
$names = array();

// $currentType = @$_GET['type'] ? : '2';
// $currentKind = @$_GET['kind'] ? : '6064d6fa-ea46-33fb-e1a2-5215e8625889';
// $currentName = @$_GET['name'] ? : '6073a913-a926-1bde-8c8e-5215e83a0c8a';

$currentType = @$prefs['type_basic'][0] ? : $types[key($types)];

if (true == isset($_REQUEST['process'])) {
    $currentType = @$_GET['type'] ? : $types[key($types)];
}

// ~

$map = array_flip(array(
    1 => 2, 
    2 => 3,
    3 => 1,
    4 => 0,
));

$kindsQuery = 'select vk.name, vk.id from ecmfkvatkinds as vk where vk.type = ' . $map[$currentType] . ';';

// dump($kindsQuery);
// exit;

$kindsResult = $db->query($kindsQuery);

while($row = $db->fetchByAssoc($kindsResult)) {
    list($key, $value) = array_values($row); 
    $kinds[$key] = $value;
}

$currentKind = @$prefs['ecmfkvatkind_id_basic'] [0] ? : $kinds[key($kinds)];

if (true == isset($_REQUEST['process'])) {
    $currentKind = @$_GET['kind'] ? : $kinds[key($kinds)];
}

// ~


$namesQuery = 'select vki.title, vki.id from ecmfkvatkinditems as vki join ecmfkvatkinds as vk on vk.id = vki.ecmfkvatkind_id where vk.id = \'' . $currentKind . '\';';

// dump($namesQuery);
// exit;

$namesResult = $db->query($namesQuery);

while($row = $db->fetchByAssoc($namesResult)) {

    list($key, $value) = array_values($row); 
    $names[$key] = $value;
}

$currentName = @$_GET['name'] ? : $names[key($names)];

// ~

// dump($date_from, $date_to);
// dump($types, $kinds, $names);
// dump($currentType, $currentKind, $currentName);
// exit;

?>

<link rel="stylesheet" type="text/css" href="modules/EcmFkVatRegisters/report.css" />
<script src="modules/EcmFkVatRegisters/report.js" type="text/javascript"></script>
<script type="text/javascript" >
    var map = {
        'name': 'type'
    };

    function populateKinds(object) {
        object.getValue = selectGetValue;
        
        // console.log(object.getValue());
        // return;
        
        // ~
        
        var req = mint.Request();
        
        req.OnSuccess = function() {
            console.log('responseText', this.responseText);  
            var kind = document.getElementById('kind');
            
            kind.innerHTML = this.responseText;
            kind.onchange();
            return;
        };

        var url = 'index.php?module=EcmFkVatRegisters&action=populatekinds&src=ListView&data=' + object.getValue().join(',') + '&forceselect[]=' + <?php echo '\'' . $currentName . '\''; ?> + '&src=report'; 
        req.Send(url);
        
        console.log()
    }
    
    function populateNames(object) {
        object.getValue = selectGetValue;
        
        // console.log(object.getValue());
        // return;
        
        // ~
        
        var req = mint.Request();
        
        req.OnSuccess = function() {
            console.log('responseText', this.responseText);  
            var name = document.getElementById('name');
            
            name.innerHTML = this.responseText;
            return;
        };

        var url = 'index.php?module=EcmFkVatRegisters&action=populatenames&src=ListView&data=' + object.getValue().join(',') + '&forceselect[]=' + <?php echo '\'' . $currentName . '\''; ?> + '&src=report'; 
        req.Send(url);
    }
</script>

<table cellspacing="0" cellpadding="0" border="0">
    <tr>
        <td>
            <h2>Wydruki PDF</h2>
        </td>
    </tr>
</table>

<br/>

<ul class="tablist" id="tabs">
    <li id="panel_ECMFKBOOK_PDF_menu" class="active">
        <script language="javascript">
            var setPDF = function() {
                SetTab('panel_ECMFKBOOK_PDF');
            };
        </script>
        <a href="javascript:setPDF();" class="current">Wydruki PDF</a>
    </li>
</ul>

<form action="index.php" method="get" name="search_reports" id="search_reports" onsubmit="return sendForm();">
    <input type="hidden" name="module" value="EcmFkVatRegisters" />
    <input type="hidden" name="action" value="p"/>
    <input type="hidden" name="record" value="<?php echo $currentKind; ?>" class="passable"/>
    <input type="hidden" name="process" value="yes" />

    <table>
        <tr>
            <td>
                Rodzaj rejestru:
            </td>
            <td>
                <select id="type" name="type" onchange="populateKinds(this);">
                    <?php foreach($types as $key => $value) : ?>
                        <?php $selected = ($value == $currentType) ? (' selected="selected"') : ''; ?>
                        <option value="<?php echo $value; ?>"<?php echo $selected; ?>><?php echo $key; ?></option>
                    <?php endforeach; ?>
                </select>                                
            </td>
            <td>
                Rejestr:
            </td>
            <td>
                <select id="kind" name="kind" onchange="populateNames(this);">
                    <?php foreach($kinds as $key => $value) : ?>
                        <?php $selected = ($value == $currentKind) ? (' selected="selected"') : ''; ?>
                        <option value="<?php echo $value; ?>"<?php echo $selected; ?>><?php echo $key; ?></option>
                    <?php endforeach; ?>
                </select>                                
            </td>
            <td>
                Wydruk:
            </td>
            <td>
                <select id="name" name="name" class="passable">
                    <?php foreach($names as $key => $value) : ?>
                        <?php $selected = ($value == $currentName) ? (' selected="selected"') : ''; ?>
                        <option value="<?php echo $value; ?>"<?php echo $selected; ?>><?php echo $key; ?></option>
                    <?php endforeach; ?>
                </select>                                
            </td>
            <td>
                Od:
            </td>
            <td>                                    
                <input autocomplete="off" name="date_from" id="date_from" value="<?php echo $date_from; ?>" title="" tabindex="" size="11" maxlength="10" type="text" class="passable">
                <img src="themes/default/images/jscalendar.gif" alt="Enter Date" id="date_from_trigger" align="absmiddle" border="0">
                <script type="text/javascript">
                    Calendar.setup ({
                        inputField : 'date_from',
                        daFormat : '%d.%m.%Y',
                        button : 'date_from_trigger',
                        singleClick : true,
                        dateStr : '',
                        step : 1
                    });
                </script>
            </td>
            <td>
                Do:
            </td>
            <td>                                    
                <input autocomplete="off" name="date_to" id="date_to" value="<?php echo $date_to; ?>" title="" tabindex="" size="11" maxlength="10" type="text" class="passable">
                <img src="themes/default/images/jscalendar.gif" alt="Enter Date" id="date_to_trigger" align="absmiddle" border="0">
                <script type="text/javascript">
                    Calendar.setup ({
                        inputField : 'date_to',
                        daFormat : '%d.%m.%Y',
                        button : 'date_to_trigger',
                        singleClick : true,
                        dateStr : '',
                        step : 1
                    });
                </script>
            </td>
            <td>
                <input type="submit" class="button" value="Generuj"/>
            </td>
        </tr>
    </table>
</form>

<br/>

<div id="panel_ECMFKBOOK_PDF" style="display: block;"></div>