<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Projects extends CI_Controller
    {

        public function __construct()
        {
            parent::__construct();
            $this->load->model('ProjectsModel', '', TRUE);
            $this->load->model('FileModel', '', TRUE);
        }

        public function index()
        {
            if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) redirect(base_url('login'));

            $entries = $this->ProjectsModel->getEntries('all');
            $categories = $this->ProjectsModel->getCategories('all');

            $this->load->view('admin/sidebar', ['title' => 'Projekte verwalten']);
            $this->load->view('admin/projects', ['entries' => $entries, 'categories' => $categories]);
            $this->load->view('admin/footer');
        }

        public function edit($id = NULL)
        {
            if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) redirect(base_url('login'));
            $edit = $id === NULL ? false : true;
            $content = null;
            $projectCategories = [];

            if ($edit) {
                if ($this->ProjectsModel->checkIfExists($id)) {
                    $content = $this->ProjectsModel->getEntry($id);
                    $content = $this->ProjectsModel->mergeFullTranslationData($content)[0];
                    $projectCategories = $this->ProjectsModel->getEntryCategories($id);
                } else {
                    redirect(base_url('admin/projects/edit'));
                }
            }

            $categories = $this->ProjectsModel->getCategories();

            $this->load->view('admin/sidebar', ['title' => 'Projekt erstellen', 'additionalStyles' => ['lib/content-tools/content-tools.min.css', 'project-edit.css']]);
            $this->load->view('admin/project_edit', ['edit' => $id === NULL ? -1 : $id, 'categories' => $categories, 'content' => $content, 'pCategories' => $projectCategories]);
            $this->load->view('admin/footer', ['additionalScripts' => ['lib/content-tools/content-tools.min.js', 'project-edit.js']]);
        }

        public function sendEdit()
        {
            header('Content-Type: application/json');
            if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) {
                echo json_encode(['success' => false, 'message' => 'Du hast nicht genĂ¼gend Rechte, um Projekte zu erstellen bzw. bearbeiten.']);
                exit;
            }

            $translations = [];
            $translations['de']['title'] = $this->input->post('titleDE');
            $translations['de']['description'] = $this->input->post('headlineDE');
            $translations['de']['content'] = $this->input->post('contentDE');
            $translations['en']['title'] = $this->input->post('titleEN');
            $translations['en']['description'] = $this->input->post('headlineEN');
            $translations['en']['content'] = $this->input->post('contentEN');
            $translations['fr']['title'] = $this->input->post('titleFR');
            $translations['fr']['description'] = $this->input->post('headlineFR');
            $translations['fr']['content'] = $this->input->post('contentFR');

            $url = $this->input->post('url');

            $download['available'] = $this->input->post('isDownloadable') == 'on' ? true : false;
            $download['link'] = $this->input->post('downloadLink');
            $download['name'] = $this->input->post('downloadLinkName');

            $openSource['available'] = $this->input->post('isOpenSource') == 'on' ? true : false;
            $openSource['link'] = $this->input->post('openSourceLink');
            $openSource['name'] = $this->input->post('openSourceLinkName');

            $customLink['link'] = $this->input->post('customLink');
            $customLink['name'] = $this->input->post('customLinkName');

            $categories = $this->input->post('categories');
            $date = date('Y-m-d H:i:s', strtotime($this->input->post('date')));
            $image = $this->input->post('image');

            $editingID = $this->input->post('editingID');

            if($editingID == '-1' && $this->ProjectsModel->checkIfNameExists($url)) {
                echo json_encode(['success' => false, 'message' => 'Die angegebene URL ist bereits vergeben.']);
                exit;
            }

            if ($editingID == '-1' || !$this->ProjectsModel->checkIfExists($editingID)) {
                $editingID = $this->ProjectsModel->createNewProjectDraft();
            }

            $this->ProjectsModel->updateProject($editingID, $translations, $url, $download, $openSource, $customLink, $date, $image);
            $this->ProjectsModel->updateCategories($editingID, $categories);

            echo json_encode(['success' => true, 'message' => 'Das Projekt wurde erfolgreich gespeichert.', 'id' => $editingID]);
        }

        public function delete()
        {
            if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) redirect(base_url('login'));
            $id = filter_input(INPUT_POST, "id");
            $this->ProjectsModel->delete($id);
        }

        public function delete_category()
        {
            if (!isset($_SESSION['user']) || empty($_SESSION['user']) || $_SESSION['user']['rank'] < 9) redirect(base_url('login'));
            $id = filter_input(INPUT_POST, "id");
            $this->ProjectsModel->deleteCategory($id);
        }
    }