<?php
/**
 * Created by PhpStorm.
 * User: pejot
 * Date: 2015-03-08
 * Time: 22:39
 */
global $ankieta;
global $data;
global $page;

//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);

require_once('../../vendor/autoload.php');

use League\Csv\Reader;

$reader = Reader::createFromPath('quota.txt', 'r');
$reader->setDelimiter("\t");
$fileData=$reader->fetchAssoc(0);
$data=[];
foreach ($fileData as $lp=>$dataRow) {
    $data[$dataRow['NRANK']]=$dataRow;
    unset($data[$dataRow['NRANK']]['NRANK']);
}
//new dBug($data);


$ankieta->writeGap();
$ankieta->writeHeader("KONTROLA PRÓBY");
$ankieta->writeGap();

$ankieta->writeInput('NRANK','Proszę wpisać nr ankiety:',@clean($data['NRANK'],''),true,"controlQuotas()", "STRING","255",true,false);
?>
<div id="status" class="center-block"></div>

<?


$answers=array();
$answers[1]='Warszawa';
$answers[2]='Lublin';
$ankieta->writeRadio('MIASTOPR','MIASTO PRÓBA',$answers,true,@clean($data['MIASTOPR']));

$answers=array();
$answers[1]='25-42';
$answers[2]='43-60';
$answers[3]='25-60';
$ankieta->writeRadio('WIEKPR','WIEK PRÓBA',$answers,true,@clean($data['WIEKPR']));

//$answers=array();
//$answers[1]='Mężczyzna';
//$answers[2]='Kobieta';
//$ankieta->writeRadio('PLECPR','PŁEĆ PRÓBA',$answers,true,@clean($data['PLECPR']));


$answers=array();
#$answers[1]='Najczęściej używa Kujawski PET';
#$answers[2]='najczęściej Wielkopolski PET / Wyborny PET';
$answers[1]='najczęściej używa Kujawski PET';
$answers[2]='Wielkopolski PET';
$answers[3]='Wyborny PET';

$ankieta->writeRadio('USERPR','UŻYTKOWNIK PRÓBA',$answers,true,@clean($data['USERPR']));




$answers=array();
$answers[1]="Produkt P8";
$answers[2]="Produkt L5";
$answers[3]="Produkt O7";
$ankieta->writeRadio('PROD1PR','PRODUKT1 PRÓBA',$answers,true,@clean($data['PROD1PR']));
//$ankieta->writeRadio('PROD2PR','PRODUKT2 PRÓBA',$answers,true,@clean($data['PROD2PR']));

$answers=array();
$answers[1]="12,99 zł";
$answers[2]="9,99 zł";
$ankieta->writeRadio('CENA1','CENA 1',$answers,true,@clean($data['CENA1']));
$ankieta->writeRadio('CENA2','CENA 2',$answers,true,@clean($data['CENA2']));



?>


<script type="text/javascript">

    var quota=<?php echo json_encode($data);?>;
    //console.log('quota',quota);

    $(document).ready(function() {
        for (f in quota[1]) {
            disableButtons("[name=" + f + "]");
        }

    });

    function preSubmit() {
        console.log('preSubmit');
        var objNRANK=$('#NRANK');
        var NRANK=objNRANK.val();
        for (f in quota[NRANK]) {
            enableButtons("[name=" + f + "]");
        }
    }
    $('#NextDebugButton').on('click',function(el) {
        console.log('click',el);
        //var input=$(this).next().next();
        //console.log('input',input);
    });

    function controlQuotas() {
        var objNRANK=$('#NRANK');
        var NRANK=objNRANK.val();
        //console.log('Quota control');
        console.log('NRANK',NRANK);
        if (quota.hasOwnProperty(NRANK)) {
            for (f in quota[NRANK]) {
                disableButtons("[name=" + f + "]");
            }
            $('#status').removeClass('alert alert-warning alert-danger').addClass('alert alert-success').text("Poprawny nr ankiety, uzupełniam dane");
            //console.log('Prawidłowy nr ankiety',NRANK);
            //console.log('qutoas',quota[NRANK]);
            selectRadio("MIASTOPR",quota[NRANK]['MIASTOPR']);
            selectRadio("WIEKPR",quota[NRANK]['WIEKPR']);
            selectRadio("USERPR",quota[NRANK]['USERPR']);
            //selectRadio("PLECPR",quota[NRANK]['PLECPR']);
            selectRadio("PROD1PR",quota[NRANK]['PROD1PR']);
            selectRadio("CENA1",quota[NRANK]['CENA1']);
            selectRadio("CENA2",quota[NRANK]['CENA2']);
            //selectRadio("PROD2PR",quota[NRANK]['PROD2PR']);
        } else {
            if (NRANK!=="") {
                //console.log('NRANK',NRANK);
                if (parseFloat(NRANK,10)===0) {
                    $('#status').removeClass('alert alert-success alert-danger').addClass('alert alert-warning').text("Wpisano 0 jako Nr Ankiety. Będzie to ankieta testowa.");
                    for (f in quota[1]) {
                        enableButtons("[name=" + f + "]");
                    }
                } else {
                    $('#status').removeClass('alert alert-success alert-danger').addClass('alert alert-warning').text("Niepoprawny nr ankiety. Jeśli jest to konieczne, to należy samemu uzupełnić schemat badawczy.");
                    for (f in quota[1]) {
                        enableButtons("[name=" + f + "]");
                    }
                }
            } else {
                $('#status').removeClass('alert alert-success alert-warning').addClass('alert alert-danger').text("Pusty nr ankiety a jest on wymagany do sprawdzenia próby.");
                for (f in quota[NRANK]) {
                    disableButtons("[name=" + f + "]");
                }
            }


        }

    }



</script>