$(function() {
  "use strict"

  $(".datetimepickerfield").each(function() {
    $(this).datetimepicker({
      format: $("body").attr("data-datetimeformat"),
      locale: $("body").attr("data-datetimelocale"),
      useCurrent: false,
      showClear: !$(this).prop("required"),
      icons: {
        time: "fa fa-clock-o",
        date: "fa fa-calendar",
        up: "fa fa-chevron-up",
        down: "fa fa-chevron-down",
        previous: "fa fa-chevron-left",
        next: "fa fa-chevron-right",
        today: "fa fa-image",
        clear: "fa fa-trash",
        close: "fa fa-times",
      },
    })
  })

  $(".datepickerfield").each(function() {
    var opts = {
      format: $("body").attr("data-dateformat"),
      locale: $("body").attr("data-datetimelocale"),
      useCurrent: false,
      showClear: !$(this).prop("required"),
      icons: {
        time: "fa fa-clock-o",
        date: "fa fa-calendar",
        up: "fa fa-chevron-up",
        down: "fa fa-chevron-down",
        previous: "fa fa-chevron-left",
        next: "fa fa-chevron-right",
        today: "fa fa-image",
        clear: "fa fa-trash",
        close: "fa fa-times",
      },
    }
    $(this).datetimepicker(opts)
  })

  $(".datetimepicker[data-date-after], .datepickerfield[data-date-after]").each(
    function() {
      var later_field = $(this),
        earlier_field = $($(this).attr("data-date-after")),
        update = function() {
          var earlier = earlier_field.data("DateTimePicker").date(),
            later = later_field.data("DateTimePicker").date()
          if (earlier === null) {
            earlier = false
          } else if (
            later !== null &&
            later.isBefore(earlier) &&
            !later.isSame(earlier)
          ) {
            later_field.data("DateTimePicker").date(earlier.add(1, "h"))
          }
          later_field.data("DateTimePicker").minDate(earlier)
        }
      update()
      earlier_field.on("dp.change", update)
    }
  )
})
