add-new-report.component.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. files = [{
  35. title : null,
  36. file: null,
  37. file_type: null,
  38. nameFile: null,
  39. }];
  40. dataForm = {
  41. id_number: '',
  42. name : '',
  43. phoneNum: '',
  44. created_date : "",
  45. created_time : "",
  46. subject : '',
  47. message : '',
  48. main_location_id : '',
  49. sub_location_id : '',
  50. files: []
  51. };
  52. currentDate: any = Date.now();
  53. //higri date array
  54. month = ['محرم', 'صفر', 'ربيع الأول', 'ربيع الآخر', 'جمادى الأولى', 'جمادى الآخرة', 'رجب', 'شعبان', 'رمضان', 'شوال',
  55. 'ذو القعدة', 'ذو الحجة'];
  56. ngOnInit() {
  57. //init the values of permision boolean
  58. this.authService.showAddBtn = false;
  59. this.authService.showDeleteBtn = false;
  60. this.authService.showEditBtn = false;
  61. //show / hide notification search in header
  62. this.authService.notificationLogin = true;
  63. this.authService.showSearchHeader = false;
  64. this.authService.showHeaderLogin = false;
  65. this.authService.showHeaderDashBoard = true;
  66. this.authService.showDashboardHeader = true;
  67. this.authService.internalHeader = false;
  68. this.route.params.subscribe(
  69. (parmas: Params) => {
  70. this.pageId = +parmas['reportId'];
  71. localStorage.setItem('pageIdActive', parmas['reportId']);
  72. }
  73. );
  74. this.currentDate = this.authService.writeHijri(new Date(this.currentDate), 'ar', 'system');
  75. console.log(this.currentDate.split(' '));
  76. const nowHigriDate = this.currentDate.split(' ');
  77. for(let i = 0; i < this.month.length; i++) {
  78. if(this.currentDate.split(' ')[1] == this.month[i]) {
  79. this.currentDate.split(' ')[1] = i + 1;
  80. nowHigriDate[1] = i + 1;
  81. }
  82. }
  83. this.bindingDateSplitStart = {
  84. year: parseInt(nowHigriDate[2]),
  85. month: parseInt(nowHigriDate[1]),
  86. day: parseInt(nowHigriDate[0]),
  87. }
  88. console.log('hhhhhhhhhhhhhhhhhhhh', this.bindingDateSplitStart);
  89. var today = new Date();
  90. this.dataForm.created_time = today.getHours() + ":" + today.getMinutes() ;
  91. this.dataForm.created_date = nowHigriDate[2] + '-' + nowHigriDate[1] + '-' + nowHigriDate[0];
  92. console.log('dataForm object ', this.dataForm.created_date);
  93. this.http.get(this.authService.pathApi + '/parent_locations_list').subscribe(
  94. (response) => {
  95. this.locationList = response['locations'];
  96. console.log(response)
  97. },
  98. (error) => {
  99. console.log(error);
  100. }
  101. );
  102. this.http.get(this.authService.pathApi + '/profile').subscribe(
  103. (responce) => {
  104. this.dataForm.id_number = responce['user'].identity_number;
  105. this.dataForm.name = responce['user'].name;
  106. this.dataForm.phoneNum = responce['user'].phone
  107. console.log('dsf',responce)
  108. },
  109. (error) => {console.log(error)},
  110. );
  111. }
  112. onChangemainLocation(event){
  113. const id = event.target.value
  114. this.dataForm.main_location_id = id;
  115. this.http.get(this.authService.pathApi + '/childs_locations_list_by_parent_id/' + id).subscribe(
  116. (response) => {
  117. console.log(id);
  118. this.subLocationList = response['locations'];
  119. },
  120. (error) => {
  121. console.log(error)
  122. }
  123. );
  124. }
  125. changeSubLocation(event){
  126. console.log(event.target.value);
  127. this.dataForm.sub_location_id = event.target.value;
  128. }
  129. plusImage() {
  130. this.files.push({
  131. title: null,
  132. file: null,
  133. file_type: null,
  134. nameFile: null,
  135. });
  136. console.log('files after plus ', this.files);
  137. }
  138. //delete row from table
  139. onDeleteRow(index:number) {
  140. this.files.splice(index , 1);
  141. console.log('files after delete ',this.files);
  142. }
  143. //change file
  144. onFileChanges(event, index:number) {
  145. console.log(event);
  146. console.log(index);
  147. this.uploaded = true;
  148. this.files[index].nameFile = event[0].name.substring(0,20) + '....';
  149. this.files[index].file = event[0].base64;
  150. this.files[index].file_type = event[0].type.split('/')[1];
  151. console.log(this.files);
  152. console.log('files after change ',this.files);
  153. }
  154. onSubmitted(){
  155. for (let i = 0; i < this.files.length; i++) {
  156. if(this.files[i].file_type){
  157. this.files[i].file_type = this.files[i].file_type.toLowerCase();
  158. }
  159. if(this.files[i].file != null && this.files[i].title == null){
  160. this.toastr.warning('ادخل عنوان الملف رقم ' + (i+1) );
  161. this.checked = true;
  162. }else if(this.files[i].file == null && this.files[i].title != null){
  163. this.files.splice(i , 1);
  164. }else if(this.files[i].file == null && this.files[i].title == null){
  165. this.files.splice(i , 1);
  166. } else {
  167. delete this.files[i].nameFile;
  168. this.dataForm.files.push(this.files[i]);
  169. }
  170. }
  171. if(!this.checked){
  172. delete this.dataForm.id_number;
  173. delete this.dataForm.name;
  174. delete this.dataForm.phoneNum;
  175. if(this.dataForm.files.length == 0 ){
  176. delete this.dataForm.files;
  177. }
  178. console.log(this.dataForm);
  179. this.http.post(this.authService.pathApi + '/add_communication' , this.dataForm )
  180. .subscribe(
  181. res => {
  182. console.log(res);
  183. this.checked = false;
  184. this.toastr.success('تم حفظ البلاغ ');
  185. },
  186. err => {
  187. console.log(err)
  188. this.toastr.error('حدث خطأ في الاتصال ')
  189. }
  190. );
  191. }else{
  192. console.log('i cant send the request');
  193. }
  194. }
  195. }