-
Notifications
You must be signed in to change notification settings - Fork 0
/
next-form.page.ts
170 lines (139 loc) · 5.32 KB
/
next-form.page.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Component, OnInit } from '@angular/core';
import { ModalController, AlertController, NavController} from '@ionic/angular';
import { FormGroup, FormControl } from '@angular/forms';
import { AddTravelService } from '../add-travel.service';
import 'rxjs/add/operator/map';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Storage } from '@ionic/storage';
import { MyurlsService } from '../myurls.service';
@Component({
selector: 'app-next-form',
templateUrl: './next-form.page.html',
styleUrls: ['./next-form.page.scss'],
})
export class NextFormPage implements OnInit {
public NbPlace: number;
public Prix: number;
public form: FormGroup;
public travelObj: any;
private participation: number;
error: boolean;
constructor(public modalCtrl: ModalController,
public alertCtrl: AlertController,
public navCtrl: NavController,
public ats: AddTravelService,
private http: HttpClient,
private storage: Storage,
private url: MyurlsService) {
}
ngOnInit() {
this.form = new FormGroup({
NbPlace: new FormControl(),
Prix: new FormControl()
});
}
goBack() {
this.navCtrl.back();
this.hideModal();
}
cancel() {
this.hideModal();
}
submit() {
if (!( (this.form.get('NbPlace').value === null) ||
(this.form.get('Prix').value === null))) {
this.ats.data.NbPlace = this.form.get('NbPlace').value;
this.ats.data.Prix = this.form.get('Prix').value;
this.travelObj = {
Depart : this.ats.data.Depart,
Destination : this.ats.data.Destination,
Rencontre: this.ats.data.Rencontre,
Depot: this.ats.data.Depot,
Date: this.ats.data.Date,
NbPlace: this.ats.data.NbPlace,
Prix: this.ats.data.Prix,
LoginConducteur: this.ats.data.LoginConducteur,
Heure: this.ats.data.Heure,
NbPlaceDispo: this.ats.data.NbPlace
};
this.confirmation();
this.hideModal();
} else {
this.error = true;
}
}
hideModal() {
this.modalCtrl.dismiss();
}
price() {
const headers: any = new HttpHeaders({ 'Content-Type': 'application/json' }),
options: any = {
key: 'price',
travelCouple: {
depart: this.ats.data.Depart,
destination: this.ats.data.Destination
}
};
this.http.post(this.url.retrieveUrl, JSON.stringify(options), headers)
.subscribe((data: any) => {
this.participation = Math.round((data[0].prix / this.form.get('NbPlace').value) / 50) * 50;
},
(error: any) => {
console.log(error);
});
}
async confirmation() {
const alert = await this.alertCtrl.create({
header: 'Confirmez-vous vouloir publier ce trajet ?',
message: 'RECAPITULATIF' + '<br>'
+ 'Depart : ' + this.ats.data.Depart + '<br>'
+ 'Destination : ' + this.ats.data.Destination + '<br>'
+ 'Date et Heure: ' + this.ats.data.Date + ' à ' + this.ats.data.Heure + '<br>'
+ 'places Disponibles: ' + this.ats.data.NbPlace + '<br>'
+ 'Participation : ' + this.ats.data.Prix + '<br>',
buttons: [
{
text: 'Ok',
handler: () => {
const headers: any = new HttpHeaders({ 'Content-Type': 'application/json' }),
options: any = {key: 'publish', travelObj: this.travelObj};
this.http.post(this.url.manageUrl, JSON.stringify(options), headers)
.subscribe(() => {
this.storage.get('travels').then( (array: any[]) => {
if (!(array === null)) {
array.push(this.travelObj);
} else {
array = [this.travelObj];
}
this.storage.set('travels', array).then( () => {
this.ok();
});
});
},
(error: any) => {
console.log(error);
});
}
},
{
text: 'Annuler',
handler: () => {}
}
]
});
await alert.present();
}
async ok() {
const alert = await this.alertCtrl.create({
header: 'OPERATION REUSSIE',
message: 'votre trajet à bel et bien été publié',
buttons: [
{
text: 'Ok',
handler: () => {}
},
]
});
await alert.present();
}
}