add-new-report.component.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { NgxSpinnerService } from 'ngx-spinner';
  2. import { DashboardService } from './../../../shared/dashboard.service';import { HttpClient } from '@angular/common/http';
  3. import { AuthServiceService } from './../../../shared/auth-service.service';
  4. import { ActivatedRoute, Params, Router } from '@angular/router';
  5. import { Component, OnInit, ViewChild } from '@angular/core';
  6. import { NgForm } from '@angular/forms';
  7. import { ToastrService } from 'ngx-toastr';
  8. import { Location, formatDate } from '@angular/common';
  9. import { LocationListComponent } from '../../definition-of-location/location-list/location-list.component';
  10. import { UserService } from 'src/app/shared/user.service';
  11. @Component({
  12. selector: 'app-add-new-report',
  13. templateUrl: './add-new-report.component.html',
  14. styleUrls: ['./add-new-report.component.css']
  15. })
  16. export class AddNewReportComponent implements OnInit {
  17. constructor(private route: ActivatedRoute,
  18. private http: HttpClient,
  19. private toastr: ToastrService,
  20. private dashBoardSer: DashboardService,
  21. private spiner: NgxSpinnerService,
  22. private location: Location,
  23. private authService: AuthServiceService,
  24. private userService: UserService,
  25. private router: Router
  26. ) { }
  27. locationList = [];
  28. subLocationList = [];
  29. checkSaveClick: boolean = false;
  30. bindingDateSplitStart;
  31. checked : boolean = false;
  32. pageId: number;
  33. uploaded: boolean = false;
  34. disabledVal: boolean = true;
  35. serviceId: number;
  36. userId: number;
  37. files = [{
  38. title : null,
  39. file: null,
  40. file_type: null,
  41. nameFile: null,
  42. }];
  43. dataForm = {
  44. id_number: '',
  45. name : '',
  46. phoneNum: '',
  47. created_date : "",
  48. created_time : "",
  49. subject : '',
  50. message : '',
  51. main_location_id : '',
  52. sub_location_id : '',
  53. files: []
  54. };
  55. currentDate: any = Date.now();
  56. //higri date array
  57. month = ['محرم', 'صفر', 'ربيع الأول', 'ربيع الآخر', 'جمادى الأولى', 'جمادى الآخرة', 'رجب', 'Shaʻban', 'رمضان', 'شوال',
  58. 'ذو القعدة', 'ذو الحجة'];
  59. ngOnInit() {
  60. //init the values of permision boolean
  61. this.authService.showAddBtn = false;
  62. this.authService.showDeleteBtn = false;
  63. this.authService.showEditBtn = false;
  64. //show / hide notification search in header
  65. this.authService.notificationLogin = true;
  66. this.authService.showSearchHeader = false;
  67. this.authService.showHeaderLogin = false;
  68. this.authService.showHeaderDashBoard = true;
  69. this.authService.showDashboardHeader = true;
  70. this.authService.internalHeader = false;
  71. this.route.params.subscribe(
  72. (parmas: Params) => {
  73. this.pageId = +parmas['reportId'];
  74. localStorage.setItem('pageIdActive', parmas['reportId']);
  75. }
  76. );
  77. this.route.parent.params.subscribe(
  78. (params:Params) => {
  79. this.serviceId = params['serviceID'];
  80. this.userId = params['userID'];
  81. }
  82. );
  83. this.currentDate = this.authService.writeHijri(new Date(this.currentDate), 'en', 'system');
  84. const nowHigriDate = this.currentDate.split(' ');
  85. console.log('nowHigriDate', nowHigriDate);
  86. console.log('current higri date split', this.currentDate.split(' '));
  87. for(let i = 0; i < this.month.length; i++) {
  88. if(this.currentDate.split(' ')[0] == this.month[i]) {
  89. this.currentDate.split(' ')[0] = i + 1;
  90. nowHigriDate[0] = i + 1;
  91. }
  92. }
  93. this.bindingDateSplitStart = {
  94. year:parseInt(nowHigriDate[2]),
  95. month: parseInt(nowHigriDate[0]),
  96. day: parseInt(nowHigriDate[1]),
  97. }
  98. console.log('hhhhhhhhhhhhhhhhhhhh', this.bindingDateSplitStart);
  99. var today = new Date();
  100. this.dataForm.created_time = ( today.getHours() > 10 ? today.getHours() : '0' + today.getHours() ) + ":" + ( today.getMinutes() > 10 ? today.getMinutes() : '0' + today.getMinutes() );
  101. console.log('created_timmmmme', this.dataForm.created_time);
  102. this.dataForm.created_date = nowHigriDate[2] + '-' + nowHigriDate[0] + '-' + nowHigriDate[1];
  103. console.log('dataForm object ', this.dataForm.created_date);
  104. this.http.get(this.authService.pathApi + '/parent_locations_list').subscribe(
  105. (response) => {
  106. this.locationList = response['locations'];
  107. console.log(response)
  108. },
  109. (error) => {
  110. console.log(error);
  111. }
  112. );
  113. this.http.get(this.authService.pathApi + '/profile').subscribe(
  114. (responce) => {
  115. this.dataForm.id_number = responce['user'].identity_number;
  116. this.dataForm.name = responce['user'].name;
  117. this.dataForm.phoneNum = responce['user'].phone
  118. console.log('dsf',responce)
  119. },
  120. (error) => {console.log(error)},
  121. );
  122. }
  123. onChangemainLocation(event){
  124. const id = event.target.value
  125. this.dataForm.main_location_id = id;
  126. this.http.get(this.authService.pathApi + '/childs_locations_list_by_parent_id/' + id).subscribe(
  127. (response) => {
  128. console.log(id);
  129. this.subLocationList = response['locations'];
  130. },
  131. (error) => {
  132. console.log(error)
  133. }
  134. );
  135. }
  136. changeSubLocation(event){
  137. console.log(event.target.value);
  138. this.dataForm.sub_location_id = event.target.value;
  139. }
  140. plusImage() {
  141. this.files.push({
  142. title: null,
  143. file: null,
  144. file_type: null,
  145. nameFile: null,
  146. });
  147. console.log('files after plus ', this.files);
  148. }
  149. //delete row from table
  150. onDeleteRow(index:number) {
  151. this.files.splice(index , 1);
  152. console.log('files after delete ',this.files);
  153. }
  154. //change file
  155. onFileChanges(event, index:number) {
  156. console.log(event);
  157. console.log(index);
  158. this.uploaded = true;
  159. this.files[index].nameFile = event[0].name.substring(0,20) + '....';
  160. this.files[index].file = event[0].base64;
  161. this.files[index].file_type = event[0].type.split('/')[1];
  162. console.log(this.files);
  163. console.log('files after change ',this.files);
  164. }
  165. onSubmitted(){
  166. for (let i = 0; i < this.files.length; i++) {
  167. if(this.files[i].file_type){
  168. this.files[i].file_type = this.files[i].file_type.toLowerCase();
  169. }
  170. if(this.files[i].file != null && this.files[i].title == null){
  171. this.toastr.warning('ادخل عنوان الملف رقم ' + (i+1) );
  172. this.checked = true;
  173. }else if(this.files[i].file == null && this.files[i].title != null){
  174. this.files.splice(i , 1);
  175. }else if(this.files[i].file == null && this.files[i].title == null){
  176. this.files.splice(i , 1);
  177. } else {
  178. delete this.files[i].nameFile;
  179. this.dataForm.files.push(this.files[i]);
  180. }
  181. }
  182. if(!this.checked){
  183. delete this.dataForm.id_number;
  184. delete this.dataForm.name;
  185. delete this.dataForm.phoneNum;
  186. if(this.dataForm.files.length == 0 ){
  187. delete this.dataForm.files;
  188. }
  189. console.log(this.dataForm);
  190. this.http.post(this.authService.pathApi + '/add_communication' , this.dataForm )
  191. .subscribe(
  192. res => {
  193. console.log(res);
  194. this.checked = false;
  195. this.toastr.success('تم حفظ البلاغ ');
  196. this.router.navigate(['/service/' + this.userId + '/' + this.serviceId + '/followCommunicationList/58']);
  197. },
  198. err => {
  199. console.log(err)
  200. this.toastr.error('حدث خطأ في الاتصال ');
  201. }
  202. );
  203. }else{
  204. console.log('i cant send the request');
  205. }
  206. }
  207. }