<?php
/* Smarty version 4.3.4, created on 2025-12-14 09:45:13
  from '/var/www/mds-cms/system/content/extensions/education/students_parents.tpl' */

/* @var Smarty_Internal_Template $_smarty_tpl */
if ($_smarty_tpl->_decodeProperties($_smarty_tpl, array (
  'version' => '4.3.4',
  'unifunc' => 'content_693e87296bb601_70948794',
  'has_nocache_code' => false,
  'file_dependency' => 
  array (
    'e94254e9c8403bb0f54f33dd345ca6a7ee731ea5' => 
    array (
      0 => '/var/www/mds-cms/system/content/extensions/education/students_parents.tpl',
      1 => 1765702366,
      2 => 'file',
    ),
  ),
  'includes' => 
  array (
    'file:main/datatable.tpl' => 1,
  ),
),false)) {
function content_693e87296bb601_70948794 (Smarty_Internal_Template $_smarty_tpl) {
?><button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#studentModal" onclick="openCreateModal('student', 'Добавить ученика')">Добавить ученика</button>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#parentModal" onclick="openCreateModal('parent', 'Добавить родителя')">Добавить родителя</button>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#student_parent_relModal" onclick="openCreateModal('student_parent_rel', 'Привязка ученика к родителю')">Привязать ученика к родителю</button>


<div class="modal fade" id="studentModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form id="studentForm" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="studentModalTitle"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">

                    <input type="hidden" id="student_id" name="id">

                    <div class="row mb-3">
                        <div class="col-6">
                            <label for="first_name" class="form-label">Имя *</label>
                            <input type="text" name="first_name" id="first_name" class="form-control" placeholder="">
                            <div class="text-danger small" id="error-first_name"></div>
                        </div>

                        <div class="col-6">
                            <label for="last_name" class="form-label">Фамилия *</label>
                            <input type="text" name="last_name" id="last_name" class="form-control" placeholder="">
                            <div class="text-danger small" id="error-last_name"></div>
                        </div>

                    </div>

                    <div class="row mb-3">

                        <div class="col-6">
                            <label for="age" class="form-label">Возраст *</label>
                            <input type="number" name="age" min="5" max="100" id="age" class="form-control" placeholder="">
                            <div class="text-danger small" id="error-age"></div>
                        </div>

                        <div class="col-6">
                            <label for="email" class="form-label">Эл. почта (опционально)</label>
                            <input type="email" name="email" id="email" class="form-control" placeholder="">
                            <div class="text-danger small" id="error-email"></div>
                        </div>

                    </div>

                    <div class="row">

                        <div class="col-6">
                            <label for="gender" class="form-label">Пол *</label>
                            <select id="gender" name="gender" class="form-control">
                                <option value="male">Мужской</option>
                                <option value="female">Женский</option>
                            </select>
                            <div class="text-danger small" id="error-gender"></div>
                        </div>

                    </div>

                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
                    <button type="submit" class="btn btn-primary" id="roleSubmitBtn">Сохранить</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="modal fade" id="parentModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form id="parentForm" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="parentModalTitle"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">

                    <input type="hidden" id="parent_id" name="id">

                    <div class="row mb-3">
                        <div class="col-6">
                            <label for="first_name" class="form-label">Имя *</label>
                            <input type="text" name="first_name" id="first_name" class="form-control" placeholder="">
                            <div class="text-danger small" id="parent-error-first_name"></div>
                        </div>

                        <div class="col-6">
                            <label for="last_name" class="form-label">Фамилия *</label>
                            <input type="text" name="last_name" id="last_name" class="form-control" placeholder="">
                            <div class="text-danger small" id="parent-error-last_name"></div>
                        </div>

                    </div>

                    <div class="row mb-3">

                        <div class="col-6">
                            <label for="age" class="form-label">Возраст</label>
                            <input type="number" name="age" min="5" max="100" id="age" class="form-control" placeholder="">
                            <div class="text-danger small" id="parent-error-age"></div>
                        </div>

                        <div class="col-6">
                            <label for="email" class="form-label">Эл. почта *</label>
                            <input type="email" name="email" id="email" class="form-control" placeholder="">
                            <div class="text-danger small" id="parent-error-email"></div>
                        </div>

                    </div>

                    <div class="row">

                        <div class="col-6">
                            <label for="gender" class="form-label">Пол *</label>
                            <select id="gender" name="gender" class="form-control">
                                <option value="male">Мужской</option>
                                <option value="female">Женский</option>
                            </select>
                            <div class="text-danger small" id="parent-error-gender"></div>
                        </div>

                    </div>

                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
                    <button type="submit" class="btn btn-primary" id="roleSubmitBtn">Сохранить</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="modal fade" id="student_parent_relModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form id="student_parent_relForm" method="post">
                <div class="modal-header">
                    <h5 class="modal-title" id="student_parent_relModalTitle"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">

                    <input type="hidden" id="student_parent_rel_id" name="id">

                    <div class="row mb-3">
                        <div class="col-6">
                            <label for="selected_parent" class="form-label">Родитель *</label>
                            <select id="selected_parent" name="selected_parent" class="form-control">
                                <option value="" selected disabled>-- Не выбран --</option>
                                <?php
$_from = $_smarty_tpl->smarty->ext->_foreach->init($_smarty_tpl, $_smarty_tpl->tpl_vars['parentsDataList']->value, 'parentsDataAssoc');
$_smarty_tpl->tpl_vars['parentsDataAssoc']->do_else = true;
if ($_from !== null) foreach ($_from as $_smarty_tpl->tpl_vars['parentsDataAssoc']->value) {
$_smarty_tpl->tpl_vars['parentsDataAssoc']->do_else = false;
?>
                                    <option value="<?php echo $_smarty_tpl->tpl_vars['parentsDataAssoc']->value['id'];?>
"><?php echo $_smarty_tpl->tpl_vars['parentsDataAssoc']->value['name'];?>
</option>
                                <?php
}
$_smarty_tpl->smarty->ext->_foreach->restore($_smarty_tpl, 1);?>
                            </select>
                            <div class="text-danger small" id="student_parent-error-selected_parent"></div>
                        </div>

                        <div class="col-6">
                            <label for="selected_student" class="form-label">Ученик *</label>
                            <select id="selected_student" name="selected_student" class="form-control">
                                <option value="" selected disabled>-- Не выбран --</option>
                                <?php
$_from = $_smarty_tpl->smarty->ext->_foreach->init($_smarty_tpl, $_smarty_tpl->tpl_vars['studentsDataList']->value, 'studentsDataAssoc');
$_smarty_tpl->tpl_vars['studentsDataAssoc']->do_else = true;
if ($_from !== null) foreach ($_from as $_smarty_tpl->tpl_vars['studentsDataAssoc']->value) {
$_smarty_tpl->tpl_vars['studentsDataAssoc']->do_else = false;
?>
                                    <option value="<?php echo $_smarty_tpl->tpl_vars['studentsDataAssoc']->value['id'];?>
"><?php echo $_smarty_tpl->tpl_vars['studentsDataAssoc']->value['name'];?>
</option>
                                <?php
}
$_smarty_tpl->smarty->ext->_foreach->restore($_smarty_tpl, 1);?>
                            </select>
                            <div class="text-danger small" id="student_parent-error-selected_student"></div>
                        </div>

                    </div>

                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
                    <button type="submit" class="btn btn-primary" id="roleSubmitBtn">Сохранить</button>
                </div>
            </form>
        </div>
    </div>
</div>


<div class="col-12">
    <?php $_smarty_tpl->_subTemplateRender('file:main/datatable.tpl', $_smarty_tpl->cache_id, $_smarty_tpl->compile_id, 0, $_smarty_tpl->cache_lifetime, array('tableTitle'=>"Все ученики / родители:",'theads'=>array("id",$_smarty_tpl->tpl_vars['lang']->value['datatable']['name'],$_smarty_tpl->tpl_vars['lang']->value['datatable']['date'],"parents",$_smarty_tpl->tpl_vars['lang']->value['datatable']['actions']),'data'=>$_smarty_tpl->tpl_vars['students']->value), 0, false);
?>
</div>


<?php echo '<script'; ?>
>

    $(document).ready(function() {

        $(document).on("click", ".delete-ico", function (e) {
            e.preventDefault();

            const dataId = $(this).data("id");

            if (!confirm("Вы уверены, что хотите удалить ученика?")) {
                return;
            }

            $.ajax({
                url: 'engine/ajax/lib/education/students/delete_student.php',
                type: 'POST',
                dataType: 'json',
                data:  {id: dataId} ,
                success: function (response) {

                    if (response) {
                        window.location.reload();
                    } else {

                    }

                },
                error: function (xhr, status, error) {
                    return;
                }
            });
        });

        $(".edit-ico").on("click", function () {
            debugger;

            const dataId = $(this).data("id");

            if (dataId) {
                $.ajax({
                    url: 'engine/ajax/lib/education/students/get_student_data.php',
                    type: 'POST',
                    dataType: 'json',
                    data: {id: dataId} ,
                    success: function (response) {

                        if (response) {
                            openEditStudentModal(response)
                        }

                    },
                    error: function (xhr, status, error) {
                        return;
                    }
                });
            }
        });

        $("#studentForm").on("submit", function(e) {
            debugger;
            e.preventDefault();

            let formData = $(this).serialize();
            let id = $("#student_id").val();
            let url = id ? "engine/ajax/lib/education/students/update_student.php" : "engine/ajax/lib/education/students/create_student.php";

            $(".text-danger.small").text(""); // сброс ошибок

            $.ajax({
                url: url,
                type: "POST",
                data: formData,
                dataType: "json",
                success: function(response) {
                    if (response.success) {
                        location.reload(); // или обнови таблицу
                    } else if (response.errors) {
                        $.each(response.errors, function(field, msg) {
                            $("#error-" + field).text(msg);
                        });
                    }
                }
            });
        });


        $("#parentForm").on("submit", function(e) {
            debugger;
            e.preventDefault();

            let formData = $(this).serialize();
            let id = $("#parent_id").val();
            let url = id ? "engine/ajax/lib/education/parents/update_parent.php" : "engine/ajax/lib/education/parents/create_parent.php";

            $(".text-danger.small").text(""); // сброс ошибок

            $.ajax({
                url: url,
                type: "POST",
                data: formData,
                dataType: "json",
                success: function(response) {
                    if (response.success) {
                        location.reload(); // или обнови таблицу
                    } else if (response.errors) {
                        $.each(response.errors, function(field, msg) {
                            $("#parent-error-" + field).text(msg);
                        });
                    }
                }
            });
        });

        $("#student_parent_relForm").on("submit", function(e) {
            debugger;
            e.preventDefault();

            let formData = $(this).serialize();
            let id = $("#student_parent_rel_id").val();
            let url = id ? "engine/ajax/lib/education/students_parents/update_student_parent_rel.php" : "engine/ajax/lib/education/students_parents/create_student_parent_rel.php";

            $(".text-danger.small").text(""); // сброс ошибок

            $.ajax({
                url: url,
                type: "POST",
                data: formData,
                dataType: "json",
                success: function(response) {
                    if (response.success) {
                        location.reload(); // или обнови таблицу
                    } else if (response.errors) {
                        $.each(response.errors, function(field, msg) {
                            $("#student_parent-error-" + field).text(msg);
                        });
                    }
                }
            });
        });

        function openEditStudentModal(studentData) {
            debugger;
            $("#studentModalTitle").text("Редактировать ученика");

            $("#studentModal").find("#student_id").val(studentData.id);
            $("#studentModal").find("#first_name").val(studentData.first_name);

            $("#studentModal").find("#age").val(studentData.age);
            $("#studentModal").find("#gender").val(studentData.gender);


            $("#studentModal").find("#last_name").val(studentData.last_name);
            $("#studentModal").find("#email").val(studentData.email);

            $("#studentModal").find(".text-danger.small").text("");
            $("#studentModal").modal("show");
        }
    });
    function openCreateModal(object, title) {
        $('#'+object+"ModalTitle").text(title);
        $('#'+object+"Form")[0].reset();
        $('#'+object+"_id").val("");
        $(".text-danger.small").text("");
        $('#'+object+"Modal").modal("show");
    }
<?php echo '</script'; ?>
><?php }
}
