view-my-female-requests-add.component.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import { HttpClient } from '@angular/common/http';
  2. import { NgxSpinnerService } from 'ngx-spinner';
  3. import { ActivatedRoute, Params } from '@angular/router';
  4. import { Location } from '@angular/common';
  5. import { AuthServiceService } from './../../../shared/auth-service.service';
  6. import { ToastrService } from 'ngx-toastr';
  7. import { DashboardService } from './../../../shared/dashboard.service';
  8. import { Component, OnInit, ViewChild } from '@angular/core';
  9. import { NgForm } from '@angular/forms';
  10. @Component({
  11. selector: 'app-view-my-female-requests-add',
  12. templateUrl: './view-my-female-requests-add.component.html',
  13. styleUrls: ['./view-my-female-requests-add.component.css']
  14. })
  15. export class ViewMyFemaleRequestsAddComponent implements OnInit {
  16. editPageId: number;
  17. isEdit: boolean;
  18. typePage: string;
  19. checkSaveclick: boolean;
  20. disable: boolean;
  21. files = [];
  22. uploaded: boolean = false;
  23. pageId: number;
  24. currentPage: number;
  25. types = [];
  26. needFiles: boolean = false;
  27. needdate: boolean = false;
  28. require_attachments: any;
  29. require_date: any;
  30. whoIsComing: any;
  31. isAdmin = false;
  32. acceptCheak = false;
  33. refusedCheak = false;
  34. statusAccept : boolean;
  35. statusrefu: boolean;
  36. comfoe: string;
  37. toster: string;
  38. tookDes = false;
  39. employeeId: string;
  40. complex_list = [];
  41. units = []
  42. building_types= [];
  43. buildings = [];
  44. isBuilding: boolean = false;
  45. isComplex: boolean = false;
  46. isBuildingType: boolean = false;
  47. constructor(
  48. private dashBoardSer: DashboardService,
  49. private toastr: ToastrService,
  50. public authSer: AuthServiceService,
  51. private route: ActivatedRoute,
  52. private spinner: NgxSpinnerService,
  53. private location: Location,
  54. private http: HttpClient
  55. ) { }
  56. formData = {
  57. type_id:'',
  58. request_reason:'',
  59. leaving_date:'',
  60. leaving_time:'',
  61. return_date:null,
  62. return_time:null,
  63. files:[]
  64. }
  65. adminForm = {
  66. request_id :'',
  67. status:'',
  68. status_description:'',
  69. }
  70. userData = {
  71. name:'',
  72. identity_number:'',
  73. functional_number:'',
  74. phone:'',
  75. email:''
  76. }
  77. unitInformation = {
  78. housing_complex_id: '',
  79. building_type_id: '',
  80. building_id:'',
  81. unit_id:'',
  82. bedrooms_number:'',
  83. bathrooms_number:'',
  84. covenants: [],
  85. independent_kitchen: 'yes',
  86. sittingrooms_number:''
  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. this.http.get(this.authSer.pathApi + '/housing_complexes_list').subscribe(
  101. res => {
  102. this.complex_list = res[('housing_complexes')];
  103. }
  104. );
  105. this.route.params.subscribe(
  106. (params: Params) => {
  107. this.editPageId = +params['editPageId'];
  108. this.whoIsComing = params['type'];
  109. console.log(this.editPageId);
  110. }
  111. );
  112. console.log('current page',this.pageId)
  113. this.dashBoardSer.getRequestTypesList( 1 ,10000 , 'all', 'all', 'all').subscribe(
  114. res => {
  115. console.log('x',res);
  116. this.types = res['types'];
  117. if (this.editPageId) {
  118. this.spinner.show();
  119. this.isEdit = true;
  120. this.typePage = 'تعديل';
  121. if(this.isAdmin){
  122. this.typePage = 'عرض تفاصيل الطلب'
  123. }
  124. this.dashBoardSer.getItemData(this.editPageId, 'femalesRequest' ).subscribe(
  125. res => {
  126. const data = res['data'].female_request;
  127. const unitInfo = res['data'].unit_information
  128. console.log('ssss', res);
  129. this.formData.type_id = data.type_id;
  130. this.formData.request_reason = data.request_reason;
  131. if(this.isAdmin){
  132. this.adminForm.request_id = data.id;
  133. }
  134. this.adminForm.status = data.status;
  135. if(this.adminForm.status != 'new'){
  136. this.tookDes = true;
  137. if(this.adminForm.status == 'accepted' || this.adminForm.status == 'confirmed'){
  138. this.statusAccept = true;
  139. if(this.adminForm.status == 'accepted'){
  140. this.comfoe = 'مقبول'
  141. }else{
  142. this.comfoe = 'مؤكد'
  143. }
  144. }else if(this.adminForm.status == 'refused'){
  145. this.adminForm.status_description = data.status_description;
  146. this.statusAccept = false;
  147. this.statusrefu = true;
  148. this.refusedCheak = true;
  149. this.comfoe = 'مرفوض'
  150. }
  151. }else{
  152. this.comfoe = 'جديد'
  153. if(this.whoIsComing == 'AD74'){
  154. this.isAdmin = true;
  155. }
  156. }
  157. this.employeeId = data.employee_id;
  158. this.http.get(this.authSer.pathApi + '/get_user/' + this.employeeId).subscribe(
  159. res =>{
  160. console.log('=>',res);
  161. this.userData.name = res['user'].name;
  162. this.userData.identity_number = res['user'].identity_number;
  163. this.userData.functional_number = res['user'].functional_number;
  164. this.userData.phone = res['user'].phone;
  165. this.userData.email = res['user'].email;
  166. },
  167. err =>{
  168. console.log(err);
  169. }
  170. )
  171. this.onRequestType(this.formData.type_id);
  172. console.log('>>>>>' ,this.formData.type_id);
  173. if(this.needFiles){
  174. this.files = data.files;
  175. console.log('<><>' , this.files);
  176. }
  177. if(this.needdate){
  178. this.formData.return_date = data.return_date;
  179. this.formData.return_time = data.return_time;
  180. this.formData.leaving_date = data.leaving_date;
  181. this.formData.leaving_time = data.leaving_time;
  182. }
  183. this.unitInformation.housing_complex_id = unitInfo.housing_complex_id;
  184. console.log(this.unitInformation.housing_complex_id );
  185. this.getBuildingType(this.unitInformation.housing_complex_id);
  186. this.unitInformation.building_type_id = unitInfo.building_type_id;
  187. this.getBuilding(this.unitInformation.building_type_id);
  188. this.unitInformation.building_id = unitInfo.building_id;
  189. this.unitInformation.unit_id = unitInfo.unit_id;
  190. },
  191. (error) => {
  192. console.log(error);
  193. }
  194. );
  195. }else {
  196. this.typePage = 'اضافة';
  197. }
  198. this.spinner.hide();
  199. },
  200. err => {
  201. console.log(err);
  202. }
  203. )
  204. }
  205. plusImage() {
  206. this.files.push({
  207. title: null,
  208. file: null,
  209. file_type: null,
  210. nameFile: null,
  211. });
  212. console.log('files after plus ', this.files);
  213. }
  214. onDeleteRow(index:number) {
  215. this.files.splice(index , 1);
  216. console.log('files after delete ',this.files);
  217. }
  218. //change file
  219. onFileChanges(event, index:number) {
  220. console.log(event);
  221. console.log(index);
  222. this.uploaded = true;
  223. this.files[index].nameFile = event[0].name.substring(0,20) + '....';
  224. this.files[index].file = event[0].base64;
  225. this.files[index].file_type = event[0].type.split('/')[1];
  226. console.log(this.files);
  227. console.log('files after change ',this.files);
  228. }
  229. accept(){
  230. this.adminForm.status = 'accepted'
  231. this.acceptCheak = true;
  232. this.refusedCheak = false;
  233. this.adminForm.status_description = ''
  234. this.toster = 'تم قبول الطلب'
  235. }
  236. refuse(){
  237. this.adminForm.status = 'refused'
  238. this.acceptCheak = false;
  239. this.refusedCheak = true;
  240. this.toster = 'تم رفض الطلب'
  241. }
  242. // makeItValid(){
  243. // this.disable = false;
  244. // }
  245. onRequestType(event){
  246. const id = event;
  247. this.needFiles = false;
  248. this.needdate = false;
  249. this.disable = false;
  250. for (let i = 0 , n = this.types.length; i < n; i++) {
  251. if(this.types[i].id == id){
  252. console.log('here' + id);
  253. const c = this.types[i];
  254. this.require_attachments = c.require_attachments;
  255. this.require_date = c.require_date;
  256. if( c.require_attachments == '1'){
  257. this.needFiles = true;
  258. }else{
  259. this.disable = false
  260. }
  261. if( c.require_date == '1'){
  262. this.needdate = true;
  263. }else{
  264. this.disable = false;
  265. }
  266. }
  267. }
  268. }
  269. getBuildingType(event){
  270. console.log('sdsdsd',event);
  271. const id = event;
  272. console.log(id);
  273. this.http.get(this.authSer.pathApi+ '/building_types_list/' + id ).subscribe(
  274. res => {
  275. console.log(res);
  276. this.building_types = res[('building_types')];
  277. if(this.building_types.length > 0)
  278. this.isComplex = true;
  279. else {
  280. this.isComplex = false;
  281. this.isBuildingType = false;
  282. this.isBuilding = false;
  283. }
  284. },
  285. err => {
  286. console.log(err);
  287. }
  288. );
  289. }
  290. getBuilding(event){
  291. this.http.get(this.authSer.pathApi + '/buildings_list_by_type_id/' + event ).subscribe(
  292. res => {
  293. console.log('res',res);
  294. this.buildings = res['buildings'];
  295. if(this.buildings.length > 0){
  296. this.isBuildingType = true;
  297. console.log(this.buildings);
  298. this.getUnit(this.unitInformation.building_id);
  299. }else{
  300. this.isBuildingType = false;
  301. this.isBuilding = false;
  302. }
  303. },
  304. err => {
  305. console.log(err);
  306. }
  307. );
  308. }
  309. getUnit(event){
  310. this.http.get(this.authSer.pathApi + '/units_list_by_building_id/' + event ).subscribe(
  311. res => {
  312. console.log(res);
  313. this.units = res['units'];
  314. if(this.units.length > 0){
  315. this.isBuilding = true;
  316. console.log(this.units);
  317. }else{
  318. this.isBuilding = false;
  319. }
  320. },
  321. err => {
  322. console.log(err);
  323. }
  324. );
  325. }
  326. onSubmitted() {
  327. console.log('HERE',this.formData);
  328. this.disable = true;
  329. if(this.isAdmin){
  330. console.log(this.adminForm);
  331. this.dashBoardSer.addItem( this.adminForm , 'addFemaleRequestAdmin').subscribe(
  332. (responce) => {
  333. console.log('ADD',responce);
  334. this.toastr.success(this.toster);
  335. this.checkSaveclick = false;
  336. this.location.back();
  337. },
  338. (error) => {
  339. console.log(error);
  340. this.checkSaveclick = false;
  341. this.disable = false;
  342. this.toastr.error('خطأ في الخادم ، حاول لاحقا');
  343. }
  344. );
  345. }else{
  346. if(this.files.length > 0){
  347. this.formData.files = this.files
  348. }
  349. if(this.require_attachments == '1'){
  350. if(this.formData.files.length <= 0){
  351. this.toastr.warning('يجب اضافة مرفق')
  352. this.disable = false;
  353. return 0;
  354. }
  355. }
  356. if(this.require_date == '1'){
  357. if(this.formData.return_date == null || this.formData.return_time == null){
  358. this.toastr.warning('يجب اضافة وقت و تاريخ العوده')
  359. this.disable = false;
  360. return 0;
  361. }
  362. if(this.formData.leaving_date >= this.formData.return_date){
  363. this.toastr.warning('تاريخ العوده يجب ان يكون بعد تاريخ المغادرة');
  364. this.disable = false;
  365. return 0;
  366. }
  367. }
  368. if(this.editPageId) {
  369. this.dashBoardSer.editItem(this.editPageId, this.formData, 'editFemaleRequest').subscribe(
  370. (responce) => {
  371. this.disable = true;
  372. console.log(responce);
  373. this.toastr.success('تم التعديل بنجاح');
  374. this.location.back();
  375. },
  376. (error) => {
  377. this.disable = false;
  378. console.log(error);
  379. this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
  380. }
  381. )
  382. } else {
  383. this.dashBoardSer.addItem( this.formData , 'addFemaleRequest').subscribe(
  384. (responce) => {
  385. console.log('ADD',responce);
  386. this.toastr.success('تم الإضافه بنجاح');
  387. this.checkSaveclick = false;
  388. this.location.back();
  389. },
  390. (error) => {
  391. console.log(error);
  392. this.checkSaveclick = false;
  393. this.disable = false;
  394. this.toastr.error('خطأ في الخادم ، حاول لاحقا');
  395. }
  396. );
  397. }
  398. }
  399. }
  400. }