<?php

namespace App\Controller;

use App\Controller\AppController;

/**
 * Departments Controller
 *
 * @property \App\Model\Table\DepartmentsTable $Departments
 */
class DepartmentsController extends AppController {

    public function listDep($examination_id = null) {
        $this->loadModel('Examinations');
        $examinations = $this->Examinations->find('all');
        if(isset($examination_id)) {
            $actual = $this->Examinations->get($examination_id);
            $this->set('actual',$actual);
        }
        //debug($examinations);die;
        $this->set('examinations', $examinations);
        //$this->loadModel('Supervisings');
        //$supervisings = $this->Supervisings->find('all', array('conditions' => array('schooltype_id' => $school_type)));
        
        $supervisings = $this->Departments->find('all', array('fields' => array('super_school_code'), 'conditions' => array('examination_id' => $examination_id),  'group' => array('super_school_code')));
        
        foreach ($supervisings as $supervising) {
            //debug($supervising);
            if (!empty($supervising['super_school_code'])) {
                $aufsicht[$supervising['super_school_code']]['super_school_code'] = $supervising['super_school_code'];
                $delivered_query = $this->Departments->find('all', array('conditions' => array('examination_id' => $examination_id,'super_school_code' => $supervising['super_school_code'], 'delivery_date is not null')));
                $aufsicht[$supervising['super_school_code']]['delivered'] = $delivered_query->count();
                $all_query = $this->Departments->find('all', array('conditions' => array('examination_id' => $examination_id,'super_school_code' => $supervising['super_school_code'])));
                $aufsicht[$supervising['super_school_code']]['all'] = $all_query->count();
            //debug($supervising);
            }
        }
        if(isset($examination_id)) {
            $sum_all_query = $this->Departments->find('all', array('conditions' => array('examination_id' => $examination_id)));
            $this->set('all',$sum_all_query->count());
            $sum_del_query = $this->Departments->find('all', array('conditions' => array('examination_id' => $examination_id, 'delivery_date is not null')));
            $this->set('del',$sum_del_query->count());
        }
        
        //debug($aufsicht);
        //die;
        
        $status = $this->Departments->find('all', array('fields' => array('status'), 'conditions' => array('examination_id' => $examination_id),'group' => array('status')));
        $status_arr = '';
        $i = 0;
        foreach ($status as $s) {
            //debug($s);
            $status_arr[$i] = $s->status;
            $i++;
        }
        //debug($status_arr);die;
        $this->set('status', $status);
        $this->set('examination_id', $examination_id);
        //$this->set('schooltype', $schooltype->name);
        //$this->set('school_type',$school_type);
        if(isset($aufsicht)) {
        $this->set('aufsicht', $aufsicht);
        }
        $this->render(null, 'asd');
    }

    public function listDepAjax($examination_id) {
        //debug($school_type);
        $list = '';
        $conditions = array('examination_id' => $examination_id);
        //debug($conditions);
        //debug($this->request->data);
        if (isset($this->request->data['delivery_date_on']) AND isset($this->request->data['delivery_date_off'])) {
            unset($this->request->data['delivery_date_on']);
            unset($this->request->data['delivery_date_off']);
        } elseif (isset($this->request->data['delivery_date_on'])) {
            unset($this->request->data['delivery_date_on']);
            $conditions[0] = 'delivery_date is not null';
        } else {
            unset($this->request->data['delivery_date_off']);
            $conditions[0] = 'delivery_date is null';
        }
        //debug($this->request->data);
        //debug($conditions);
        if (!empty($this->request->data)) {
            foreach ($this->request->data as $key => $d) {
                //debug($key);
                $key_ex = explode('--', $key);
                $list[$key_ex[0]][$key_ex[1]] = $key_ex[1];
            }
            $j = 1;
            foreach ($list as $key => $l) {
                $i = 0;
                foreach ($l as $item) {
                    $item_arr[$i] = '"' . $item . '"';
                    $i++;
                }
                //debug($item_arr);
                $string[$key] = rtrim(implode(',', $item_arr), ',');
                $conditions[$j] = $key . ' IN (' . $string[$key] . ')';
                $j++;
            }
        } else {
            $string = '';
        }
        //debug($conditions);
        //die;
        //$this->loadModel('Schooltypes');
        //$schooltype = $this->Schooltypes->get($school_type);
        //$this->loadModel('Supervisings');
        //$supervisings = $this->Supervisings->find('all');
        //$aufsicht = '';
        //foreach ($supervisings as $s) {
        //    $aufsicht[$s->id] = $s->code;
        //}
        //debug($conditions);
        if (isset($this->request->query['delivered']) && $this->request->query['delivered'] == 'no') {
            //debug('Hallo1');
            $departments = $this->Departments->find('all', array('conditions' => array('schooltype_id' => $school_type, 'delivery_date is null'), 'fields' => array('supervising_id', 'title', 'status', 'code', 'delivery_date', 'delivered')));
        } else {
            //debug('Hallo2');
            //$departments = $this->Departments->find('all', array('fields' => array('supervising_id', 'title', 'status', 'code', 'delivery_date', 'delivered')));
            $departments = $this->Departments->find('all', array('conditions' => $conditions, 'fields' => array('super_school_code', 'super_school_code_2', 'title', 'status', 'code', 'delivery_date', 'delivered', 'checkedin')));
            //debug($departments);
        }

        foreach ($departments as $department) {
            //debug($department);
            //$department->supervising_id = $aufsicht[$department->supervising_id];
            //debug($department->delivery_date);
            $time = $department->delivery_date;
            //debug($time->i18nFormat('yyyy-MM-dd')); die;
            if (!empty($department->delivery_date)) {
                $department->delivery_date = $time->i18nFormat('yyyy-MM-dd');
            }
            //debug($department);
        }

        //debug(json_encode($departments));
        //die;
        //$this->set('schooltype', $schooltype->name);
        $this->set('departments', $departments);
        $this->render(null, 'ajax');
    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index() {
        $this->paginate = [
            'contain' => ['Schooltypes', 'Supervisings']
        ];
        $departments = $this->paginate($this->Departments);

        $this->set(compact('departments'));
        $this->set('_serialize', ['departments']);
    }

    /**
     * View method
     *
     * @param string|null $id Department id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null) {
        $department = $this->Departments->get($id, [
            'contain' => ['Schooltypes', 'Supervisings']
        ]);

        $this->set('department', $department);
        $this->set('_serialize', ['department']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add() {
        $department = $this->Departments->newEntity();
        if ($this->request->is('post')) {
            $department = $this->Departments->patchEntity($department, $this->request->data);
            if ($this->Departments->save($department)) {
                $this->Flash->success(__('The department has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The department could not be saved. Please, try again.'));
        }
        $schooltypes = $this->Departments->Schooltypes->find('list', ['limit' => 200]);
        $supervisings = $this->Departments->Supervisings->find('list', ['limit' => 200]);
        $this->set(compact('department', 'schooltypes', 'supervisings'));
        $this->set('_serialize', ['department']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Department id.
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null) {
        $department = $this->Departments->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $department = $this->Departments->patchEntity($department, $this->request->data);
            if ($this->Departments->save($department)) {
                $this->Flash->success(__('The department has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The department could not be saved. Please, try again.'));
        }
        $schooltypes = $this->Departments->Schooltypes->find('list', ['limit' => 200]);
        $supervisings = $this->Departments->Supervisings->find('list', ['limit' => 200]);
        $this->set(compact('department', 'schooltypes', 'supervisings'));
        $this->set('_serialize', ['department']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Department id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null) {
        $this->request->allowMethod(['post', 'delete']);
        $department = $this->Departments->get($id);
        if ($this->Departments->delete($department)) {
            $this->Flash->success(__('The department has been deleted.'));
        } else {
            $this->Flash->error(__('The department could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }

}
