add-menu.component.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { HttpClient } from '@angular/common/http';
  2. import { DashboardService } from './../../../shared/dashboard.service';
  3. import { MainMenuService } from './../../../shared/main-menu.service';
  4. import { AuthServiceService } from './../../../shared/auth-service.service';
  5. import { ActivatedRoute, Params } from '@angular/router';
  6. import { Location } from '@angular/common';
  7. import { Component, OnInit, ViewChild } from '@angular/core';
  8. import { NgxSpinnerService } from 'ngx-spinner';
  9. import { ToastrService } from 'ngx-toastr';
  10. import { NgForm } from '@angular/forms';
  11. @Component({
  12. selector: 'app-add-menu',
  13. templateUrl: './add-menu.component.html',
  14. styleUrls: ['./add-menu.component.css']
  15. })
  16. export class AddMenuComponent implements OnInit {
  17. @ViewChild('f') externalFormData: NgForm;
  18. urlImg: string = '../../assets/image/avatar.png';
  19. imageBase64: string = '';
  20. photoType: string = '';
  21. checkChangeImage: boolean = false;
  22. checkValidImg: boolean = true;
  23. typeMode: boolean = false;
  24. photoEdit: boolean = true;
  25. checkSaveClick:boolean = false;
  26. servicesName:string = '';
  27. typeLink: string = '';
  28. menuId: number;
  29. serviceId: number;
  30. parentList = [];
  31. internalPages = [];
  32. menu = {
  33. name: '',
  34. name_en: '',
  35. parent_id: '',
  36. link: '',
  37. link2: '',
  38. status: '',
  39. ranking: '',
  40. link_type: '',
  41. };
  42. linkTypeCahnge: string = '';
  43. constructor(private toastr: ToastrService,
  44. private spinner: NgxSpinnerService,
  45. private location: Location,
  46. private route: ActivatedRoute,
  47. private authSer: AuthServiceService,
  48. private authService: AuthServiceService,
  49. private dashBoardService: DashboardService,
  50. private http: HttpClient,
  51. private menuService: MainMenuService, ) {
  52. this.spinner.show();
  53. }
  54. ngOnInit() {
  55. //show / hide notification search in header
  56. this.authSer.notificationLogin = true;
  57. this.authSer.showSearchHeader = false;
  58. this.authSer.showHeaderLogin = false;
  59. this.authSer.showHeaderDashBoard = true;
  60. this.authSer.showDashboardHeader = true;
  61. this.authSer.internalHeader = false;
  62. this.menu.status = '1';
  63. this.route.parent.params.subscribe(
  64. (params: Params) => {
  65. this.serviceId = params['serviceID'];
  66. if(this.serviceId == 6) {
  67. this.servicesName = 'خدمه إداره المحتوي';
  68. } else if(this.serviceId == 2) {
  69. this.servicesName = 'خدمه أدراه الصفحه الخارجيه';
  70. }
  71. }
  72. );
  73. this.route.params.subscribe(
  74. (params: Params) => {
  75. if(params['typeMenuMode'] == 'edit') {
  76. this.typeMode = true;
  77. this.typeLink = 'تعديل';
  78. this.menuId = params['editMenuId'];
  79. this.dashBoardService.getItemData(this.menuId, 'mainMenu').subscribe(
  80. (responce) => {
  81. console.log(responce);
  82. console.log(responce['menu']);
  83. this.menu.name = responce['menu'].name;
  84. this.menu.name_en = responce['menu'].name_en;
  85. this.menu.link = responce['menu'].link;
  86. this.menu.status = responce['menu'].status;
  87. this.menu.ranking = responce['menu'].ranking;
  88. this.menu.parent_id = responce['menu'].parent_id ? responce['menu'].parent_id : '';
  89. this.menu.link_type = responce['menu'].link_type;
  90. this.menu.link = responce['menu'].link ? responce['menu'].link : '';
  91. this.menu.link2 = responce['menu'].link2 ? responce['menu'].link2 : '';
  92. if(responce['menu'].photo) {
  93. this.checkValidImg = false;
  94. this.urlImg = this.authService.pathImg + responce['menu'].photo;
  95. }
  96. this.spinner.hide();
  97. },
  98. (error) => {
  99. console.log(error);
  100. }
  101. )
  102. } else {
  103. this.typeLink = 'أنشاء جديد';
  104. this.spinner.hide();
  105. }
  106. }
  107. );
  108. //get external pages
  109. this.http.get(this.authSer.pathApi + '/system_pages_list').subscribe(
  110. (responce) => {
  111. console.log('external pagesssssssssssssssss' , responce);
  112. this.internalPages = responce['pages'];
  113. }
  114. )
  115. this.menuService.getParentList().subscribe(
  116. (responce) => {
  117. console.log('parents' , responce);
  118. this.parentList = responce['menus'];
  119. },
  120. (error) => {
  121. console.log(error);
  122. }
  123. );
  124. };
  125. onFileChanges(event) {
  126. console.log(event);
  127. this.imageBase64 = event[0].base64;
  128. this.photoType = event[0].type.split('/');
  129. console.log(this.photoType[1]);
  130. this.checkChangeImage = true;
  131. this.checkValidImg = false;
  132. }
  133. getUrl(event) {
  134. if (event.target.files && event.target.files[0]) {
  135. var reader = new FileReader();
  136. reader.readAsDataURL(event.target.files[0]); // read file as data url
  137. reader.onload = (event) => { // called once readAsDataURL is completed
  138. this.urlImg = event.target['result'];
  139. }
  140. }
  141. }
  142. linkTypeValue(linkTypeVal) {
  143. console.log(linkTypeVal);
  144. this.menu.link_type = linkTypeVal.target.value;
  145. }
  146. //on submitted
  147. onSubmitted() {
  148. this.checkSaveClick = true;//to make save disabled after one click
  149. const formData = this.externalFormData.value;
  150. console.log(formData);
  151. if(this.checkChangeImage){
  152. formData['photo'] = this.imageBase64;
  153. formData['photo_type'] = this.photoType[1].toLowerCase();
  154. } else {
  155. delete formData.photo;
  156. delete formData.photo_type;
  157. this.photoEdit = false;
  158. console.log(formData);
  159. }
  160. if(this.typeMode){
  161. if(this.imageBase64 == '' && this.photoEdit == true){
  162. this.toastr.warning('قم باختيار صوره !');
  163. this.checkSaveClick = false;
  164. } else if(formData.photo_type != 'png'){
  165. this.toastr.warning('الصوره يجب أن تكون بصيغه png');
  166. this.checkSaveClick = false;
  167. } else {
  168. if(this.serviceId == 6) {
  169. this.dashBoardService.editItem( this.menuId, formData, 'internalMenu').subscribe(
  170. (responce) => {
  171. console.log(responce);
  172. this.toastr.success('تمت التعديل بنجاح');
  173. this.location.back();
  174. this.checkSaveClick = false;
  175. },
  176. (error) => {
  177. this.checkSaveClick = false;
  178. console.log(error);
  179. this.toastr.error('خطأ في التعديل !');
  180. }
  181. );
  182. } else if(this.serviceId == 2) {
  183. this.dashBoardService.editItem( this.menuId, formData, 'externalMenu').subscribe(
  184. (responce) => {
  185. console.log(responce);
  186. this.toastr.success('تمت التعديل بنجاح');
  187. this.location.back();
  188. this.checkSaveClick = false;
  189. },
  190. (error) => {
  191. this.checkSaveClick = false;
  192. console.log(error);
  193. this.toastr.error('خطأ في التعديل !');
  194. }
  195. );
  196. }
  197. }
  198. } else {
  199. if(this.imageBase64 == ''){
  200. this.toastr.warning('قم باختيار صوره !');
  201. this.checkSaveClick = false;
  202. } else if(formData.photo_type != 'png'){
  203. this.toastr.warning('الصوره يجب أن تكون بصيغه png');
  204. this.checkSaveClick = false;
  205. } else {
  206. if(this.serviceId == 6){
  207. this.dashBoardService.addItem(formData, 'internalMainMenu').subscribe(
  208. (responce) => {
  209. console.log(responce);
  210. this.toastr.success('تمت الاضافه بنجاح');
  211. this.location.back();
  212. this.checkSaveClick = false;
  213. },
  214. (error) => {
  215. console.log(error);
  216. this.checkSaveClick = false;
  217. this.toastr.error('خطأ في الحفظ !');
  218. }
  219. );
  220. } else if(this.serviceId == 2){
  221. this.dashBoardService.addItem(formData, 'externalMainMenu').subscribe(
  222. (responce) => {
  223. console.log(responce);
  224. this.toastr.success('تمت الاضافه بنجاح');
  225. this.location.back();
  226. this.checkSaveClick = false;
  227. },
  228. (error) => {
  229. console.log(error);
  230. this.checkSaveClick = false;
  231. this.toastr.error('خطأ في الحفظ !');
  232. }
  233. );
  234. }
  235. }
  236. }
  237. }
  238. }