add-footer.component.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { DashboardService } from './../../../shared/dashboard.service';
  2. import { AuthServiceService } from './../../../shared/auth-service.service';
  3. import { ActivatedRoute, Params } from '@angular/router';
  4. import { Location } from '@angular/common';
  5. import { Component, OnInit, ViewChild } from '@angular/core';
  6. import { NgxSpinnerService } from 'ngx-spinner';
  7. import { ToastrService } from 'ngx-toastr';
  8. import { NgForm } from '@angular/forms';
  9. @Component({
  10. selector: 'app-add-footer',
  11. templateUrl: './add-footer.component.html',
  12. styleUrls: ['./add-footer.component.css']
  13. })
  14. export class AddFooterComponent implements OnInit {
  15. @ViewChild('f') footerFormData: NgForm;
  16. files: any;
  17. urlImg: string = '../../assets/image/avatar.png';
  18. imageBase64: string = '';
  19. photoType: string = '';
  20. checkChangeImage: boolean = false;
  21. checkValidImg: boolean = true;
  22. typeMode: boolean = false;
  23. photoEdit: boolean = true;
  24. typeLink: string = '';
  25. externalId: number;
  26. serviceId: number;
  27. //to show emeil || description || link
  28. showEmailField: boolean = false;
  29. showLinkField: boolean = false;
  30. showDescriptionField: boolean = false;
  31. checkContactUs:boolean = false;
  32. checkOtherField: boolean = false;
  33. checkNotContactUs:boolean = false;
  34. contactUsPage: boolean = false;
  35. serviceName: string = '';
  36. footer = {
  37. name: '',
  38. name_en: '',
  39. link: '',
  40. status: '',
  41. type: '',
  42. email: '',
  43. description: '',
  44. description_en: '',
  45. };
  46. constructor(private toastr: ToastrService,
  47. private spinner: NgxSpinnerService,
  48. private location: Location,
  49. public authSer: AuthServiceService,
  50. private route: ActivatedRoute,
  51. private dashBoardService: DashboardService) { }
  52. ngOnInit() {
  53. //show / hide notification search in header
  54. this.authSer.notificationLogin = true;
  55. this.authSer.showSearchHeader = false;
  56. this.authSer.showHeaderLogin = false;
  57. this.authSer.showHeaderDashBoard = true;
  58. //show / hide notification search in header
  59. this.authSer.notificationLogin = true;
  60. this.authSer.showSearchHeader = false;
  61. this.authSer.showHeaderLogin = false;
  62. this.authSer.showHeaderDashBoard = true;
  63. this.authSer.showDashboardHeader = true;
  64. this.authSer.internalHeader = false;
  65. this.route.parent.params.subscribe(
  66. (params: Params) => {
  67. this.serviceId = params['serviceID'];
  68. if(this.serviceId == 2) {
  69. this.serviceName = 'خدمه إداره الصفحه الخارجيه';
  70. } else if (this.serviceId == 6) {
  71. this.serviceName = 'خدمه إداره المحتوي';
  72. }
  73. }
  74. );
  75. this.route.params.subscribe(
  76. (params: Params) => {
  77. if(params['typeFooterMode'] == 'edit') {
  78. this.typeLink = 'تعديل';
  79. this.spinner.show();
  80. this.typeMode = true;
  81. this.externalId = params['editFooterId'];
  82. this.dashBoardService.getItemData(this.externalId, 'footer').subscribe(
  83. (responce) => {
  84. console.log(responce['footer']);
  85. this.footer.name = responce['footer'].name;
  86. this.footer.name_en = responce['footer'].name_en;
  87. if(this.footer.name == 'تواصل معنا' || this.footer.name_en == 'contact us') {
  88. this.showEmailField = true;
  89. this.showDescriptionField = false;
  90. this.showLinkField = false;
  91. this.footer.description = '';
  92. this.footer.description_en = '';
  93. this.footer.link = '';
  94. this.contactUsPage = true;
  95. } else {
  96. this.checkNotContactUs = true;
  97. this.showDescriptionField = true;
  98. this.showEmailField = false;
  99. this.showLinkField = false;
  100. this.footer.email = '';
  101. this.footer.link = '';
  102. }
  103. this.footer.link = responce['footer'].link;
  104. this.footer.status = responce['footer'].status;
  105. this.footer.type = responce['footer'].type;
  106. this.footer.email = responce['footer'].email;
  107. this.footer.description = responce['footer'].description;
  108. this.footer.description_en = responce['footer'].description_en;
  109. if(responce['footer'].photo) {
  110. this.checkValidImg = false;
  111. this.urlImg = this.authSer.pathImg + responce['footer'].photo;
  112. }
  113. this.spinner.hide();
  114. },
  115. (error) => {
  116. console.log(error);
  117. }
  118. )
  119. } else {
  120. this.typeLink = 'إنشاء جديد';
  121. }
  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. //console.log(this.imageBase64);
  131. this.checkChangeImage = true;
  132. this.checkValidImg = false;
  133. }
  134. getUrl(event) {
  135. if (event.target.files && event.target.files[0]) {
  136. var reader = new FileReader();
  137. reader.readAsDataURL(event.target.files[0]); // read file as data url
  138. reader.onload = (event) => { // called once readAsDataURL is completed
  139. this.urlImg = event.target['result'];
  140. }
  141. }
  142. }
  143. onChangeType(event) {
  144. console.log(event.target.value);
  145. this.footer.type = event.target.value;
  146. if(event.target.value == 0 && this.showEmailField == true) {
  147. this.showDescriptionField = true;
  148. this.showLinkField = false;
  149. this.footer.link = '';
  150. this.footer.description = '';
  151. this.footer.description_en = '';
  152. } else if(event.target.value == 0 && this.checkNotContactUs == true) {
  153. this.showDescriptionField = true;
  154. this.showLinkField = false;
  155. this.showEmailField = false;
  156. this.footer.link = '';
  157. this.footer.email = '';
  158. } else if(event.target.value == 1 || event.target.value == 2 || this.showDescriptionField) {
  159. this.showLinkField = true;
  160. this.showEmailField = false;
  161. this.showDescriptionField = false;
  162. this.footer.description = '';
  163. this.footer.description_en = '';
  164. this.footer.email = '';
  165. } else {
  166. if(this.contactUsPage) {
  167. this.showDescriptionField = false;
  168. this.showEmailField = true;
  169. this.showLinkField = false;
  170. this.footer.link = '';
  171. this.footer.description = '';
  172. this.footer.description_en = '';
  173. } else {
  174. this.showDescriptionField = true;
  175. this.showEmailField = false;
  176. this.showLinkField = false;
  177. this.footer.email = '';
  178. this.footer.link = '';
  179. }
  180. }
  181. }
  182. //on submitted
  183. onSubmitted() {
  184. const formData = this.footerFormData.value;
  185. console.log(formData);
  186. if(this.checkChangeImage){
  187. formData['photo'] = this.imageBase64;
  188. formData['photo_type'] = this.photoType[1];
  189. } else {
  190. delete formData.photo;
  191. delete formData.photo_type;
  192. this.photoEdit = false;
  193. console.log(formData);
  194. }
  195. if(this.typeMode){
  196. if(this.photoType[1] != 'png' && this.photoEdit == true) {
  197. this.toastr.warning('الصوره يجب أن تكون بصيغه Png');
  198. } else if(this.imageBase64 == '' && this.photoEdit == true){
  199. this.toastr.warning('قم باختيار صوره !');
  200. } else {
  201. if(this.serviceId == 2) {
  202. this.dashBoardService.editItem( this.externalId, formData, 'internalFooter').subscribe(
  203. (responce) => {
  204. console.log(responce);
  205. this.toastr.success('تمت التعديل بنجاح');
  206. this.location.back();
  207. },
  208. (error) => {
  209. console.log(error);
  210. this.toastr.error('خطأ في التعديل !');
  211. }
  212. );
  213. } else if(this.serviceId == 6){
  214. this.dashBoardService.editItem( this.externalId, formData, 'externalFooter').subscribe(
  215. (responce) => {
  216. console.log(responce);
  217. this.toastr.success('تمت التعديل بنجاح');
  218. this.location.back();
  219. },
  220. (error) => {
  221. console.log(error);
  222. this.toastr.error('خطأ في التعديل !');
  223. }
  224. );
  225. }
  226. }
  227. } else {
  228. if(this.photoType[1] != 'png') {
  229. this.toastr.warning('الصوره يجب أن تكون بصيغه Png');
  230. } else if(this.imageBase64 == ''){
  231. this.toastr.warning('قم باختيار صوره !');
  232. } else {
  233. // formData['photo'] = this.imageBase64;
  234. // formData['photo_type'] = this.photoType[1];
  235. if(this.serviceId == 2) {
  236. this.dashBoardService.addItem(formData, 'externalFooter').subscribe(
  237. (responce) => {
  238. console.log(responce);
  239. this.toastr.success('تمت الاضافه بنجاح');
  240. this.location.back();
  241. },
  242. (error) => {
  243. console.log(error);
  244. this.toastr.error('خطأ في الحفظ !');
  245. }
  246. );
  247. } else if(this.serviceId == 6){
  248. this.dashBoardService.addItem(formData, 'internalFooter').subscribe(
  249. (responce) => {
  250. console.log(responce);
  251. this.toastr.success('تمت الاضافه بنجاح');
  252. this.location.back();
  253. },
  254. (error) => {
  255. console.log(error);
  256. this.toastr.error('خطأ في الحفظ !');
  257. }
  258. );
  259. }
  260. }
  261. }
  262. }
  263. }