2021-05-07 17:27:02 +02:00
|
|
|
import { HtmlUtility } from "./html-utility.js";
|
|
|
|
|
|
|
|
export class DialogItemVente extends Dialog {
|
|
|
|
|
2023-01-01 22:16:26 +01:00
|
|
|
static async display({ item, callback, quantiteMax = undefined }) {
|
|
|
|
const quantite = quantiteMax ?? item.getQuantite() ?? 1;
|
2022-12-23 00:34:17 +01:00
|
|
|
const isOwned = item.isOwned;
|
2021-05-07 17:27:02 +02:00
|
|
|
const venteData = {
|
2022-09-07 18:47:56 +02:00
|
|
|
item: item,
|
2023-01-01 22:16:26 +01:00
|
|
|
alias: item.actor?.name ?? game.user.name,
|
|
|
|
vendeurId: item.actor?.id ,
|
2023-01-03 01:37:50 +01:00
|
|
|
prixOrigine: item.calculerPrixCommercant(),
|
|
|
|
prixUnitaire: item.calculerPrixCommercant(),
|
|
|
|
prixLot: item.calculerPrixCommercant(),
|
2021-05-07 17:27:02 +02:00
|
|
|
tailleLot: 1,
|
2021-12-10 01:18:59 +01:00
|
|
|
quantiteNbLots: quantite,
|
|
|
|
quantiteMaxLots: quantite,
|
2022-12-23 00:34:17 +01:00
|
|
|
quantiteMax: quantite,
|
2023-01-01 22:16:26 +01:00
|
|
|
quantiteIllimite: !isOwned || quantiteMax == undefined,
|
2022-12-23 00:34:17 +01:00
|
|
|
isOwned: isOwned,
|
2021-05-07 17:27:02 +02:00
|
|
|
};
|
|
|
|
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-vente.html`, venteData);
|
2022-10-07 19:05:56 +02:00
|
|
|
return new DialogItemVente(venteData, html, callback).render(true);
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(venteData, html, callback) {
|
2022-11-16 02:46:26 +01:00
|
|
|
let options = { classes: ["dialogvente"], width: 400, height: 'fit-content', 'z-index': 99999 };
|
2021-05-07 17:27:02 +02:00
|
|
|
|
|
|
|
let conf = {
|
|
|
|
title: "Proposer",
|
|
|
|
content: html,
|
|
|
|
default: "proposer",
|
2021-06-04 19:35:27 +02:00
|
|
|
buttons: { "proposer": { label: "Proposer", callback: it => { this.onProposer(it); } } }
|
2021-05-07 17:27:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
super(conf, options);
|
|
|
|
this.callback = callback;
|
|
|
|
this.venteData = venteData;
|
|
|
|
}
|
|
|
|
|
|
|
|
activateListeners(html) {
|
|
|
|
super.activateListeners(html);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html = html;
|
|
|
|
HtmlUtility._showControlWhen(this.html.find(".quantiteNbLots"), !this.venteData.quantiteIllimite)
|
2021-05-07 17:27:02 +02:00
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find(".tailleLot").change(event => this.setTailleLot(Number(event.currentTarget.value)));
|
|
|
|
this.html.find(".quantiteNbLots").change(event => this.setNbLots(Number(event.currentTarget.value)));
|
|
|
|
this.html.find(".quantiteIllimite").change(event => this.setQuantiteIllimite(event.currentTarget.checked));
|
|
|
|
this.html.find(".prixLot").change(event => this.setPrixLot(Number(event.currentTarget.value)));
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 02:00:31 +01:00
|
|
|
async onProposer(it) {
|
|
|
|
await this.html.find(".tailleLot").change();
|
|
|
|
await this.html.find(".quantiteNbLots").change();
|
|
|
|
await this.html.find(".quantiteIllimite").change();
|
|
|
|
await this.html.find(".prixLot").change();
|
|
|
|
this.callback(this.venteData);
|
2022-12-23 00:34:17 +01:00
|
|
|
}
|
2022-12-09 02:00:31 +01:00
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2021-05-07 17:27:02 +02:00
|
|
|
setPrixLot(prixLot) {
|
|
|
|
this.venteData.prixLot = prixLot;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTailleLot(tailleLot) {
|
|
|
|
// recalculer le prix du lot
|
|
|
|
if (tailleLot != this.venteData.tailleLot) {
|
|
|
|
this.venteData.prixLot = (tailleLot * this.venteData.prixOrigine).toFixed(2);
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find(".prixLot").val(this.venteData.prixLot);
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
this.venteData.tailleLot = tailleLot;
|
2022-12-23 00:34:17 +01:00
|
|
|
// recalculer le nombre de lots max
|
|
|
|
this.venteData.quantiteMaxLots = Math.floor(this.venteData.quantiteMax / tailleLot);
|
|
|
|
this.venteData.quantiteNbLots = Math.min(this.venteData.quantiteMaxLots, this.venteData.quantiteNbLots);
|
|
|
|
this.html.find(".quantiteNbLots").val(this.venteData.quantiteNbLots);
|
|
|
|
this.html.find(".quantiteNbLots").attr("max", this.venteData.quantiteMaxLots)
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setNbLots(nbLots) {
|
|
|
|
if (this.venteData.isOwned) {
|
|
|
|
nbLots = Math.max(0, Math.min(nbLots, this.venteData.quantiteMaxLots));
|
|
|
|
}
|
|
|
|
this.venteData.quantiteNbLots = nbLots;
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find(".quantiteNbLots").val(this.venteData.quantiteNbLots);
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setQuantiteIllimite(checked) {
|
|
|
|
this.venteData.quantiteIllimite = checked;
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html.find(".label-quantiteIllimite").text(this.venteData.quantiteIllimite ? "Illimités" : "disponibles");
|
|
|
|
HtmlUtility._showControlWhen(this.html.find(".quantiteNbLots"), !this.venteData.quantiteIllimite)
|
2021-05-07 17:27:02 +02:00
|
|
|
}
|
|
|
|
}
|