2024-06-01 00:17:39 +02:00
import { HtmlUtility } from "../html-utility.js" ;
import { RdDUtility } from "../rdd-utility.js" ;
import { ChatVente } from "./chat-vente.js" ;
2021-05-07 17:27:02 +02:00
export class DialogItemVente extends Dialog {
2024-06-01 00:17:39 +02:00
static async display ( { item , quantiteMax = undefined } ) {
2023-01-01 22:16:26 +01:00
const quantite = quantiteMax ? ? item . getQuantite ( ) ? ? 1 ;
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 ,
2024-10-04 21:08:44 +02:00
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 ,
2024-06-01 00:17:39 +02:00
nbLots : quantite ,
maxLots : quantite ,
2022-12-23 00:34:17 +01:00
quantiteMax : quantite ,
2024-06-01 00:17:39 +02:00
quantiteIllimite : item . isItemCommerce ( ) ? quantiteMax == undefined : ! item . parent ,
isOwned : item . parent ,
}
2021-05-07 17:27:02 +02:00
const html = await renderTemplate ( ` systems/foundryvtt-reve-de-dragon/templates/dialog-item-vente.html ` , venteData ) ;
2024-06-01 00:17:39 +02:00
return new DialogItemVente ( venteData , html ) . render ( true ) ;
2021-05-07 17:27:02 +02:00
}
2024-06-01 00:17:39 +02:00
constructor ( venteData , html ) {
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 . venteData = venteData ;
}
activateListeners ( html ) {
super . activateListeners ( html ) ;
2023-01-28 11:35:33 +01:00
2022-12-09 02:00:31 +01:00
this . html = html ;
this . html . find ( ".tailleLot" ) . change ( event => this . setTailleLot ( Number ( event . currentTarget . value ) ) ) ;
2024-06-01 00:17:39 +02:00
this . html . find ( ".nbLots" ) . change ( event => this . setNbLots ( Number ( event . currentTarget . value ) ) ) ;
2022-12-09 02:00:31 +01:00
this . html . find ( ".quantiteIllimite" ) . change ( event => this . setQuantiteIllimite ( event . currentTarget . checked ) ) ;
this . html . find ( ".prixLot" ) . change ( event => this . setPrixLot ( Number ( event . currentTarget . value ) ) ) ;
2023-01-28 11:35:33 +01:00
this . setQuantiteIllimite ( this . venteData . quantiteIllimite ) ;
2021-05-07 17:27:02 +02:00
}
2022-12-09 02:00:31 +01:00
async onProposer ( it ) {
2023-01-28 11:35:33 +01:00
this . updateVente ( this . getChoixVente ( ) ) ;
2024-06-01 00:17:39 +02:00
this . venteData [ "properties" ] = this . venteData . item . getProprietes ( ) ;
if ( this . venteData . isOwned ) {
if ( this . venteData . nbLots * this . venteData . tailleLot > this . venteData . quantiteMax ) {
ui . notifications . warn ( ` Vous avez ${ this . venteData . quantiteMax } ${ this . venteData . item . name } , ce n'est pas suffisant pour vendre ${ this . venteData . nbLots } de ${ this . venteData . tailleLot } ` )
return ;
}
}
await ChatVente . displayAchatVente ( this . venteData )
2022-12-23 00:34:17 +01:00
}
2022-12-09 02:00:31 +01:00
2023-01-28 11:35:33 +01:00
updateVente ( update ) {
2024-05-01 09:13:21 +02:00
foundry . utils . mergeObject ( this . venteData , update ) ;
2023-01-28 11:35:33 +01:00
}
getChoixVente ( ) {
return {
2024-06-01 00:17:39 +02:00
nbLots : Number ( this . html . find ( ".nbLots" ) . val ( ) ) ,
2023-01-28 11:35:33 +01:00
tailleLot : Number ( this . html . find ( ".tailleLot" ) . val ( ) ) ,
quantiteIllimite : this . html . find ( ".quantiteIllimite" ) . is ( ':checked' ) ,
prixLot : Number ( this . html . find ( ".prixLot" ) . val ( ) )
} ;
}
2022-12-09 02:00:31 +01:00
/* -------------------------------------------- */
2021-05-07 17:27:02 +02:00
setPrixLot ( prixLot ) {
this . venteData . prixLot = prixLot ;
}
setTailleLot ( tailleLot ) {
2023-01-28 11:35:33 +01:00
const maxLots = Math . floor ( this . venteData . quantiteMax / tailleLot ) ;
this . updateVente ( {
tailleLot ,
2024-06-01 00:17:39 +02:00
nbLots : Math . min ( maxLots , this . venteData . nbLots ) ,
maxLots : maxLots ,
2023-01-28 11:35:33 +01:00
prixLot : ( tailleLot * this . venteData . prixOrigine ) . toFixed ( 2 )
} ) ;
this . html . find ( ".prixLot" ) . val ( this . venteData . prixLot ) ;
2024-06-01 00:17:39 +02:00
this . html . find ( ".nbLots" ) . val ( this . venteData . nbLots ) ;
this . html . find ( ".nbLots" ) . attr ( "max" , this . venteData . maxLots )
2021-05-07 17:27:02 +02:00
}
setNbLots ( nbLots ) {
2023-01-28 11:35:33 +01:00
this . updateVente ( {
2024-06-01 00:17:39 +02:00
nbLots : this . venteData . isOwned ? Math . max ( 0 , Math . min ( nbLots , this . venteData . maxLots ) ) : nbLots
2023-01-28 11:35:33 +01:00
} )
2024-06-01 00:17:39 +02:00
this . html . find ( ".nbLots" ) . val ( this . venteData . nbLots ) ;
2021-05-07 17:27:02 +02:00
}
setQuantiteIllimite ( checked ) {
2023-01-28 11:35:33 +01:00
this . updateVente ( { quantiteIllimite : checked } )
2022-12-09 02:00:31 +01:00
this . html . find ( ".label-quantiteIllimite" ) . text ( this . venteData . quantiteIllimite ? "Illimités" : "disponibles" ) ;
2024-06-01 00:17:39 +02:00
HtmlUtility . showControlWhen ( this . html . find ( ".nbLots" ) , ! this . venteData . quantiteIllimite )
2021-05-07 17:27:02 +02:00
}
}