news-list.component.ts 10 KB

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