<script>
     $(function() {

          if (!isOutlookLoggedIn()) {
               $('body').find('#showOutlookCheckbox').remove();
          } else {
               acquireTokenPopupAndCallMSGraph();
          }

          var div_name = $('#div_name');
          var div_email = $('#div_email');
          var div_phone = $('#div_phone');

          init_editor('textarea[name="notes"]', {
               menubar: false,
          });

          initAppointmentScheduledDates();

          $('.modal').on('hidden.bs.modal', function(e) {
               $('.xdsoft_datetimepicker').remove();
               $(this).removeData();
          });

          $('#appointment-form').on('submit', function(e) {
               e.preventDefault();
          });

          $('body').on('click', '#outlook-checkbox', function() {
               $(this).attr('checked', $(this).is(':checked') ? true : false);
          });

          appValidateForm($("#appointment-form"), {
               subject: "required",
               description: "required",
               date: "required",
               enddate: "required",
               rel_type: "required",
               'attendees[]': {
                    required: true,
                    minlength: 1
               }
          }, apply_appointments_form_data, {
               'attendees[]': "Please select at least 1 staff member"
          });

          function apply_appointments_form_data(form) {
               $('button[type="submit"], button.close_btn').prop('disabled', true);
               $('button[type="submit"]').html('<i class="fa fa-refresh fa-spin fa-fw"></i>');
               $('#appointment-form .modal-body').addClass('filterBlur');
               $('.modal-title').html("<?= _l('appointment_please_wait'); ?>");

               var formSerializedData = $(form).serializeArray();

               var isOutlookChecked = document.getElementById('outlook-checkbox') || null;

               var data = $(form).serialize();
               var url = form.action;

               $.post(url, data).done(function(response) {
                    if (response.result) {
                         if (isOutlookLoggedIn()) {
                              if (isOutlookChecked !== null && isOutlookChecked.checked) {
                                   outlookAddOrUpdateEvent(formSerializedData);
                              }
                         }
                         if (response.result && (isOutlookChecked == null || !isOutlookChecked.checked)) {
                              alert_float('success', "<?= _l("appointment_created"); ?>");
                              setTimeout(() => {
                                   window.location.reload();
                              }, 1000);
                         }
                    }
               });
               return false;
          }

          $("body").on('change', '#rel_type', function() {
               var optionSelected = $("option:selected", this).attr('id');
               var contact_id = $("#contact_id");
               var select_contacts = $('#select_contacts');
               var div_nep = $('#div_name, #div_email, #div_phone');
               var dninput = $('#div_name input, #div_email input, #div_phone input');
               var rel_id_wrapper = $('body').find('#appointment-form #rel_id_wrapper');
               var lead_select = $('body').find('#appointment-form #rel_id');

               if (optionSelected == 'external') {
                    dninput.val('').attr('disabled', false).attr('required', true);
                    $('#div_phone input').attr('required', false);
                    div_nep.removeClass('hidden');
                    select_contacts.addClass('hidden');
                    contact_id.selectpicker("refresh").attr('required', false);;
                    rel_id_wrapper.addClass('hide');
                    lead_select.attr('required', false).val('default').selectpicker("refresh");

               } else if (optionSelected == 'internal') {
                    contact_id.val('default').selectpicker("refresh").attr('required', true);
                    div_nep.addClass('hidden').attr('required', false);
                    select_contacts.removeClass('hidden');
                    rel_id_wrapper.addClass('hide');
                    lead_select.attr('required', false).val('default').selectpicker("refresh");
               } else {
                    dninput.attr('required', false);
                    contact_id.val('default').selectpicker("refresh").attr('required', false);
                    div_nep.addClass('hidden').attr('required', false);
                    select_contacts.addClass('hidden');
                    rel_id_wrapper.removeClass('hide');
                    lead_select.attr('required', true);
               }
          });

          $('body').on('change', '#contact_id, #rel_id', function() {

               var contact_id = $("option:selected", this).val();

               if (contact_id == "" && div_name.children('input').is(":visible")) {
                    div_name.children('input').val('');
                    div_email.children('input').val('');
                    div_phone.children('input').val('');
               }

               var url = "<?= admin_url('appointly/appointments/fetch_contact_data'); ?>";

               $.post(url, {
                    contact_id: contact_id,
                    lead: ($(this).attr('id') == 'rel_id') ? true : false
               }).done(function(response) {
                    if (response !== null) {

                         $('#div_name, #div_email, #div_phone').removeClass('hidden');

                         var full_name = (typeof(response.firstname) != 'undefined') ? response.firstname + ' ' + response.lastname : response.name;
                         var email = response.email;
                         var phone = response.phonenumber;

                         div_name.children('input').val(full_name).attr('disabled', true);

                         div_email.children('input').val(email).attr('disabled', (response.email == '') ? false : true);
                         div_email.children('input[name="email"]').val(email).attr('required', (response.email == '') ? true : false);
                         div_phone.children('input').val(phone).attr('disabled', true);
                    }
               });
          });

          init_selectpicker();

          // Leads functionality
          var _rel_id = $('#rel_id'),
               _rel_type = $('#rel_lead_type');

          // Items ajax search for leads
          var serverData = {};

          init_ajax_search('items', '#item_select.ajax-search', undefined, admin_url + 'items/search');
          serverData.rel_id = _rel_id.val();
          init_ajax_search(_rel_type.val(), _rel_id, serverData);

     });
     
     
     
$(document).ready(function() {
    var addressDropdown = $('#address');
    var attendeesDropdown = $('#attendees\\[\\]'); // using jQuery to handle special characters in ID

    // Listen for changes using the Bootstrap Select specific event
    addressDropdown.on('changed.bs.select', function() {
        console.log("Address dropdown changed to:", $(this).val()); // Debugging change detection

        var selectedValue = $(this).val();
        var valuesToSelect = attendeesDropdown.val() || []; // Start with existing selections if needed

        // Check the selected value and adjust attendeesDropdown accordingly
        switch (selectedValue) {
            case "Meeting Room 1":
                if (!valuesToSelect.includes("8")) { // Ensure "8" is not already selected
                    valuesToSelect.push("8");
                }
                break;
              
            case "Meeting Room 5":
                if (!valuesToSelect.includes("9")) { // Ensure "9" is not already selected
                    valuesToSelect.push("9");
                }
                break;
             case "Seminar Room 1":
                if (!valuesToSelect.includes("10")) { // Ensure "9" is not already selected
                    valuesToSelect.push("10");
                }
                break;
                case "Seminar Room 2":
                if (!valuesToSelect.includes("11")) { // Ensure "9" is not already selected
                    valuesToSelect.push("11");
                }
                break;
             case "Seminar Room 3":
                if (!valuesToSelect.includes("12")) { // Ensure "9" is not already selected
                    valuesToSelect.push("12");
                }
                break;
                 case "Auditorium":
                if (!valuesToSelect.includes("13")) { 
                    valuesToSelect.push("13");
                }
                break;
                 case "Case Study Room 1":
                if (!valuesToSelect.includes("14")) {
                    valuesToSelect.push("14");
                }
                break;
                  case "Case Study Room 2":
                if (!valuesToSelect.includes("15")) { 
                    valuesToSelect.push("15");
                }
                break;
                 case "Lecture Hall 1":
                if (!valuesToSelect.includes("16")) {
                    valuesToSelect.push("16");
                }
                break;
                 case "Lecture Hall 2":
                if (!valuesToSelect.includes("17")) { 
                    valuesToSelect.push("17");
                }
                break;
                 case "Lecture Hall 3":
                if (!valuesToSelect.includes("18")) { 
                    valuesToSelect.push("18");
                }
                break;
                 case "The Lounge":
                if (!valuesToSelect.includes("19")) {
                    valuesToSelect.push("19");
                }
                break;
                 case "Creative Lounge":
                if (!valuesToSelect.includes("20")) {
                    valuesToSelect.push("20");
                }
                break;
                 case "Faculty Lounge":
                if (!valuesToSelect.includes("21")) { 
                    valuesToSelect.push("21");
                }
                break;
                 case "Council Room":
                if (!valuesToSelect.includes("22")) { 
                    valuesToSelect.push("22");
                }
                break;
                case "Classroom 1":
                if (!valuesToSelect.includes("23")) { 
                    valuesToSelect.push("23");
                }
                break;
                 case "Classroom 2":
                if (!valuesToSelect.includes("24")) {
                    valuesToSelect.push("24");
                }
                break;
                 case "Classroom 3":
                if (!valuesToSelect.includes("25")) {
                    valuesToSelect.push("25");
                }
                break;
                 case "Classroom 4":
                if (!valuesToSelect.includes("26")) { 
                    valuesToSelect.push("26");
                }
                break;
                   case "Meeting Room 3":
                if (!valuesToSelect.includes("30")) { // Ensure "8" is not already selected
                    valuesToSelect.push("30");
                }
                break;
            default:
                // Optionally handle default case, like clearing other specific values
                valuesToSelect = valuesToSelect.filter(value => value !== "8" && value !== "9" && value !== "10" && value !== "11" && value !== "12" && value !== "13" && value !== "14" && value !== "15" && value !== "16" && value !== "17" && value !== "18" && value !== "19" && value !== "20" && value !== "21" && value !== "22" && value !== "23" && value !== "24" && value !== "25" && value !== "26" && value !== "30");
                break;
        }

        attendeesDropdown.selectpicker('val', valuesToSelect).selectpicker('refresh');
        console.log("Updated attendees dropdown:", attendeesDropdown.val());
    });
});



</script>