add-join-us.component.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { AuthServiceService } from './../../shared/auth-service.service';
  2. import { HttpClient } from '@angular/common/http';
  3. import { UserService } from './../../shared/user.service';
  4. import { ActivatedRoute, Params, Router } from '@angular/router';
  5. import { Component, OnInit, ViewChild, Input } from '@angular/core';
  6. import { ToastrService } from 'ngx-toastr';
  7. import { NgxSpinnerService } from 'ngx-spinner';
  8. import { FormGroup, FormControl, Validators } from '@angular/forms';
  9. import { Location } from '@angular/common';
  10. @Component({
  11. selector: 'app-add-join-us',
  12. templateUrl: './add-join-us.component.html',
  13. styleUrls: ['./add-join-us.component.css']
  14. })
  15. export class AddJoinUsComponent implements OnInit {
  16. constructor(private userSer: UserService,
  17. private spineer: NgxSpinnerService,
  18. private route: ActivatedRoute,
  19. public authSer: AuthServiceService,
  20. private location: Location,
  21. private toastr: ToastrService) { }
  22. adminstrations = [];
  23. files: any;
  24. typeLink: string = 'إنشاء جديد';
  25. urlImg: string = '../../assets/image/avatar.png';
  26. joinUsForm: FormGroup;
  27. checkMode: boolean = false;
  28. checkChangeImage: boolean = false;
  29. checkValidImg: boolean = false;
  30. photoEdit: boolean = true;
  31. checkSaveClick: boolean = false;
  32. imageBase64: string = '';
  33. photoType: string = '';
  34. joinId: number;
  35. higriDateVal: string;
  36. bindingDateSplit;
  37. currentDate: any = Date.now();
  38. ngOnInit() {
  39. //show / hide notification search in header
  40. this.authSer.notificationLogin = true;
  41. this.authSer.showSearchHeader = false;
  42. this.authSer.showHeaderLogin = false;
  43. this.authSer.showHeaderDashBoard = true;
  44. //show / hide notification search in header
  45. this.authSer.notificationLogin = true;
  46. this.authSer.showSearchHeader = false;
  47. this.authSer.showHeaderLogin = false;
  48. this.authSer.showHeaderDashBoard = true;
  49. this.authSer.showDashboardHeader = true;
  50. this.authSer.internalHeader = false;
  51. //get profile data
  52. this.userSer.getUserDataProfile();
  53. this.route.params.subscribe(
  54. (params: Params) => {
  55. this.joinId = params['editJoinId'];
  56. }
  57. );
  58. this.joinUsForm = new FormGroup({
  59. name: new FormControl(null, Validators.required),
  60. name_en: new FormControl(null, Validators.required),
  61. adminstration_id: new FormControl(null, Validators.required),
  62. status: new FormControl(null, Validators.required),
  63. });
  64. //get adminstration
  65. this.userSer.getAdministration().subscribe(
  66. (responce) => {
  67. console.log(responce);
  68. this.adminstrations = responce['adminstrations'];
  69. },
  70. (error) => {
  71. console.log(error)
  72. }
  73. );
  74. this.route.params.subscribe(
  75. (params:Params) => {
  76. if(params['typeJoinMode'] == 'edit') {
  77. this.spineer.show();
  78. this.checkMode = true;
  79. this.typeLink = 'تعديل';
  80. this.userSer.onGetUserData(this.joinId, 'editJo').subscribe(
  81. (responce) => {
  82. console.log(responce);
  83. this.joinUsForm.patchValue({
  84. name: responce['user'].name,
  85. name_en: responce['user'].name_en,
  86. //date_of_employment_hij: responce['user'].date_of_employment_hij,
  87. adminstration_id: responce['user'].adminstration_id,
  88. status: responce['user'].status,
  89. });
  90. this.urlImg = responce['user'].photo ? this.authSer.pathImg + responce['user'].photo : this.urlImg;
  91. const date = responce['user'].date_of_employment_hij.split('-');
  92. this.bindingDateSplit = {
  93. 'year': parseInt(date[0]),
  94. 'month': parseInt(date[1]),
  95. 'day': parseInt(date[2])
  96. };
  97. this.higriDateVal = responce['user'].date_of_employment_hij;
  98. console.log(this.bindingDateSplit);
  99. this.spineer.hide();
  100. }
  101. );
  102. }
  103. }
  104. );
  105. }
  106. onFileChanges(event) {
  107. console.log(event);
  108. this.imageBase64 = event[0].base64;
  109. this.photoType = event[0].type.split('/');
  110. console.log(this.photoType[1]);
  111. //console.log(this.imageBase64);
  112. this.checkChangeImage = true;
  113. this.checkValidImg = false;
  114. }
  115. getUrl(event) {
  116. if (event.target.files && event.target.files[0]) {
  117. var reader = new FileReader();
  118. reader.readAsDataURL(event.target.files[0]); // read file as data url
  119. reader.onload = (event) => { // called once readAsDataURL is completed
  120. this.urlImg = event.target['result'];
  121. }
  122. }
  123. }
  124. //get value date from child component
  125. public getDate(date: any):void {
  126. console.log( date);
  127. this.higriDateVal = date.year + '-' + date.month + '-' + date.day;
  128. console.log('higrii date', this.higriDateVal);
  129. }
  130. onSubmittedForm() {
  131. this.checkSaveClick = true;
  132. console.log(this.joinUsForm.value);
  133. const formData = this.joinUsForm.value;
  134. const dateHigriArray = this.higriDateVal.split('-');
  135. console.log('higriiii datttte', this.higriDateVal);
  136. formData['date_of_employment_hij'] = this.higriDateVal;
  137. console.log(dateHigriArray);
  138. if(this.checkChangeImage){
  139. formData['photo'] = this.imageBase64;
  140. formData['photo_type'] = this.photoType[1];
  141. } else {
  142. delete formData.photo;
  143. delete formData.photo_type;
  144. this.photoEdit = false;
  145. console.log(formData);
  146. }
  147. if(this.checkMode) {
  148. formData['id'] = this.joinId;
  149. console.log(formData);
  150. if(this.imageBase64 == '' && this.photoEdit == true) {
  151. this.toastr.warning('قم باختيار صوره !');
  152. } else {
  153. this.userSer.onEditUser(formData, 'joinUs').subscribe(
  154. (responce) => {
  155. this.checkSaveClick = true;
  156. console.log(responce);
  157. this.toastr.success('تم التعديل بنجاح');
  158. this.location.back();
  159. },
  160. (error) => {
  161. this.checkSaveClick = false;
  162. console.log(error);
  163. if(error.error['status'] == 'invalid date_of_employment_hij') {
  164. this.toastr.error('تاريخ التوظيف الهجري لا يجب ان يكون في الحاضر او اليوم');
  165. } else if(error.error['status'] == 'active status for only 4 records') {
  166. this.toastr.warning('لديك أربعه أشخاص في الحاله فعال بالفعل ، قم بعدم تفعيل أو مسح أحدهم ');
  167. } else {
  168. this.toastr.error('خطأ في السيرفير');
  169. }
  170. }
  171. );
  172. }
  173. } else {
  174. if(this.imageBase64 == '') {
  175. this.toastr.warning('قم باختيار صوره !');
  176. this.checkSaveClick = false;
  177. } else {
  178. this.userSer.addUser(formData, 'joinUs').subscribe(
  179. (responce) => {
  180. this.checkSaveClick = true;
  181. console.log(responce);
  182. this.toastr.success('تم الاضافه بنجاح');
  183. this.location.back();
  184. },
  185. (error) => {
  186. this.checkSaveClick = false;
  187. console.log(error.error['status']);
  188. if(error.error['status'] == 'invalid date_of_employment_hij') {
  189. this.toastr.error('تاريخ التوظيف الهجري لا يجب ان يكون في الحاضر او اليوم');
  190. }else if(error.error['status'] == 'active status for only 4 records') {
  191. this.toastr.warning('لديك أربعه أشخاص في الحاله فعال بالفعل ، قم بعدم تفعيل أو مسح أحدهم ');
  192. } else {
  193. this.toastr.error('خطأ في السيرفير');
  194. }
  195. }
  196. );
  197. }
  198. }
  199. }
  200. }