<?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>
 */

require_once('../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once('locallib.php');
require_once('edit_form.php');

$id = required_param('id', PARAM_INT);
$cid = required_param('cid', PARAM_INT);

$course = get_course($cid);
require_course_login($course, false);
$context = context_course::instance($course->id);
if (!has_capability('mod/mtcontabilita:addinstance', $context)) {
    throw new \moodle_exception('nopermissions', 'error', '', get_string('nopermissions', 'mod_mtcontabilita'));
}

$PAGE->set_context($context);
$PAGE->set_url('/mod/mtcontabilita/manage/');
require_capability('mod/mtcontabilita:view', $context);

$returnurl = new moodle_url('/mod/mtcontabilita/manage/index.php', ['id' => $cid]);
// *********************************************************************************************************************
if (!$data = $DB->get_record('mtcontabilita_list', ['id' => $id])) {
    $data = (object)[];
    $data->id = 0;
    $data->courseid = $cid;
} else {
    $data->scheme = strtoupper($data->scheme);
    if ($data->nature == 1 || $data->nature == 2) {
        $data->nature1 = $data->nature;
    } else if ($data->nature == 3 || $data->nature == 4 || $data->nature == 5) {
        $data->nature2 = $data->nature;
    }
    $data->{"mc".$data->nature} = $data->macroclasse;
}
$data->cid = $cid;

$mform1 = new step1_form( null, ['data' => $data ] );
if ($dt = $mform1->is_cancelled()) {
    redirect($returnurl);
} else if ($dt = $mform1->get_data()) {
    // echo "<pre>"; var_dump($dt); die();
    unset($dt->cid);

    if ($dt->scheme == "CE") {
        $dt->nature = $dt->nature1;
        $dt->macroclasse = @$dt->{'mc'.$dt->nature1};
        unset($dt->{'mc'.$dt->nature1});
    } else {
        $dt->nature = $dt->nature2;
        $dt->macroclasse = @$dt->{'mc'.$dt->nature2};
        unset($dt->{'mc'.$dt->nature2});
    }

    if ($dt->id == 0) {
        $dt->timecreated = time();
        $dt->usercreated = $USER->id;
        unset($dt->id);
        if (!isset($dt->macroclasse)) {
            $dt->macroclasse = "";
        }
        $id = $DB->insert_record('mtcontabilita_list', $dt);
        $params = [
            'context' => $context,
            'objectid' => $id,
            'courseid' => $cid,
        ];
        $event = \mod_mtcontabilita\event\add_conto::create($params);
        $event->trigger();
    } else {
        $DB->update_record('mtcontabilita_list', $dt);
        $params = [
            'context' => $context,
            'objectid' => $id,
            'courseid' => $cid,
        ];
        $event = \mod_mtcontabilita\event\update_conto::create($params);
        $event->trigger();
    }
    redirect('index.php?id='.$cid);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('account', 'mtcontabilita'));
$mform1->display();

echo $OUTPUT->footer();
