final-accerditation-add.component.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import { NgxSpinnerService } from 'ngx-spinner';
  2. import { ActivatedRoute, Params, Router } from '@angular/router';
  3. import { DashboardService } from './../../../shared/dashboard.service';
  4. import { HttpClient } from '@angular/common/http';
  5. import { UserService } from './../../../shared/user.service';
  6. import { FormGroup, Validators, FormControl, NgForm } from '@angular/forms';
  7. import { Component, OnInit, ViewChild } from '@angular/core';
  8. import { AuthServiceService } from '../../../shared/auth-service.service';
  9. import { ToastrService } from 'ngx-toastr';
  10. import { Location, formatDate } from '@angular/common';
  11. @Component({
  12. selector: 'app-final-accerditation-add',
  13. templateUrl: './final-accerditation-add.component.html',
  14. styleUrls: ['./final-accerditation-add.component.css']
  15. })
  16. export class FinalAccerditationAddComponent implements OnInit {
  17. constructor(private userSer: UserService,
  18. private http: HttpClient,
  19. private toastr: ToastrService,
  20. private route: ActivatedRoute,
  21. private dashBoardSer: DashboardService,
  22. private router: Router,
  23. private spinner: NgxSpinnerService,
  24. private location: Location,
  25. public authSer: AuthServiceService) { }
  26. typeLink:string = '';
  27. userId: number;
  28. checkSaveClick:boolean = false; //to make save button is disabled when click save
  29. disabledInput: boolean = false; //to make button dsabled in create mode
  30. reportId: number;
  31. stoppedShow: boolean = false;
  32. endedShow: boolean = false;
  33. userLoginId: number;
  34. serviceId: number;
  35. countries: any[] = [];
  36. identities: any[] = [];
  37. trainees:any[] = [];
  38. departments:any[] = [];
  39. departmentsId:any[] = [];
  40. attachments_ids: any[] =[];
  41. @ViewChild('f') traineeForm: NgForm;
  42. //all form data
  43. formData = {
  44. trainee: '',
  45. type: '',
  46. specialization_name: '',
  47. specific_specialization: '',
  48. university: '',
  49. level: '',
  50. training_period: '',
  51. registration_number: '',
  52. authority_card_number: '',
  53. functional_number: '',
  54. contract_type: '',
  55. status: '',
  56. organization_affiliated_with: '',
  57. department: '',
  58. start_date: '',
  59. end_date: ''
  60. }
  61. //user data object
  62. userData = {
  63. name: '',
  64. email: '',
  65. nationality_id: '',
  66. identity_number: '',
  67. identity_type_id: '',
  68. birthday: '',
  69. gender: '',
  70. phone: '',
  71. }
  72. actionForm = {
  73. status: '',
  74. adminstration_notes: '',
  75. card_received: 0,
  76. id: null,
  77. }
  78. identity_id: number = 0; //to determine te number of identity to validation
  79. identity_type: number;
  80. typeMode:boolean = false;
  81. files = [{
  82. title : '',
  83. file: '',
  84. file_type: '',
  85. nameFile: '',
  86. id: null,
  87. }];
  88. ngOnInit() {
  89. //init the values of permision boolean
  90. this.authSer.showAddBtn = false;
  91. this.authSer.showDeleteBtn = false;
  92. this.authSer.showEditBtn = false;
  93. //show / hide notification search in header
  94. this.authSer.notificationLogin = true;
  95. this.authSer.showSearchHeader = false;
  96. this.authSer.showHeaderLogin = false;
  97. this.authSer.showHeaderDashBoard = true;
  98. this.authSer.showDashboardHeader = true;
  99. this.authSer.internalHeader = false;
  100. //catch params id from activated route
  101. this.route.params.subscribe(
  102. (params: Params) => {
  103. this.reportId = +params['finalAccerditationEdit'];
  104. }
  105. );
  106. //catch parent params id from activated route
  107. this.route.parent.params.subscribe(
  108. (params:Params) => {
  109. this.userLoginId = +params['userID'];
  110. this.serviceId = +params['serviceID'];
  111. }
  112. );
  113. //get nationality data
  114. this.userSer.getNationality().subscribe(
  115. (responce) => {
  116. console.log(responce);
  117. this.countries = responce['countries'];
  118. },
  119. (error) => {
  120. console.log(error);
  121. }
  122. );
  123. //get identites
  124. this.userSer.onGetIdentities().subscribe(
  125. (responce) => {
  126. this.identities = responce['identities'];
  127. console.log('idddentiiiesssssssssss', this.identities);
  128. },
  129. (error) => {
  130. console.log(error);
  131. }
  132. );
  133. //get trainee data
  134. this.http.get(this.authSer.pathApi + '/get_trainees').subscribe(
  135. (responce) => {
  136. this.trainees = responce['users'];
  137. console.log(this.trainees);
  138. },
  139. (error) => {
  140. console.log(error);
  141. }
  142. );
  143. //get departments
  144. this.http.get(this.authSer.pathApi + '/training_adminstration_departments').subscribe(
  145. (responce) => {
  146. this.departments = responce['departments'];
  147. console.log(this.departments);
  148. },
  149. (error) => {
  150. console.log(error);
  151. }
  152. );
  153. if(this.reportId) {
  154. this.actionForm.id = this.reportId;
  155. this.disabledInput = true;
  156. this.spinner.show();
  157. this.typeMode = true;
  158. this.files = [];
  159. this.dashBoardSer.getItemData(this.reportId,'traineeRequest').subscribe(
  160. (responce) => {
  161. console.log('get request by id', responce);
  162. this.formData.trainee = responce['data']['user'].id;
  163. this.formData.type = responce['data']['request'].type;
  164. this.formData.status = responce['data']['request'].status;
  165. this.formData.specialization_name = responce['data']['request'].specialization_name;
  166. this.formData.specific_specialization = responce['data']['request'].specific_specialization;
  167. this.formData.level = responce['data']['request'].level;
  168. this.formData.contract_type = responce['data']['request'].contract_type;
  169. this.formData.university = responce['data']['request'].university;
  170. this.formData.authority_card_number = responce['data']['request'].authority_card_number;
  171. this.formData.training_period = responce['data']['request'].training_period;
  172. this.formData.functional_number = responce['data']['request'].functional_number;
  173. this.formData.organization_affiliated_with = responce['data']['request'].organization_affiliated_with;
  174. this.formData.registration_number = responce['data']['request'].registration_number;
  175. this.formData.start_date = responce['data']['request'].start_date;
  176. this.formData.end_date = responce['data']['request'].end_date;
  177. this.userData.name = responce['data']['user'].name;
  178. this.userData.email = responce['data']['user'].email;
  179. this.userData.identity_number = responce['data']['user'].identity_number;
  180. this.userData.identity_type_id = responce['data']['user'].identity_type_id;
  181. this.userData.phone = responce['data']['user'].phone;
  182. this.userData.birthday = responce['data']['user'].birthday;
  183. this.userData.gender = responce['data']['user'].gender;
  184. this.userData.nationality_id = responce['data']['user'].nationality_id;
  185. this.userId = responce['data']['user'].id;
  186. this.userData['id'] = this.userId;
  187. for(let i = 0; i < responce['data']['request'].attachments.length; i++) {
  188. this.attachments_ids.push(responce['data']['request'].attachments[i].id);
  189. }
  190. console.log('attachemnts ids ', this.attachments_ids);
  191. for(let i = 0; i < responce['data']['request'].departments.length; i++) {
  192. if(responce['data']['request'].departments[i].is_confirmed == 1) {
  193. this.formData.department = responce['data']['request'].departments[i].department_id;
  194. }
  195. }
  196. if(responce['data']['request'].attachments.length > 0) {
  197. for(let i = 0; i < responce['data']['request'].attachments.length; i++) {
  198. this.files.push({
  199. title: responce['data']['request'].attachments[i].title,
  200. file: '',
  201. nameFile: responce['data']['request'].attachments[i].file,
  202. file_type: '',
  203. id: responce['data']['request'].attachments[i].id,
  204. })
  205. }
  206. } else {
  207. console.log('no attachements user add !');
  208. }
  209. this.spinner.hide();
  210. },
  211. (error) => {
  212. console.log(error);
  213. }
  214. );
  215. }
  216. }
  217. recieveCard() {
  218. this.toastr.success('تم نسجيل الإستلام');
  219. this.actionForm.card_received = 1
  220. }
  221. printCard() {
  222. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/finalAccerditationReprot']);
  223. }
  224. //delete row of files
  225. onDeleteRow(rowIndex: number) {
  226. this.files.splice(rowIndex , 1);
  227. if(this.attachments_ids.length > 0) {
  228. this.attachments_ids.splice(rowIndex, 1);
  229. }
  230. console.log('files after delete ',this.files);
  231. console.log('attachements ids', this.attachments_ids);
  232. }
  233. //add attachements
  234. plusImage() {
  235. this.files.push({
  236. title: '',
  237. file: '',
  238. file_type: '',
  239. nameFile: '',
  240. id: null,
  241. });
  242. console.log('files after plus ', this.files);
  243. }
  244. //finalAccerditationReprot
  245. //on file change
  246. //change file
  247. onFileChanges(event, index:number) {
  248. console.log(event);
  249. console.log(index);
  250. this.files[index].nameFile = event[0].name.substring(0,20) + '....';
  251. this.files[index].file = event[0].base64;
  252. this.files[index].file_type = event[0].type.split('/')[1];
  253. //validate in pdf type file in edit mode
  254. if(this.files[index].file_type != '') {
  255. if(this.files[index].file_type != 'pdf') {
  256. this.toastr.warning('يجب أن تكون صيغه الملف pdf !');
  257. this.checkSaveClick = true;
  258. } else {
  259. this.checkSaveClick = false;
  260. }
  261. }
  262. console.log(this.files);
  263. }
  264. changeDepartment(event) {
  265. console.log(event.target.value);
  266. this.formData.department = event.target.value;
  267. }
  268. changeType(event) {
  269. console.log(event.target.value);
  270. this.formData.type = event.target.value;
  271. }
  272. getSelectedOptionText(event) {
  273. this.userData.identity_type_id = event.target.value;
  274. }
  275. onIdentitiyChange(event) {
  276. console.log(event);
  277. this.userData.identity_number = event;
  278. }
  279. //submitted form
  280. onSubmittedForm() {
  281. console.log(this.actionForm);
  282. //get the attchiements edit
  283. const editAttachements = [];
  284. for(let i = 0; i < this.files.length; i++) {
  285. for(let j = 0; j < this.attachments_ids.length; j++) {
  286. if(this.files[i].id == this.attachments_ids[j]) {
  287. editAttachements[i] = this.files[i];
  288. }
  289. }
  290. };
  291. //delete file & file type if not change it in old file
  292. for(let i = 0; i < editAttachements.length; i++) {
  293. if(editAttachements[i].file_type == '') {
  294. delete editAttachements[i].file;
  295. delete editAttachements[i].file_type;
  296. }
  297. };
  298. //delete the extera key file name
  299. for(let i = 0; i < editAttachements.length; i++) {
  300. delete editAttachements[i].nameFile;
  301. };
  302. //remove old file from files object
  303. for(let i = 0; i < editAttachements.length; i++) {
  304. for(let j = 0; j < this.files.length; j++) {
  305. if(editAttachements[i].id == this.files[j].id) {
  306. this.files.splice(j , 1);
  307. }
  308. }
  309. };
  310. //remove nameFile and id from object
  311. for(let i = 0; i < this.files.length; i++) {
  312. delete this.files[i].nameFile;
  313. delete this.files[i].id;
  314. };
  315. console.log('files after update ', this.files);
  316. console.log('edit files after update ', editAttachements);
  317. this.actionForm['attachments_ids'] = this.attachments_ids;
  318. this.actionForm['editable_attachments'] = editAttachements;
  319. this.actionForm['attachments'] = this.files;
  320. this.checkSaveClick = true;
  321. this.http.post(this.authSer.pathApi + '/final_accreditation_of_trainees', this.actionForm).subscribe(
  322. (responce) => {
  323. console.log(responce),
  324. this.toastr.success('تم بنجاح');
  325. this.checkSaveClick = false;
  326. this.location.back();
  327. },
  328. (error) => {
  329. console.log(error);
  330. this.checkSaveClick = false;
  331. this.toastr.error('يوجد خطأ ، حاول لاحقاَ');
  332. this.location.back();
  333. }
  334. );
  335. }
  336. }