model-vehicle-list.component.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { DashboardService } from './../../../shared/dashboard.service';
  2. import { Modal } from 'ngx-modialog/plugins/bootstrap';
  3. import { AuthServiceService } from './../../../shared/auth-service.service';
  4. import { UserService } from './../../../shared/user.service';
  5. import { HttpClient } from '@angular/common/http';
  6. import { ActivatedRoute, Router, Params } from '@angular/router';
  7. import { Component, OnInit } from '@angular/core';
  8. import { NgxSpinnerService } from 'ngx-spinner';
  9. import { ToastrService } from 'ngx-toastr';
  10. @Component({
  11. selector: 'app-model-vehicle-list',
  12. templateUrl: './model-vehicle-list.component.html',
  13. styleUrls: ['./model-vehicle-list.component.css']
  14. })
  15. export class ModelVehicleListComponent implements OnInit {
  16. pageId: number;
  17. dataList = [];
  18. dataListIds = [];
  19. count: number;
  20. perPagePagenation: number;
  21. currentPage:number = 1;
  22. filtterStatus = 'all';
  23. selectedAll: any;
  24. userLoginId:number;
  25. serviceId:number;
  26. dataTableNumber: number = 5;
  27. pages = [];
  28. vehicle_types:any[] = [];
  29. mergedData = []
  30. constructor(private route: ActivatedRoute,
  31. private userSer: UserService,
  32. private spinner: NgxSpinnerService,
  33. public authSer: AuthServiceService,
  34. private toastr: ToastrService,
  35. private http: HttpClient,
  36. private modal: Modal,
  37. private dashBoardSer: DashboardService,
  38. private router: Router) { }
  39. ngOnInit() {
  40. this.spinner.show();
  41. //init the values of permision boolean
  42. this.authSer.showAddBtn = false;
  43. this.authSer.showDeleteBtn = false;
  44. this.authSer.showEditBtn = false;
  45. //show / hide notification search in header
  46. this.authSer.notificationLogin = true;
  47. this.authSer.showSearchHeader = false;
  48. this.authSer.showHeaderLogin = false;
  49. this.authSer.showHeaderDashBoard = true;
  50. this.authSer.showDashboardHeader = true;
  51. this.authSer.internalHeader = false;
  52. this.route.params.subscribe(
  53. (params: Params) => {
  54. this.pageId = +params['listPageId'];
  55. localStorage.setItem('pageIdActive', params['listPageId']);
  56. }
  57. );
  58. this.http.get(this.authSer.pathApi + '/get_vehicle_types_list').subscribe(
  59. (response) => {
  60. console.log(response);
  61. this.vehicle_types = response['types'];
  62. },
  63. (error) => {
  64. console.log(error);
  65. }
  66. );
  67. this.route.parent.params.subscribe(
  68. (params:Params) => {
  69. this.userLoginId = params['userID'];
  70. this.serviceId = params['serviceID'];
  71. this.userSer.getPagesPermetiotns(this.userLoginId, this.serviceId).subscribe(
  72. (responce) => {
  73. console.log('permission list', responce);
  74. this.pages = responce['pages'];
  75. for(let i = 0; i< this.pages.length; i++) {
  76. if(this.pages[i].id == 44) {
  77. if(this.pages[i].permissions[0].name == 'registration_of_vehicle_models') {
  78. this.authSer.showAddBtn = true;
  79. this.authSer.showEditBtn = true;
  80. this.authSer.showDeleteBtn = true;
  81. }
  82. }else {
  83. console.log('no lectures');
  84. }
  85. }
  86. this.spinner.hide();
  87. },
  88. (error) => {console.log(error)}
  89. );
  90. }
  91. );
  92. this.dashBoardSer.getListData(this.pageId, this.currentPage , this.dataTableNumber).subscribe(
  93. (response) => {
  94. console.log(response);
  95. this.dataList = response['vehicle_models'];
  96. this.count = response['count'];
  97. this.perPagePagenation = response['per_page'];
  98. console.log('xxx', this.dataList)
  99. this.mergeToGether()
  100. },
  101. (error) => {
  102. console.log(error);
  103. }
  104. )
  105. }
  106. mergeToGether() {
  107. for (let i = 0, n = this.dataList.length ; i < n ; i++) {
  108. for(let ii = 0, m = this.vehicle_types.length; ii < m ; ii++ ){
  109. if (this.dataList[i].vehicle_type_id == this.vehicle_types[ii].id) {
  110. this.mergedData.push({
  111. id: this.dataList[i].id,
  112. name: this.dataList[i].name,
  113. status: this.dataList[i].status,
  114. vehicle_type_id: this.dataList[i].vehicle_type_id,
  115. vehicle_type_name: this.vehicle_types[ii].name
  116. })
  117. console.log('done');
  118. }
  119. }
  120. }
  121. console.log(this.mergedData);
  122. }
  123. //make all checkbox of user checked
  124. selectAll() {
  125. for (var i = 0; i < this.dataList.length; i++) {
  126. this.dataList[i].selected = this.selectedAll;
  127. }
  128. };
  129. checkIfAllSelected() {
  130. this.selectedAll = this.dataList.every(function(item:any) {
  131. return item.selected == true;
  132. });
  133. };
  134. //filtter function
  135. filtterFunc(data) {
  136. this.dataList = [];
  137. console.log(data.target.value);
  138. const dataSearch = data.target.value;
  139. this.currentPage = 1;
  140. console.log('search curent page', this.currentPage);
  141. this.dashBoardSer.getDataUSerSearchBar(dataSearch, this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  142. (responce) => {
  143. console.log(responce);
  144. this.dataList = responce['vehicle_models'];
  145. this.count = responce['count'];
  146. this.perPagePagenation = responce['per_page'];
  147. console.log('filtter count', this.count);
  148. console.log('filtter perPagePAgenation', this.perPagePagenation);
  149. },
  150. (error) => {
  151. console.log(error)
  152. }
  153. );
  154. };
  155. //change page
  156. onPageChange(pagenationNumber) {
  157. this.spinner.show();
  158. this.currentPage = pagenationNumber;
  159. this.dataList = [];
  160. //console.log(pagenationNumber);
  161. //console.log(this.pageId);
  162. this.dashBoardSer.getListData(this.pageId, pagenationNumber, this.dataTableNumber).subscribe(
  163. (responce) => {
  164. console.log(responce);
  165. this.dataList = responce['vehicle_models'];
  166. this.count = responce['count'];
  167. this.perPagePagenation = responce['per_page'];
  168. console.log(this.dataList);
  169. this.spinner.hide();
  170. },
  171. (error) => {
  172. console.log(error);
  173. this.spinner.hide();
  174. }
  175. );
  176. };
  177. //determine the list count from select element
  178. onGetValue(event) {
  179. this.spinner.show();
  180. this.dataList = [];
  181. this.dataTableNumber = event.target.value;
  182. this.dashBoardSer.getListData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  183. (responce) => {
  184. console.log(responce);
  185. this.dataList = responce['vehicle_models'];
  186. this.count = responce['count'];
  187. this.perPagePagenation = responce['per_page'];
  188. this.spinner.hide();
  189. },
  190. (error) => {
  191. console.log(error);
  192. this.spinner.hide();
  193. }
  194. );
  195. };
  196. onGetValueFiltter(event) {
  197. this.spinner.show();
  198. this.dataList = [];
  199. const typeId = event.target.value;
  200. console.log(typeId);
  201. this.http.get(this.authSer.pathApi +'/page_list/' + this.pageId + '/' + this.currentPage + '/' + this.dataTableNumber + '/' + typeId).subscribe(
  202. (responce) => {
  203. console.log(responce);
  204. this.dataList = responce['vehicle_models'];
  205. this.count = responce['count'];
  206. this.perPagePagenation = responce['per_page'];
  207. this.spinner.hide();
  208. console.log('UrL>>', this.authSer.pathApi +'/page_list/' + this.pageId + '/' + this.currentPage + '/' + this.dataTableNumber + '/' + typeId)
  209. },
  210. (error) => {
  211. console.log(error);
  212. }
  213. );
  214. }
  215. onDelete() {
  216. this.dataListIds = [];
  217. for(let i = 0; i < this.dataList.length; i++) {
  218. if(this.dataList[i].selected == true) {
  219. this.dataListIds.push(this.dataList[i].id);
  220. }
  221. }
  222. console.log(this.dataListIds);
  223. if(this.dataListIds.length > 0) {
  224. const dialogRef = this.modal.alert()
  225. .size('sm')
  226. .showClose(true)
  227. .title('تأكيد الحذف')
  228. .body(`
  229. <h4>هل ترغب في حذف العناصر المحدده ؟ </h4>
  230. `)
  231. .open();
  232. dialogRef.result
  233. .then( result =>
  234. this.dashBoardSer.deleteItem(this.dataListIds , this.pageId).subscribe(
  235. (responce) => {
  236. console.log(responce);
  237. this.toastr.success('تم الحذف');
  238. this.spinner.show();
  239. this.dataList = [];
  240. //get list data
  241. this.dashBoardSer.getListData(this.pageId, this.currentPage ,this.dataTableNumber).subscribe(
  242. (responce) => {
  243. console.log(responce);
  244. this.dataList = responce['vehicle_models'];
  245. this.count = responce['count'];
  246. this.perPagePagenation = responce['per_page'];
  247. this.spinner.hide();
  248. },
  249. (error) => {
  250. console.log(error);
  251. this.spinner.hide();
  252. }
  253. );
  254. },
  255. (error) => {
  256. // console.log(error);
  257. this.spinner.hide();
  258. if(error.error.status == 'vehicle type id is already taken'){
  259. this.toastr.warning(' لا يمكن مسح هذا النوع')
  260. }
  261. },
  262. )
  263. );
  264. } else {
  265. this.toastr.warning('لم يتم إختيار أي عنصر للمسح !');
  266. }
  267. };
  268. //add function
  269. onAdd() {
  270. console.log('service/' + this.userLoginId + '/' + this.serviceId + '/addTab');
  271. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/vehicleModel/add']);
  272. }
  273. //edit function
  274. onEdit(editTabID) {
  275. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'vehicleModel/edit/' + editTabID]);
  276. };
  277. }