<?php

namespace AsdCurl;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use AsdCurl\AsdCurl;
use Cake\Core\Configure;
use Cake\I18n\Time;

class AsdUp extends AsdCurl
{
    public function __construct($url = null)
    {
        $this->status = true;
        $this->error = false;
    }

    public function change_school($ch,$snr) {
        $SCHULPORTAL_ASD_BASE_URL = Configure::read('AsdCurl.asdbaseurl');
        // Schule wechseln
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');

        $post = [
            'mainForm' => 'mainForm',
            'referenzBaumAnzeigen' => 'false',
            'conversationId' => $conversationId,
            'changeNotification' => 'true',
            'mainForm:hasChanged' => 'false',
            'mainForm:hasNotReleased' => 'false',
            'mainForm:hasPopUpChanged' => 'false',
            'mainForm:hasAdresseChanged' => 'false',
            'mainForm:confirmedOverride' => 'true',
            'mainForm:zuordnungNotification' => 'false',
            'skipvalidation' => 'true',
            'mainForm:istAnzeigeMaske' => 'false',
            'mainForm:schulnr' => $snr,
            'mainForm:action_anzeigen' => 'Direkt zur gewählten Schule',
            'mainForm:schuljahr' => '23',
            'javax.faces.ViewState' => $viewstate
        ];

        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_50.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }
        echo $this->html;die;
    }

    public function goto_budget($ch,$budget) {
        $SCHULPORTAL_ASD_BASE_URL = Configure::read('AsdCurl.asdbaseurl');
        // Reiter Unterrichtsplanung aufrufen
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'headerMenu' => 'headerMenu',
            'javax.faces.ViewState' => $viewstate,
            'headerMenu:j_idt29' => 'headerMenu:j_idt29'
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbv/bv_01_02.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }
        // Im linken Menue Budget GY anzeigen
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'sideMenu' => 'sideMenu',
            'javax.faces.ViewState' => $viewstate,
            'sideMenu:j_idt52:1:j_idt58:6:j_idt60' => 'sideMenu:j_idt52:1:j_idt58:6:j_idt60'
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdup/up_00_00.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }

        // Schule auswählen
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'mainForm' => 'mainForm',
	        'referenzBaumAnzeigen' => 'false',
	        'conversationId' => $conversationId,
	        'changeNotification' => 'true',
	        'mainForm:hasChanged' => 'false',
	        'mainForm:hasNotReleased' => 'false',
	        'mainForm:hasPopUpChanged' => 'false',
	        'mainForm:hasAdresseChanged' => 'false',
	        'mainForm:confirmedOverride' => 'true',
	        'mainForm:zuordnungNotification' => 'false',
	        'skipvalidation' => 'false',
	        'mainForm:istAnzeigeMaske' => 'false',
	        'mainForm:nameOffiziell' => 'Bayer.Staatsministerium für Unterricht und Kultus München',
	        'mainForm:schulstellePlzStrasse' => '80333',
	        'mainForm:schulstelleOrt' => 'München',
	        'mainForm:schulnummer' => $budget['snr'],
	        'mainForm:action_anzeigen' => 'Direkt zur gewählten Schule',
	        'javax.faces.ViewState' => $viewstate
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_51.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        } 
        
        // Auf anderen Reiter wechseln
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'mainForm' => 'mainForm',
	        'referenzBaumAnzeigen' => 'false',
	        'conversationId' => $conversationId,
	        'changeNotification' => 'true',
	        'mainForm:hasChanged' => 'false',
	        'mainForm:hasNotReleased' => 'false',
	        'mainForm:hasPopUpChanged' => 'false',
	        'mainForm:hasAdresseChanged' => 'false',
	        'mainForm:confirmedOverride' => 'true',
	        'mainForm:zuordnungNotification' => 'false',
	        'skipvalidation' => 'false',
	        'mainForm:istAnzeigeMaske' => 'false',
	        'mainForm:schulnr' => '',
	        'mainForm:schuljahr' => '23',
	        'mainForm:schulnummer' => $budget['snr'],
	        'mainForm:tabs.1' => 'Budgetzuschläge',
	        'javax.faces.ViewState' => $viewstate
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_50.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }  

    }

    public function read_budget($ch,$snr) {
        $SCHULPORTAL_ASD_BASE_URL = Configure::read('AsdCurl.asdbaseurl');
        $htmlParser = new \DOMDocument(); //to parse html using DOMDocument
        libxml_use_internal_errors(true); // your HTML gives parser warnings, keep them internal
        $htmlParser->loadHTML($this->html); //Loaded the html string we took from simple xml
        $htmlParser->preserveWhiteSpace = false;

        $headers = ['checkbox','herkunft','budget','von','bis','count','comment'];
        $table = $htmlParser->getElementById('mainForm:dataTable:zuschlaege');

        $rows = $table->getElementsByTagName('tr');
        
        for($i=0;$i < (sizeof($rows)-1);$i++) {
            $cols = $rows[$i]->getElementsByTagName('td');
            $j = 0;
            foreach($cols as $col) {
                $span =  $col->getElementsByTagName('span');
                //debug(sizeof($span));
                if(sizeof($span) > 0) {
                    $data[$i][$headers[$j]] = $span->item(0)->nodeValue;              
                }
                $j++;
            }          
        }
        $cols = $rows[sizeof($rows)-1]->getElementsByTagName('td');
        $span = $cols[1]->getElementsByTagName('span');
        $sum = $span->item(0)->nodeValue;
        //$data['sum'] = $sum;
        return($data);
    }

    public function del_all_budget($ch,$snr) {
        //debug($snr);
        //echo($this->html);
        $SCHULPORTAL_ASD_BASE_URL = Configure::read('AsdCurl.asdbaseurl');

        $htmlParser = new \DOMDocument(); //to parse html using DOMDocument
        libxml_use_internal_errors(true); // your HTML gives parser warnings, keep them internal
        $htmlParser->loadHTML($this->html); //Loaded the html string we took from simple xml
        $htmlParser->preserveWhiteSpace = false;
        
        $table = $htmlParser->getElementById('mainForm:dataTable:zuschlaege');

        $rows = $table->getElementsByTagName('tr');
        $to_del = array();
        for($i=1;$i < sizeof($rows);$i++) {
            $cols = $rows[$i]->getElementsByTagName('td');
            debug(Sizeof($cols));
            $herkunft = $cols[1]->getElementsByTagName('span')->item(0)->nodeValue;
            debug($herkunft);
            if($herkunft == 'ASD') {
                array_push($to_del,$i);
            }
        }
        debug($to_del);
        //die;

        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'mainForm' => 'mainForm',
	        'referenzBaumAnzeigen' => 'false',
	        'conversationId' => $conversationId,
	        'changeNotification' => 'true',
	        'mainForm:hasChanged' => 'true',
	        'mainForm:hasNotReleased' => 'false',
	        'mainForm:hasPopUpChanged' => 'false',
	        'mainForm:hasAdresseChanged' => 'false',
	        'mainForm:confirmedOverride' => 'true',
	        'mainForm:zuordnungNotification' => 'false',
	        'skipvalidation' => 'false',
	        'mainForm:istAnzeigeMaske' => 'false',
	        'mainForm:schulnr' => '',
	        'mainForm:schuljahr' => '23',
	        'mainForm:schulnummer' => $snr,
	        'mainForm:tab2:artZuschlag' => 'com.hp.asd.wl.domain.standard.Budgetform:1502',
	        'mainForm:tab2:datumBis' => '31.07.2019',
	        'mainForm:tab2:_edit_budget' => '',
	        'mainForm:tab2:_edit_bemerkung' => '',
	        'mainForm:loeschen' => 'Löschen',
	        'javax.faces.ViewState' => $viewstate
        ];
        foreach($to_del as $value) {    
            $post['mainForm:tab2:_' . $value .'_select'] = 'on';
        }
        //debug($post);die;
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_50.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }  

        // Budget speichern
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'mainForm' => 'mainForm',
	        'referenzBaumAnzeigen' => 'false',
	        'conversationId' => $conversationId,
	        'changeNotification' => 'true',
	        'mainForm:hasChanged' => 'true',
	        'mainForm:hasNotReleased' => 'false',
	        'mainForm:hasPopUpChanged' => 'false',
	        'mainForm:hasAdresseChanged' => 'false',
	        'mainForm:confirmedOverride' => 'true',
	        'mainForm:zuordnungNotification' => 'false',
	        'skipvalidation' => 'false',
	        'mainForm:istAnzeigeMaske' => 'false',
	        'mainForm:schulnr' => '',
	        'mainForm:schuljahr' => '23',
	        'mainForm:schulnummer' => $snr,
	        'mainForm:tab2:artZuschlag' => 'com.hp.asd.wl.domain.standard.Budgetform:1502',
	        'mainForm:tab2:datumBis' => '31.07.2019',
	        'mainForm:tab2:_edit_budget' => '',
	        'mainForm:tab2:_edit_bemerkung' => '',
	        'mainForm:speichern' => 'Speichern',
	        'javax.faces.ViewState' =>  $viewstate
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_50.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }  
        return($to_del);
    }

    public function write_budget($ch,$budget) {
        $SCHULPORTAL_ASD_BASE_URL = Configure::read('AsdCurl.asdbaseurl');

        // Budget eintragen
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'mainForm' => 'mainForm',
            'referenzBaumAnzeigen' => 'false',
            'conversationId' => $conversationId,
            'changeNotification' => 'true',
            'mainForm:hasChanged' => 'true',
            'mainForm:hasNotReleased' => 'false',
            'mainForm:hasPopUpChanged' => 'false',
            'mainForm:hasAdresseChanged' => 'false',
            'mainForm:confirmedOverride' => 'true',
            'mainForm:zuordnungNotification' => 'false',
            'skipvalidation' => 'false',
            'mainForm:istAnzeigeMaske' => 'false',
            'mainForm:schulnr' => '',
            'mainForm:schuljahr' => '23',
            'mainForm:schulnummer' => $budget['snr'],
            'mainForm:tab2:artZuschlag' => 'com.hp.asd.wl.domain.standard.Budgetform:' . $budget['budget_id'],
            'mainForm:tab2:datumBis' => '31.07.2019',
            'mainForm:tab2:_edit_budget' => $budget['count'],
            'mainForm:tab2:_edit_bemerkung' => $budget['comment'],
            'mainForm:tab2:hinzufuegen' => 'Hinzufügen',
            'javax.faces.ViewState' => $viewstate
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_50.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }  

        // Budget speichern
        $viewstate = $this->get_string_between($this->html, 'id="javax.faces.ViewState" value="', '" autocomplete');
        $conversationId = $this->get_string_between($this->html, 'name="conversationId" value="', '" />');
       
        $post = [
            'mainForm' => 'mainForm',
	        'referenzBaumAnzeigen' => 'false',
	        'conversationId' => $conversationId,
	        'changeNotification' => 'true',
	        'mainForm:hasChanged' => 'true',
	        'mainForm:hasNotReleased' => 'false',
	        'mainForm:hasPopUpChanged' => 'false',
	        'mainForm:hasAdresseChanged' => 'false',
	        'mainForm:confirmedOverride' => 'true',
	        'mainForm:zuordnungNotification' => 'false',
	        'skipvalidation' => 'false',
	        'mainForm:istAnzeigeMaske' => 'false',
	        'mainForm:schulnr' => '',
	        'mainForm:schuljahr' => '23',
	        'mainForm:schulnummer' => $budget['snr'],
	        'mainForm:tab2:artZuschlag' => 'com.hp.asd.wl.domain.standard.Budgetform:' . $budget['budget_id'],
	        'mainForm:tab2:datumBis' => '31.07.2019',
	        'mainForm:tab2:_edit_budget' => '',
	        'mainForm:tab2:_edit_bemerkung' => '',
	        'mainForm:speichern' => 'Speichern',
	        'javax.faces.ViewState' =>  $viewstate
        ];
        $this->exec_curl($ch, $SCHULPORTAL_ASD_BASE_URL . '/asdbu/up_07_50.jsf?cid=' . $conversationId, $post);
        if(!($this->status)) {
            die($this->error);
        }  

        //echo $this->html;die;        
    }

    
}
