pmtpenza

Untitled

Jun 29th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormBuilder,
  3. FormGroup,
  4. Validators } from '@angular/forms';
  5. import {
  6. Api,
  7. ToastNotify
  8. } from '../../providers/providers';
  9. import { SelectModalComponent } from '../../modal/select-modal/select-modal.page';
  10. import {
  11. ModalController,
  12. AlertController
  13. } from '@ionic/angular';
  14. import * as moment from 'moment';
  15.  
  16. @Component({
  17. selector: 'app-zayvka',
  18. templateUrl: './zayvka.page.html',
  19. styleUrls: ['./zayvka.page.scss'],
  20. })
  21. export class ZayvkaPage {
  22. loginForm: FormGroup;
  23. baseTotal: string = 'Выберите услугу и время.';
  24. selectedServices: Array<any> = [];
  25. allCategories: Array<any> = [];
  26. total: any;
  27. fieldsName: Array<string> = ['uslugi_id', 'hours', 'date', 'payment', 'price_ikon'];
  28.  
  29. ulugii: Array<any> = [];
  30. constructor (
  31. public api: Api,
  32. public alertController: AlertController,
  33. public toastNotify: ToastNotify,
  34. public modalCtrl: ModalController,
  35. public formBuilder: FormBuilder,
  36. ) {
  37. let self = this;
  38. self.total = self.baseTotal;
  39. self.loginForm = formBuilder.group({
  40. uslugi_id: [],
  41. price:[],
  42. hours: ["0"],
  43. //date: [moment().format('DD/MM/YYYY HH:mm')],
  44. date: [moment().toISOString()],
  45. payment: ["0"]
  46. });
  47. }
  48. ionViewDidEnter(){
  49. let self = this;
  50. self.api.showMask()
  51. .then(() => {
  52. self.api.getCategories()
  53. .then((response: any) => {
  54. self.api.hideMask()
  55. .then(() => {
  56. self.allCategories = response;
  57. self.loginForm.controls.uslugi_id.setValue(self.ulugii[0]);
  58. });
  59. });
  60. });
  61. }
  62. async selectCategory() {
  63. let self = this,
  64. cssClass = 'bottom-modal select-category',
  65. modalView;
  66. modalView = await self.modalCtrl.create({
  67. component: SelectModalComponent,
  68. cssClass: cssClass,
  69. backdropDismiss: true,
  70. showBackdrop: true,
  71. componentProps: {
  72. title: 'Выберите категорию',
  73. items: self.allCategories
  74. }
  75. });
  76. await modalView.present();
  77. const { data } = await modalView.onDidDismiss();
  78. if (data) {
  79. self.getServicesByCategory(data.id);
  80. }
  81. }
  82. getServicesByCategory(id: number){
  83. let self = this;
  84. self.api.showMask()
  85. .then(() => {
  86. self.api.getServicesByCategory({
  87. id: id
  88. })
  89. .then((response: any) => {
  90. let services = response.map((item) => {
  91. return {
  92. name: item.usluginame,
  93. id: item.id,
  94. price: item.price
  95. };
  96. });
  97. self.api.hideMask()
  98. .then(() => {
  99. if(services.length) {
  100. self.selectService(services)
  101. } else {
  102. self.toastNotify.error('Выберите другую категорию. В этой категории нет услуг.');
  103. }
  104. });
  105. });
  106. });
  107. }
  108. async selectService(services: Array<any>) {
  109. let self = this,
  110. cssClass = 'bottom-modal select-category',
  111. modalView;
  112. modalView = await self.modalCtrl.create({
  113. component: SelectModalComponent,
  114. cssClass: cssClass,
  115. backdropDismiss: true,
  116. showBackdrop: true,
  117. componentProps: {
  118. title: 'Выберите услугу',
  119. items: services
  120. }
  121. });
  122. await modalView.present();
  123. const { data } = await modalView.onDidDismiss();
  124. if (data) {
  125. self.specifyCount(data);
  126. }
  127. }
  128. async specifyCount(service: any) {
  129. let self = this;
  130. const alert = await this.alertController.create({
  131. header: 'Укажите количество!',
  132. inputs: [{
  133. name: 'count',
  134. type: 'tel',
  135. }],
  136. buttons: [{
  137. text: 'Отмена',
  138. role: 'cancel'
  139. }, {
  140. text: 'Выбрать',
  141. handler: (data) => {
  142. if(data.count) {
  143. self.selectServiceDone(Object.assign(service, {count: Number(data.count)}))
  144. }
  145. }
  146. }]
  147. });
  148. await alert.present();
  149. }
  150. delete(index: number) {
  151. let self = this;
  152. self.selectedServices.splice(index, 1);
  153. if(self.selectedServices.length) {
  154. self.calculateTotal();
  155. } else {
  156. self.total = 0;
  157. }
  158. }
  159. selectServiceDone(service: any){
  160. let self = this,
  161. found: boolean = false;
  162. if(self.selectedServices.filter(item => item.id === service.id).length) {
  163. self.selectedServices.map((item) => {
  164. if(item.id === service.id) {
  165. item.count += service.count;
  166. }
  167. return item;
  168. });
  169. } else {
  170. self.selectedServices.push(service);
  171. }
  172. self.calculateTotal();
  173. }
  174. calculateTotal(){
  175. let self = this;
  176. self.total = self.selectedServices.map(item => item.price * item.count).reduce((sum, x) => sum + x);
  177. }
  178. send() {
  179. let self = this,
  180. error: string = '',
  181. values = self.loginForm.getRawValue();
  182. if(!error.length) {
  183. let params: any = {
  184. };
  185. params.service = {}
  186. self.selectedServices.forEach(item => params.service[item.id] = item.count);
  187. console.log('todo send data ', params);
  188. self.api.showMask()
  189. .then(() => {
  190. self.api.sendUslugi(params)
  191. .then((response: any) => {
  192. debugger
  193. self.api.hideMask()
  194. .then(() => {
  195. if(response.status) {
  196. /*self.loginForm.controls.uslugi_id.setValue(self.ulugii[0]);
  197. self.loginForm.controls.hours.setValue("0");
  198. self.loginForm.controls.date.setValue(moment().toISOString());
  199. self.loginForm.controls.payment.setValue("0");*/
  200. } else {
  201. //self.showError(response.errorMessage);//TODO выставить правильный параметр из ответа
  202. }
  203. });
  204. });
  205. });
  206. self.toastNotify.success('Ваша заявка отправлена.');
  207. self.clearData();
  208. }
  209. }
  210. clearData(){
  211. let self = this;
  212. self.selectedServices = [];
  213. self.total = 0;
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment