<?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.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Simulatore contabilità
 *
 * @package    mod_mtcontabilita
 * @copyright  2026 Mediatouch 2000 Srl <https://www.mediatouch.it>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 * @author     Marco Cappuccio <m.cappuccio@mediatouch.it>
 */

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

require_once($CFG->libdir.'/formslib.php');
require_once($CFG->dirroot.'/mod/mtcontabilita/locallib.php');

/**
 * step1_form class
 */
class step1_form extends moodleform {

    /**
     * definition
     */
    public function definition() {
        global $DB;

        $mform = $this->_form;
        $data    = $this->_customdata['data'];
        $mtc = new \mtcontabilita();

        $bread = false;
        if ($data->id > 0) {
            if ($DB->record_exists('mtcontabilita_itemdet', ['listid' => $data->id])) {
                $bread = true;
            }
        }
        $mform->addElement('hidden', 'id');
        $mform->setType('id', PARAM_INT);

        $mform->addElement('hidden', 'cid');
        $mform->setType('cid', PARAM_INT);

        $mform->addElement('hidden', 'courseid');
        $mform->setType('courseid', PARAM_INT);

        $mform->addElement('text', 'name', get_string('denominazioneconto', 'mtcontabilita'));
        $mform->setType('name', PARAM_RAW);
        $mform->addRule('name', null, 'required', null, 'client');

        $pars = ['' => get_string('choose'), 'CE' => get_string('ce', 'mtcontabilita'), 'SP' => get_string('sp', 'mtcontabilita')];
        $mform->addElement('select', 'scheme', get_string('scheme', 'mtcontabilita'), $pars);
        $mform->addRule('scheme', null, 'required', null, 'client');
        if ($bread) {
            $mform->hardFreeze('scheme');
        }
        // 1,2.
        if ($bread && $data->scheme == "CE" || !$bread) {
            $mform->addElement('select', 'nature1', get_string('nature', 'mtcontabilita'), $mtc->get_nature_list(-1, 'ce'));
        }
        if ($bread && $data->scheme == "CE") {
            $mform->hardFreeze('nature1');
        }
        // 3,4,5.
        if ($bread && $data->scheme == "SP" || !$bread) {
            $mform->addElement('select', 'nature2', get_string('nature', 'mtcontabilita'), $mtc->get_nature_list(-1, 'sp'));
        }
        if ($bread && $data->scheme == "SP") {
            $mform->hardFreeze('nature2');
        }
        if (!$bread) {
            $mform->hideIf('nature1', 'scheme', 'neq', "CE");
            $mform->hideIf('nature2', 'scheme', 'neq', "SP");
        }

        $opt0 = ['' => get_string('choose')];

        if (!$bread) {
            for ($i = 1; $i <= 2; $i++) {
                $opt = $DB->get_records_menu('mtcontabilita_listmc', ['nature' => $i], 'sortorder', 'macroclasse, description');
                $mform->addElement('select', 'mc'.$i, get_string('macroclasse', 'mtcontabilita'), $opt0 + $opt);
                $mform->hideIf('mc'.$i, 'nature1', 'neq', $i);
                $mform->hideIf('mc'.$i, 'scheme', 'neq', 'CE');
                $mform->hideIf('mc'.$i, 'lang', 'neq', 'it');
            }
            for ($i = 3; $i <= 5; $i++) {
                $opt = $DB->get_records_menu('mtcontabilita_listmc', ['nature' => $i], 'sortorder', 'macroclasse, description');
                $mform->addElement('select', 'mc'.$i, get_string('macroclasse', 'mtcontabilita'), $opt0 + $opt);
                $mform->hideIf('mc'.$i, 'nature2', 'neq', $i);
                $mform->hideIf('mc'.$i, 'scheme', 'neq', 'SP');
                $mform->hideIf('mc'.$i, 'lang', 'neq', 'it');
            }
        } else {
            $dn = $data->nature;
            $opt = $DB->get_records_menu('mtcontabilita_listmc', ['nature' => $dn], 'sortorder', 'macroclasse, description');
            $mform->addElement('select', 'mc'.$dn, get_string('macroclasse', 'mtcontabilita'), $opt0 + $opt);
            $mform->hardFreeze('mc'.$dn);
            $mform->hideIf('mc'.$dn, 'lang', 'neq', 'it');

        }
        $mform->addElement('select', 'lang', get_string('lang', 'mtcontabilita'), ['' => get_string('choose'), 'it' => 'it', 'en' => 'en']);
        $mform->addRule('lang', null, 'required', null, 'client');
        if ($bread) {
            $mform->hardFreeze('lang');
        }
        $this->add_action_buttons(true);
        $this->set_data($data);
    }
}
