window['content_list_model_name'] = 'knowledge_area_teaching_plan';
window['content_list_submodel_name'] = 'teaching_plan';

$(function () {
  'use strict';

  var $unity = $('#knowledge_area_teaching_plan_teaching_plan_attributes_unity_id');
  var $grade = $('#knowledge_area_teaching_plan_teaching_plan_attributes_grade_id');
  var $knowledgeAreas = $('#knowledge_area_teaching_plan_knowledge_area_ids');
  var $schoolTermType = $('#knowledge_area_teaching_plan_teaching_plan_attributes_school_term_type_id');
  var $schoolTermContainer = $('#school-term-container');
  var $schoolTerm = $('#knowledge_area_teaching_plan_teaching_plan_attributes_school_term_type_step_id');
  var flashMessages = new FlashMessages();
  var contents_tags = $('#knowledge_area_teaching_plan_teaching_plan_attributes_contents_tags');
  var objectives_tags = $('#knowledge_area_teaching_plan_teaching_plan_attributes_objectives_tags');
  var idContentsCounter = 1;

  $schoolTermType.on('change', function () {
    updateSchoolTermInput($schoolTermType, $schoolTerm, $schoolTermContainer, flashMessages);
    $('#copy_plan_info').show();
  });

  updateSchoolTermInput($schoolTermType, $schoolTerm, $schoolTermContainer, flashMessages);

  validate_attachment_size($('#knowledge_area_teaching_plan'));

  contents_tags.on('change', function (e) {
    if (e.val.length) {
      var uniqueId = 'customId_' + idContentsCounter++;
      var content_description = e.val.join(", ");
      if (content_description.trim().length &&
        !$('input[type=checkbox][data-content_description="' + content_description + '"]').length) {

        var html = JST['templates/layouts/contents_list_manual_item']({
          id: uniqueId,
          description: content_description,
          model_name: 'knowledge_area_teaching_plan',
          submodel_name: 'teaching_plan'
        });

        $('#contents-list').append(html);
        $('.list-group.checked-list-box .list-group-item:not(.initialized)').each(initializeListEvents);
      } else {
        var content_input = $('input[type=checkbox][data-content_description="' + content_description + '"]');
        content_input.closest('li').show();
        content_input.prop('checked', true).trigger('change');
      }

      $('.knowledge_area_teaching_plan_teaching_plan_contents_tags .select2-input').val("");
    }
    $(this).select2('val', '');
  });

  objectives_tags.on('change', function (e) {
    if (e.val.length) {
      var uniqueId = 'customId_' + idContentsCounter++;
      var objective_description = e.val.join(", ");
      if (objective_description.trim().length &&
        !$('input[type=checkbox][data-objective_description="' + objective_description + '"]').length) {

        var html = JST['templates/layouts/objectives_list_manual_item']({
          id: uniqueId,
          description: objective_description,
          model_name: 'knowledge_area_teaching_plan',
          submodel_name: 'teaching_plan'
        });

        $('#objectives-list').append(html);
        $('.list-group.checked-list-box .list-group-item:not(.initialized)').each(initializeListEvents);
      } else {
        var objective_input = $('input[type=checkbox][data-objective_description="' + objective_description + '"]');
        objective_input.closest('li').show();
        objective_input.prop('checked', true).trigger('change');
      }

      $('.knowledge_area_teaching_plan_teaching_plan_objectives_tags .select2-input').val("");
    }
    $(this).select2('val', '');
  });

  if ($('#action_name').val() == 'show') {
    $('.list-group.checked-list-box .list-group-item').each(function () {
      $(this).off('click');
    });
  }

  $grade.on('change', function () {
    let grade_id = $grade.select2('val');
    let unity_id = $unity.select2('val');
    $knowledgeAreas.select2({ data: [] });

    if ($grade.val()) {
      return $.ajax({
        url: Routes.knowledge_areas_pt_br_path({
          grade_id: grade_id,
          unity_id: unity_id,
          format: 'json'
        }),
        success: handleFetchKnowledgeAreasByGradeSuccess,
        error: handleFetchKnowledgeAreasByGradeError
      });
    }
  });


  function handleFetchKnowledgeAreasByGradeSuccess(data) {
    let knowledge_areas = data;

    let selectedKnowledgeAreas = _.map(knowledge_areas, function (knowledge_area) {
      return { id: knowledge_area['id'], text: knowledge_area['description'] };
    });

    $knowledgeAreas.select2({ data: selectedKnowledgeAreas });

    // Define a primeira opção como selecionada por padrão
    $knowledgeAreas.val(selectedKnowledgeAreas[0].id).trigger('change');
  };

  function handleFetchKnowledgeAreasByGradeError() {
    flashMessages.error('Ocorreu um erro ao buscar as áreas de conhecimento da turma selecionada.');
  };

});

$(function () {
  $('textarea[maxLength]').maxlength();

  createSummerNote("textarea[id^=knowledge_area_teaching_plan_teaching_plan_attributes_methodology]", {
    toolbar: [
      ['font', ['bold', 'italic', 'underline', 'clear']],
    ]
  })
  createSummerNote("textarea[id^=knowledge_area_teaching_plan_teaching_plan_attributes_evaluation]", {
    toolbar: [
      ['font', ['bold', 'italic', 'underline', 'clear']],
    ]
  })
  createSummerNote("textarea[id^=knowledge_area_teaching_plan_teaching_plan_attributes_references]", {
    toolbar: [
      ['font', ['bold', 'italic', 'underline', 'clear']],
    ]
  })
});
