<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

namespace local_kid\output;

use local_kid\local\competency_repository;

defined('MOODLE_INTERNAL') || die();

class competencies_admin_page implements \renderable, \templatable {

    /** @var int */
    protected $promotionid;

    public function __construct(int $promotionid) {
        $this->promotionid = $promotionid;
    }

    public function export_for_template(\renderer_base $output): array {
        $competencies = competency_repository::get_for_promotion($this->promotionid);

        $rows = [];
        foreach ($competencies as $competency) {
            $inuse = competency_repository::is_in_use((int) $competency->id);
            $rows[] = [
                'codecourt' => $competency->codecourt,
                'libelle' => competency_repository::label($competency),
                'codeadesi' => $competency->codeadesi ?: '—',
                'statuslabel' => $competency->active
                    ? get_string('competency_active', 'local_kid')
                    : get_string('promotion_status_inactive', 'local_kid'),
                'statusclass' => $competency->active ? 'fr-badge fr-badge--success' : 'fr-badge',
                'editurl' => (new \moodle_url('/local/kid/pages/competency_edit.php',
                    ['id' => $competency->id, 'promotionid' => $this->promotionid]))->out(false),
                'deleteurl' => (!$inuse) ? (new \moodle_url('/local/kid/pages/competency_action.php',
                    ['id' => $competency->id, 'promotionid' => $this->promotionid, 'action' => 'delete', 'sesskey' => sesskey()]))->out(false) : null,
                'candelete' => !$inuse,
            ];
        }

        return [
            'promotionid' => $this->promotionid,
            'addurl' => (new \moodle_url('/local/kid/pages/competency_edit.php', ['promotionid' => $this->promotionid]))->out(false),
            'syncurl' => (new \moodle_url('/local/kid/pages/competency_sync.php', ['promotionid' => $this->promotionid]))->out(false),
            'hascompetencies' => !empty($rows),
            'competencies' => $rows,
        ];
    }
}
