registration-trainee-movement-add.component.ts 13 KB

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