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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 } 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. constructor(
  37. private dashBoardSer: DashboardService,
  38. private toastr: ToastrService,
  39. public authSer: AuthServiceService,
  40. private route: ActivatedRoute,
  41. private spinner: NgxSpinnerService,
  42. private location: Location,
  43. private http: HttpClient
  44. ) { }
  45. @ViewChild('f') linkForm : NgForm;
  46. formData = {
  47. housing_complex_id:'',
  48. building_type_id:'',
  49. building_id: '',
  50. unit_id:'',
  51. covenants:[],
  52. }
  53. categoryId:number;
  54. ngOnInit() {
  55. this.route.params.subscribe(
  56. (params: Params) => {
  57. this.editPageId = +params['listPageEditId'];
  58. }
  59. );
  60. this.http.get(this.authSer.pathApi + '/housing_complexes_list').subscribe(
  61. res => {
  62. this.complex_list = res[('housing_complexes')];
  63. }
  64. );
  65. this.http.get(this.authSer.pathApi+ '/get_covenant_categories_list').subscribe(
  66. res=>{
  67. this.covenant_categories = res['covenant_categories'];
  68. console.log(this.covenant_categories);
  69. this.covenant_categories.forEach(conv => {
  70. conv.covenants.forEach(con => {
  71. con.covenant_amount = 0;
  72. })
  73. this.formData.covenants.push({
  74. category_id: conv.id,
  75. covenants_data: conv.covenants
  76. })
  77. });
  78. console.log(this.formData);
  79. },
  80. err =>{
  81. console.log(err);
  82. }
  83. )
  84. if (this.editPageId) {
  85. this.spinner.show();
  86. this.isEdit = true;
  87. this.show_amount = true;
  88. // this.showTypes = true;
  89. this.typePage = 'تعديل';
  90. this.dashBoardSer.getItemData(this.editPageId, 'linkUnitToCovenants' ).subscribe(
  91. res => {
  92. console.log('ssss', res);
  93. this.formData.housing_complex_id = res['unit'].housing_complex_id;
  94. this.getBuildingType( this.formData.housing_complex_id);
  95. this.formData.building_type_id = res['unit'].building_type_id;
  96. this.getBuilding(this.formData.building_type_id);
  97. this.formData.building_id = res['unit'].building_id;
  98. // this.getUnit( this.formData.building_id);
  99. this.formData.unit_id = res['unit'].unit_id;
  100. this.covenant(this.formData.covenants[0].category_id);
  101. this.covenants = res['unit'].covenants;
  102. console.log('conv form',this.formData.covenants);
  103. console.log('conv', this.covenants);
  104. for (let i = 0 , n = this.formData.covenants.length; i < n; i++) {
  105. const arr= this.formData.covenants[i].covenants_data;
  106. for (let ii = 0, k = arr.length; ii < k; ii++) {
  107. for (let j = 0 , m = this.covenants.length; j < m; j++) {
  108. if(arr[ii].id == this.covenants[j].covenant_id){
  109. arr[ii].selected = true;
  110. arr[ii].covenant_amount = this.covenants[j].covenant_amount;
  111. }
  112. }
  113. }
  114. }
  115. this.spinner.hide();
  116. },
  117. err => {
  118. console.log(err);
  119. }
  120. );
  121. }else {
  122. this.typePage = 'اضافة';
  123. }
  124. }
  125. getBuildingType(event){
  126. console.log('sdsdsd',event);
  127. const id = event;
  128. console.log(id);
  129. this.http.get(this.authSer.pathApi+ '/building_types_list/' + id ).subscribe(
  130. res => {
  131. console.log(res);
  132. this.building_types = res[('building_types')];
  133. if(this.building_types.length > 0)
  134. this.isComplex = true;
  135. else {
  136. this.isComplex = false;
  137. this.isBuildingType = false;
  138. this.isBuilding = false;
  139. }
  140. },
  141. err => {
  142. console.log(err);
  143. }
  144. );
  145. }
  146. getBuilding(event){
  147. this.http.get(this.authSer.pathApi + '/buildings_list_by_type_id/' + event ).subscribe(
  148. res => {
  149. console.log('res',res);
  150. this.buildings = res['buildings'];
  151. if(this.buildings.length > 0){
  152. this.isBuildingType = true;
  153. console.log(this.buildings);
  154. if(this.editPageId) {
  155. this.getUnit(this.formData.building_id );
  156. }
  157. }else{
  158. this.isBuildingType = false;
  159. this.isBuilding = false;
  160. }
  161. },
  162. err => {
  163. console.log(err);
  164. }
  165. );
  166. }
  167. getUnit(event){
  168. this.http.get(this.authSer.pathApi + '/units_list_by_building_id/' + event ).subscribe(
  169. res => {
  170. console.log(res);
  171. this.units = res['units'];
  172. if(this.units.length > 0){
  173. this.isBuilding = true;
  174. console.log(this.units);
  175. }else{
  176. this.isBuilding = false;
  177. }
  178. },
  179. err => {
  180. console.log(err);
  181. }
  182. );
  183. }
  184. covenant(event){
  185. const id = event;
  186. console.log(id);
  187. this.categoryId = id;
  188. console.log(this.formData.covenants);
  189. }
  190. showcovenant_categories(){
  191. this.isConvenat = true;
  192. }
  193. checked(event){
  194. console.log(event);
  195. for(let i = 0, m = this.formData.covenants.length; i < m ; i++) {
  196. //final finalCov array to add categories in covenants
  197. for(let j = 0 , n = this.formData.covenants[i].covenants_data.length; j < n; j++) {
  198. if(event == this.formData.covenants[i].covenants_data[j].id ){
  199. this.formData.covenants[i].covenants_data[j].covenant_amount = 1
  200. // if(this.formData.covenants[i].covenants_data[j].selected == false){
  201. // this.formData.covenants[i].covenants_data[j].covenant_amount = 1
  202. // }else{
  203. // this.formData.covenants[i].covenants_data[j].covenant_amount = 0
  204. // }
  205. }
  206. }
  207. };
  208. }
  209. onSubmitted(){
  210. //final array to add covenants
  211. const finalCov = [];
  212. for(let i = 0; i < this.formData.covenants.length; i++) {
  213. //final finalCov array to add categories in covenants
  214. for(let j = 0; j < this.formData.covenants[i].covenants_data.length; j++) {
  215. if(this.formData.covenants[i].covenants_data[j].selected != undefined && this.formData.covenants[i].covenants_data[j].selected == true) {
  216. const realamount = this.formData.covenants[i].covenants_data[j].covenant_amount;
  217. const available_amount = this.formData.covenants[i].covenants_data[j].available_amount;
  218. if (realamount > available_amount){
  219. this.over = true;
  220. return 0;
  221. }
  222. finalCov.push({
  223. covenant_id:this.formData.covenants[i].covenants_data[j].id,
  224. covenant_amount: this.formData.covenants[i].covenants_data[j].covenant_amount,
  225. category_id: this.formData.covenants[i].covenants_data[j].category_id
  226. })
  227. }
  228. }
  229. };
  230. this.final = this.formData.covenants
  231. this.formData.covenants = finalCov;
  232. console.log(finalCov.length);
  233. if(finalCov.length == 0){
  234. this.toastr.warning('من فضلك قم بأختيار عهدة!!');
  235. this.formData.covenants = this.final;
  236. this.final = [];
  237. return 0;
  238. }
  239. console.log(this.formData);
  240. if(this.editPageId) {
  241. this.dashBoardSer.editItem(this.editPageId, this.formData, 'linkUnitToCovenants').subscribe(
  242. res => {
  243. console.log(res);
  244. this.toastr.success('تم التعديل بنجاح');
  245. this.location.back();
  246. },
  247. err => {
  248. this.formData.covenants = this.final;
  249. this.final = [];
  250. console.log(err);
  251. this.toastr.error('خطأ في الخادم ، رجاء المحارله لاحقا');
  252. }
  253. )
  254. } else {
  255. this.dashBoardSer.addItem( this.formData , 'linkUnitToCovenants').subscribe(
  256. res => {
  257. console.log('ADD',res);
  258. this.toastr.success('تم الإضافه بنجاح');
  259. this.checkSaveclick = false;
  260. this.location.back();
  261. },
  262. err => {
  263. this.formData.covenants = this.final;
  264. this.final = [];
  265. console.log(err);
  266. this.checkSaveclick = false;
  267. this.toastr.error('خطأ في الخادم ، حاول لاحقا');
  268. }
  269. );
  270. }
  271. }
  272. }