link-unit-to-covenants-add.component.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import { HttpClient } from '@angular/common/http';
  2. import { NgxSpinnerService } from 'ngx-spinner';
  3. import { ActivatedRoute, Params } from '@angular/router';
  4. import { Location } from '@angular/common';
  5. import { AuthServiceService } from './../../../shared/auth-service.service';
  6. import { ToastrService } from 'ngx-toastr';
  7. import { DashboardService } from './../../../shared/dashboard.service';
  8. import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
  9. import { NgForm } from '@angular/forms';
  10. @Component({
  11. selector: 'app-link-unit-to-covenants-add',
  12. templateUrl: './link-unit-to-covenants-add.component.html',
  13. styleUrls: ['./link-unit-to-covenants-add.component.css']
  14. })
  15. export class LinkUnitToCovenantsAddComponent implements OnInit {
  16. typePage: string = '';
  17. isComplex = false;
  18. isBuildingType = false;
  19. show_amount = false;
  20. isBuilding = false;
  21. amount = [];
  22. complex_list =[];
  23. building_types=[];
  24. buildings = [];
  25. valueType: string = '';
  26. checkSaveclick:boolean = false;
  27. check : boolean = false;
  28. isEdit: boolean = false;
  29. isConvenat = false;
  30. editPageId: number;
  31. units =[];
  32. covenant_categories = [];
  33. covenants = [];
  34. final = [] ;
  35. over : boolean = false;
  36. unitId = '';
  37. constructor(
  38. private dashBoardSer: DashboardService,
  39. private toastr: ToastrService,
  40. public authSer: AuthServiceService,
  41. private route: ActivatedRoute,
  42. private spinner: NgxSpinnerService,
  43. private location: Location,
  44. private http: HttpClient
  45. ) { }
  46. @ViewChild('f') linkForm : NgForm;
  47. @ViewChild('amountVal') amountVal: ElementRef;
  48. formData = {
  49. housing_complex_id:'',
  50. building_type_id:'',
  51. building_id: '',
  52. unit_id:'',
  53. covenants:[],
  54. }
  55. categoryId:number;
  56. ngOnInit() {
  57. this.route.params.subscribe(
  58. (params: Params) => {
  59. this.editPageId = +params['listPageEditId'];
  60. }
  61. );
  62. this.http.get(this.authSer.pathApi + '/housing_complexes_list').subscribe(
  63. res => {
  64. this.complex_list = res[('housing_complexes')];
  65. }
  66. );
  67. this.http.get(this.authSer.pathApi+ '/get_covenant_categories_list').subscribe(
  68. res=>{
  69. this.covenant_categories = res['covenant_categories'];
  70. console.log(this.covenant_categories);
  71. this.covenant_categories.forEach(conv => {
  72. conv.covenants.forEach(con => {
  73. con.covenant_amount = 0;
  74. })
  75. this.formData.covenants.push({
  76. category_id: conv.id,
  77. covenants_data: conv.covenants
  78. })
  79. });
  80. console.log(this.formData);
  81. },
  82. err =>{
  83. console.log(err);
  84. }
  85. )
  86. if (this.editPageId) {
  87. this.spinner.show();
  88. this.isEdit = true;
  89. this.show_amount = true;
  90. // this.showTypes = true;
  91. this.typePage = 'تعديل';
  92. this.dashBoardSer.getItemData(this.editPageId, 'linkUnitToCovenants' ).subscribe(
  93. res => {
  94. console.log('ssss', res);
  95. this.formData.housing_complex_id = res['unit'].housing_complex_id;
  96. this.getBuildingType( this.formData.housing_complex_id);
  97. this.formData.building_type_id = res['unit'].building_type_id;
  98. this.unitId = res['unit'].id;
  99. this.getBuilding(this.formData.building_type_id);
  100. this.formData.building_id = res['unit'].building_id;
  101. this.covenant(this.formData.covenants[0].category_id);
  102. this.covenants = res['unit'].covenants;
  103. console.log('conv form',this.formData.covenants);
  104. console.log('conv', this.covenants);
  105. for (let i = 0 , n = this.formData.covenants.length; i < n; i++) {
  106. const arr= this.formData.covenants[i].covenants_data;
  107. for (let ii = 0, k = arr.length; ii < k; ii++) {
  108. for (let j = 0 , m = this.covenants.length; j < m; j++) {
  109. if(arr[ii].id == this.covenants[j].covenant_id){
  110. arr[ii].selected = true;
  111. arr[ii].covenant_amount = this.covenants[j].covenant_amount;
  112. }
  113. }
  114. }
  115. }
  116. this.spinner.hide();
  117. },
  118. err => {
  119. console.log(err);
  120. }
  121. );
  122. }else {
  123. this.typePage = 'اضافة';
  124. }
  125. }
  126. getBuildingType(event){
  127. this.building_types = []
  128. console.log('sdsdsd',event);
  129. const id = event;
  130. this.isComplex = false;
  131. this.isBuildingType = false;
  132. this.show_amount = false;
  133. this.isBuilding = false;
  134. console.log(id);
  135. this.http.get(this.authSer.pathApi+ '/building_types_list/' + id ).subscribe(
  136. res => {
  137. console.log(res);
  138. this.building_types = res[('building_types')];
  139. if(this.building_types.length > 0)
  140. this.isComplex = true;
  141. else {
  142. this.isComplex = false;
  143. this.isBuildingType = false;
  144. this.isBuilding = false;
  145. this.toastr.warning('لا يوجد انواع مباني لهذا المجمع')
  146. }
  147. },
  148. err => {
  149. console.log(err);
  150. }
  151. );
  152. }
  153. getBuilding(event){
  154. this.buildings = []
  155. this.http.get(this.authSer.pathApi + '/buildings_list_by_type_id/' + event ).subscribe(
  156. res => {
  157. console.log('res',res);
  158. this.buildings = res['buildings'];
  159. if(this.buildings.length > 0){
  160. this.isBuildingType = true;
  161. console.log(this.buildings);
  162. if(this.editPageId) {
  163. this.getUnit(this.formData.building_id );
  164. }
  165. }else{
  166. this.isBuildingType = false;
  167. this.isBuilding = false;
  168. this.isConvenat = false;
  169. this.toastr.warning('لا يوجد بيانات لهذا النوع من المباني')
  170. }
  171. },
  172. err => {
  173. console.log(err);
  174. }
  175. );
  176. }
  177. getUnit(event){
  178. console.log('unit id >>' , this.unitId);
  179. this.units = []
  180. this.http.get(this.authSer.pathApi + '/units_list_by_building_id/' + event ).subscribe(
  181. res => {
  182. console.log(res);
  183. this.units = res['units'];
  184. if(this.units.length > 0){
  185. this.isBuilding = true;
  186. console.log(this.units);
  187. }else{
  188. this.isBuilding = false;
  189. this.isConvenat = false;
  190. this.toastr.warning('لا يوجد وحدات لهذا المبنى ')
  191. }
  192. },
  193. err => {
  194. console.log(err);
  195. }
  196. );
  197. this.formData.unit_id = this.unitId;
  198. console.log('dffd',this.formData.unit_id);
  199. if (this.editPageId) {
  200. this.showcovenant_categories()
  201. }
  202. }
  203. covenant(event){
  204. const id = event;
  205. console.log(id);
  206. this.categoryId = id;
  207. console.log(this.formData.covenants);
  208. }
  209. check_max(value, max, name){
  210. console.log( 'ss',value, max, name);
  211. if(value > max){
  212. this.toastr.warning('عفوا لقد تجاوزت العدد المحدد لـ'+name)
  213. }
  214. }
  215. showcovenant_categories(){
  216. this.isConvenat = true;
  217. }
  218. checked(event){
  219. console.log(event);
  220. for(let i = 0, m = this.formData.covenants.length; i < m ; i++) {
  221. //final finalCov array to add categories in covenants
  222. for(let j = 0 , n = this.formData.covenants[i].covenants_data.length; j < n; j++) {
  223. if(event == this.formData.covenants[i].covenants_data[j].id ){
  224. // this.formData.covenants[i].covenants_data[j].covenant_amount = 1
  225. if(this.formData.covenants[i].covenants_data[j].selected == false){
  226. this.formData.covenants[i].covenants_data[j].covenant_amount = 0
  227. }else{
  228. this.formData.covenants[i].covenants_data[j].covenant_amount = 1
  229. }
  230. }
  231. }
  232. };
  233. }
  234. onSubmitted(){
  235. //final array to add covenants
  236. const finalCov = [];
  237. for(let i = 0; i < this.formData.covenants.length; i++) {
  238. //final finalCov array to add categories in covenants
  239. for(let j = 0; j < this.formData.covenants[i].covenants_data.length; j++) {
  240. if(this.formData.covenants[i].covenants_data[j].selected != undefined && this.formData.covenants[i].covenants_data[j].selected == true) {
  241. const realamount = this.formData.covenants[i].covenants_data[j].covenant_amount;
  242. const available_amount = this.formData.covenants[i].covenants_data[j].available_amount;
  243. if (realamount > available_amount){
  244. this.over = true;
  245. return 0;
  246. }
  247. finalCov.push({
  248. covenant_id:this.formData.covenants[i].covenants_data[j].id,
  249. covenant_amount: this.formData.covenants[i].covenants_data[j].covenant_amount,
  250. category_id: this.formData.covenants[i].covenants_data[j].category_id
  251. })
  252. }
  253. }
  254. };
  255. this.final = this.formData.covenants
  256. this.formData.covenants = finalCov;
  257. console.log(finalCov.length);
  258. if(finalCov.length == 0){
  259. this.toastr.warning('من فضلك قم بأختيار عهدة!!');
  260. this.formData.covenants = this.final;
  261. this.final = [];
  262. return 0;
  263. }
  264. console.log(this.formData);
  265. if(this.editPageId) {
  266. this.dashBoardSer.editItem(this.editPageId, this.formData, 'linkUnitToCovenants').subscribe(
  267. res => {
  268. console.log(res);
  269. this.toastr.success('تم التعديل بنجاح');
  270. this.location.back();
  271. },
  272. err => {
  273. this.formData.covenants = this.final;
  274. this.final = [];
  275. console.log(err);
  276. this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
  277. }
  278. )
  279. } else {
  280. this.dashBoardSer.addItem( this.formData , 'linkUnitToCovenants').subscribe(
  281. res => {
  282. console.log('ADD',res);
  283. this.toastr.success('تم الإضافه بنجاح');
  284. this.checkSaveclick = false;
  285. this.location.back();
  286. },
  287. err => {
  288. this.formData.covenants = this.final;
  289. this.final = [];
  290. console.log(err);
  291. this.checkSaveclick = false;
  292. this.toastr.error('خطأ في الخادم ، حاول لاحقا');
  293. }
  294. );
  295. }
  296. }
  297. }