import { Misc } from "./misc.js";

/**
 * Extend the base Dialog entity by defining a custom window to perform roll.
 * @extends {Dialog}
 */
export class RdDCalendrierEditeur extends Dialog {

  /* -------------------------------------------- */
  constructor(html, calendrier, calendrierData) {
    let dialogConf = {
      content: html,
      title: "Editeur de date/heure",
      buttons: {
        save: { label: "Enregistrer", callback: html => this.fillData() }
      },
      default: "save"
    };
    let dialogOptions = { classes: ["rdd-dialog-calendar-editor"], width: 400, height: 'fit-content', 'z-index': 99999 }
    super(dialogConf, dialogOptions)

    this.calendrier = calendrier;
    this.calendrierData = calendrierData;
  }

  activateListeners(html) {
    super.activateListeners(html);
    this.html = html;
    this.html.find("input[name='nomMois']").val(this.calendrierData.moisKey);
    this.html.find("select[name='nomHeure']").val(this.calendrierData.heureKey);
    this.html.find("select[name='jourMois']").val(this.calendrierData.jourMois);
    this.html.find("select[name='minutesRelative']").val(this.calendrierData.minutesRelative);
    this.html.find("select[name='annee']").val(this.calendrierData.annee);
  }

  /* -------------------------------------------- */
  fillData() {
    this.calendrierData.annee = this.html.find("input[name='annee']").val();
    this.calendrierData.moisKey = this.html.find("select[name='nomMois']").val();
    this.calendrierData.heureKey = this.html.find("select[name='nomHeure']").val();
    this.calendrierData.jourMois = this.html.find("select[name='jourMois']").val();
    this.calendrierData.minutesRelative = this.html.find("select[name='minutesRelative']").val();

    this.calendrier.saveEditeur(this.calendrierData)
  }

  /* -------------------------------------------- */
  updateData(calendrierData) {
    this.calendrierData = duplicate(calendrierData);
  }

}
