users.component.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import { Modal } from 'ngx-modialog/plugins/bootstrap';
  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, ViewChild } from '@angular/core';
  7. import { ToastrService } from 'ngx-toastr';
  8. import { NgxSpinnerService } from 'ngx-spinner';
  9. @Component({
  10. selector: 'app-users',
  11. templateUrl: './users.component.html',
  12. styleUrls: ['./users.component.css']
  13. })
  14. export class UsersComponent implements OnInit {
  15. constructor( private router: Router,
  16. private route: ActivatedRoute,
  17. private userSer: UserService,
  18. private toastr: ToastrService,
  19. private modal: Modal,
  20. private http: HttpClient,
  21. public authSer: AuthServiceService,
  22. private spinner: NgxSpinnerService) { }
  23. pageId: number;
  24. usersList = [];
  25. usersId = [];
  26. count: number;
  27. perPagePagenation: number;
  28. p: number[] = [];
  29. currentPage:number = 1;
  30. filtterStatus = '';
  31. typeLink: string = '';
  32. typeService: string = '';
  33. selectedAll: any;
  34. serviceId:number;
  35. userLoginId:number;
  36. pages = [];
  37. dataTableNumber: number = 5;
  38. checkListType: boolean;
  39. @ViewChild('') filtterBar = '';
  40. ngOnInit() {
  41. //show spinner
  42. this.spinner.show();
  43. //init values of permisions
  44. this.authSer.showAddBtn = false;
  45. this.authSer.showDeleteBtn = false;
  46. this.authSer.showEditBtn = false;
  47. this.authSer.showPermissionsBtn = false;
  48. //show / hide notification search in header
  49. this.authSer.notificationLogin = true;
  50. this.authSer.showSearchHeader = false;
  51. this.authSer.showHeaderLogin = false;
  52. this.authSer.showHeaderDashBoard = true;
  53. this.authSer.showDashboardHeader = true;
  54. this.authSer.internalHeader = false;
  55. //catch id of page
  56. this.route.params.subscribe(
  57. (params: Params) => {
  58. this.pageId = params['listPageId'];
  59. localStorage.setItem('pageIdActive', params['listPageId']);
  60. if(this.pageId == 1) {
  61. this.typeLink = 'المستخدمين';
  62. this.typeService = 'خدمه الصلاحيات';
  63. this.checkListType = true;
  64. } else if(this.pageId == 9) {
  65. this.typeLink = 'إنضم إلينا';
  66. this.typeService = 'خدمه إداره المحتوي';
  67. this.checkListType = false;
  68. }
  69. }
  70. );
  71. //to control of pwermision of each user
  72. this.route.parent.params.subscribe(
  73. (params: Params) => {
  74. this.userLoginId = params['userID']; //user login id
  75. this.serviceId = params['serviceID']; //service id
  76. if(this.pageId == 1) {
  77. //get pages with its permisions
  78. this.userSer.getPagesPermetiotns(this.userLoginId, this.serviceId).subscribe(
  79. (responce)=>{
  80. this.pages = responce['pages'];
  81. for(let i = 0; i< this.pages.length; i++) {
  82. if(this.pages[i].id == 1) {
  83. for(let j = 0; j < this.pages[i].permissions.length; j++) {
  84. if(this.pages[i].permissions[j].name == 'add_user'){
  85. this.authSer.showAddBtn = true;
  86. }
  87. if(this.pages[i].permissions[j].name == 'edit_user'){
  88. this.authSer.showEditBtn = true;
  89. }
  90. if(this.pages[i].permissions[j].name == 'delete_user'){
  91. this.authSer.showDeleteBtn = true;
  92. }
  93. if(this.pages[i].permissions[j].name == 'asign_roles_permissions_to_user'){
  94. this.authSer.showPermissionsBtn = true;
  95. }
  96. }
  97. }else {
  98. console.log('no users');
  99. }
  100. }
  101. },
  102. (error) => {
  103. console.log(error);
  104. }
  105. );
  106. //get list of users
  107. this.userSer.getPageData(this.pageId, this.currentPage , this.dataTableNumber).subscribe(
  108. (responce) => {
  109. this.usersList = responce['users'];
  110. console.log(this.usersList);
  111. for (var i = 0; i < this.usersList.length; i++) {
  112. this.usersList[i].selected = false;
  113. }
  114. this.count = responce['count'];
  115. this.perPagePagenation = responce['per_page'];
  116. this.spinner.hide();
  117. },
  118. (error) => {console.log(error)}
  119. );
  120. } else if(this.pageId == 9) {
  121. //get join us pages with its permisions
  122. this.userSer.getPagesPermetiotns(this.userLoginId, this.serviceId).subscribe(
  123. (responce)=>{
  124. this.pages = responce['pages'];
  125. for(let i = 0; i< this.pages.length; i++) {
  126. if(this.pages[i].id == 9) {
  127. for(let j = 0; j < this.pages[i].permissions.length; j++) {
  128. if(this.pages[i].permissions[j].name == 'add_join_us_user'){
  129. this.authSer.showAddBtn = true;
  130. }
  131. if(this.pages[i].permissions[j].name == 'edit_join_us_user'){
  132. this.authSer.showEditBtn = true;
  133. }
  134. if(this.pages[i].permissions[j].name == 'delete_join_us_users'){
  135. this.authSer.showDeleteBtn = true;
  136. }
  137. }
  138. }else {
  139. console.log('no join us');
  140. }
  141. }
  142. },
  143. (error) => {
  144. console.log(error);
  145. }
  146. );
  147. //get list of join us
  148. this.userSer.getPageData(this.pageId, this.currentPage , this.dataTableNumber).subscribe(
  149. (responce) => {
  150. console.log(responce);
  151. this.usersList = responce['join_wes'];
  152. console.log(this.usersList);
  153. for (var i = 0; i < this.usersList.length; i++) {
  154. this.usersList[i].selected = false;
  155. }
  156. this.count = responce['count'];
  157. this.perPagePagenation = responce['per_page'];
  158. this.spinner.hide();
  159. },
  160. (error) => {console.log(error)}
  161. );
  162. }
  163. }
  164. );
  165. };
  166. //make all checkbox of user checked
  167. selectAll() {
  168. for (var i = 0; i < this.usersList.length; i++) {
  169. this.usersList[i].selected = this.selectedAll;
  170. }
  171. };
  172. checkIfAllSelected() {
  173. this.selectedAll = this.usersList.every(function(item:any) {
  174. return item.selected == true;
  175. })
  176. };
  177. //change page
  178. onPageChange(pagenationNumber) {
  179. this.spinner.show();
  180. this.currentPage = pagenationNumber;
  181. this.usersList = [];
  182. //console.log(pagenationNumber);
  183. //console.log(this.pageId);
  184. if(this.pageId == 1){
  185. this.userSer.getPageData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  186. (responce) => {
  187. console.log(responce);
  188. this.usersList = responce['users'];
  189. console.log(this.usersList);
  190. this.spinner.hide();
  191. },
  192. (error) => {
  193. console.log(error);
  194. this.spinner.hide();
  195. }
  196. );
  197. } else if(this.pageId == 9) {
  198. this.userSer.getPageData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  199. (responce) => {
  200. console.log(responce);
  201. this.usersList = responce['join_wes'];
  202. console.log(this.usersList);
  203. this.spinner.hide();
  204. },
  205. (error) => {
  206. console.log(error);
  207. this.spinner.hide();
  208. }
  209. );
  210. }
  211. };
  212. //edit user function
  213. onEdit(id) {
  214. if(this.pageId == 1){
  215. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'user/editUs/' + id]);
  216. } else if(this.pageId == 9){
  217. console.log('service/' + this.userLoginId + '/' + this.serviceId + '/' + 'joinUs/edit/' + id);
  218. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'joinUs/edit/' + id]);
  219. }
  220. }
  221. //add user function
  222. onAddUser() {
  223. if(this.pageId == 1){
  224. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'addu/user']);
  225. } else if(this.pageId == 9){
  226. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'addJoinUs']);
  227. }
  228. }
  229. //delete users function
  230. onDeleteUsers() {
  231. this.usersId = [];
  232. for(let i = 0; i < this.usersList.length; i++) {
  233. if(this.usersList[i].selected == true) {
  234. this.usersId.push(this.usersList[i].id);
  235. }
  236. }
  237. if(this.usersId.length > 0) {
  238. const dialogRef = this.modal.alert()
  239. .size('sm')
  240. .showClose(true)
  241. .title('تأكيد الحذف')
  242. .body(`
  243. <h4>هل ترغب في حذف العناصر المحدده ؟ </h4>
  244. `)
  245. .open();
  246. dialogRef.result
  247. .then( result =>
  248. this.userSer.deleteUser(this.usersId, this.pageId).subscribe(
  249. (responce) => {
  250. console.log(responce);
  251. this.toastr.success('تم الحذف');
  252. this.spinner.show();
  253. this.usersList = [];
  254. if(this.pageId == 1) {
  255. this.userSer.getPageData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  256. (responce) => {
  257. console.log(responce);
  258. this.usersList = responce['users'];
  259. console.log(this.usersList);
  260. this.spinner.hide();
  261. },
  262. (error) => {
  263. console.log(error);
  264. this.spinner.hide();
  265. }
  266. );
  267. } else if(this.pageId == 9) {
  268. this.userSer.getPageData(this.pageId, this.currentPage , this.dataTableNumber).subscribe(
  269. (responce) => {
  270. console.log(responce);
  271. this.usersList = responce['join_wes'];
  272. console.log(this.usersList);
  273. for (var i = 0; i < this.usersList.length; i++) {
  274. this.usersList[i].selected = false;
  275. }
  276. this.count = responce['count'];
  277. this.perPagePagenation = responce['per_page'];
  278. this.spinner.hide();
  279. },
  280. (error) => {console.log(error)}
  281. );
  282. }
  283. },
  284. (error) => {
  285. console.log(error);
  286. if(error['error'].error == 'user already used') {
  287. this.toastr.warning('لايمكن مسح المستخدمين المحددين لإرتباطهم بالنظام !');
  288. }
  289. },
  290. )
  291. );
  292. } else {
  293. this.toastr.warning('لم يتم إختيار أي عنصر للمسح !');
  294. }
  295. }
  296. //filtter function
  297. filtterFunc(data) {
  298. this.usersList = [];
  299. console.log(data.target.value);
  300. const dataSearch = data.target.value;
  301. this.currentPage = 1;
  302. if(this.pageId == 1){
  303. this.userSer.getDataUSerSearchBar(dataSearch, this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  304. (responce) => {
  305. this.usersList = responce['users'];
  306. this.count = responce['count'];
  307. this.perPagePagenation = responce['per_page'];
  308. console.log('filtter count', this.count);
  309. console.log('filtter perPagePAgenation', this.perPagePagenation);
  310. },
  311. (error) => {
  312. console.log(error)
  313. }
  314. );
  315. } else if(this.pageId == 9){
  316. this.userSer.getDataUSerSearchBar(dataSearch, this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  317. (responce) => {
  318. this.usersList = responce['join_wes'];
  319. this.count = responce['count'];
  320. this.perPagePagenation = responce['per_page'];
  321. console.log('filtter count', this.count);
  322. console.log('filtter perPagePAgenation', this.perPagePagenation);
  323. },
  324. (error) => {
  325. console.log(error)
  326. }
  327. );
  328. }
  329. }
  330. //get permission user
  331. onGetPermission(userIndex) {
  332. this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId +'/userPermission/' + userIndex]);
  333. }
  334. onGetValue(event) {
  335. this.spinner.show();
  336. this.usersList = [];
  337. this.dataTableNumber = event.target.value;
  338. if(this.pageId == 1){
  339. this.userSer.getPageData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  340. (responce) => {
  341. console.log(responce);
  342. this.usersList = responce['users'];
  343. this.count = responce['count'];
  344. this.perPagePagenation = responce['per_page'];
  345. console.log(this.usersList);
  346. this.spinner.hide();
  347. },
  348. (error) => {
  349. console.log(error);
  350. this.spinner.hide();
  351. }
  352. );
  353. } else if(this.pageId ==9){
  354. this.userSer.getPageData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
  355. (responce) => {
  356. console.log(responce);
  357. this.usersList = responce['join_wes'];
  358. this.count = responce['count'];
  359. this.perPagePagenation = responce['per_page'];
  360. console.log(this.usersList);
  361. this.spinner.hide();
  362. },
  363. (error) => {
  364. console.log(error);
  365. this.spinner.hide();
  366. }
  367. );
  368. }
  369. }
  370. }