<?php
    namespace App\Controller;

    use App\Controller\AppController;
    use Cake\Event\Event;
    use Cake\Http\Client;

    class JirasController extends AppController
    {
        public function beforeFilter(Event $event) {
            //$this->Auth->setConfig('checkAuthIn', 'Controller.initialize');
            $this->Auth->allow(['index']);
        }
        public function index()
        {
            $http = new Client();
            $tickets = ['423','469','533','568','421','424','569','422','586','587','588','589','514','420','590','591'];
            $tabledata = array();
            //$j = 0;
            foreach($tickets as $ticket) {
                //debug($ticket);

                $response = $http->get('https://kmjira.bayern.de/rest/api/latest/issue/UHD-' . $ticket .'?fields=description', [], 
                [
                    'proxy' => ['proxy' => 'www.proxy.bybn.de:80'],
                    'auth' => ['type' => 'basic', 'username' => 'TrinklW', 'password' => 'HiegTzs!12']
                ]);
                //debug($response->getStringBody());

                $data = json_decode($response->getStringBody());

                $skuList = explode(PHP_EOL, $data->fields->description);
                
                $istable = 0;
                $entries = array();
                $i=0;
                foreach($skuList as $line) {
                    if(substr($line,0,1) == '|' and substr($line,1,1) !== '|') {
                        $istable = 1;
                        //debug($i);
                        $entries[$i] = $line;
                    }
                    if($istable == 1 and substr($line,0,1) !== '|') {
                        //debug($i);
                        $entries[$i - 1] .= trim($line);
                    }
                    else if($istable == 1) {
                        $i++; 
                    }
                }
                
                foreach($entries as $entry) {
                    $entry = trim(preg_replace('/\s+/', ' ', $entry));
                    //debug($entry);
                    //debug(explode('|',$entry));
                    array_push($tabledata,explode('|',$entry));
                    //$tabledate[$j][] = explode('|',$entry);
                    //debug($j);
                    //debug($tabledate[$j]);
                    //debug($tabledata);
                    //$j++;
                }                
            }
            //debug($tabledata);
            $this->set('tabledata',$tabledata);
        }
    }
