/** Angular Imports */ import { Component , OnInit } from '@angular/core' ; import { FormGroup , FormBuilder , Validators , FormControl } from '@angular/forms' ; import { Router , ActivatedRoute } from '@angular/router' ; /** Custom Services */ import { ProductsService } from '../../products.service' ; /**
 * Create Product mix component.
 */ @ Component ( { selector : 'mifosx-create-product-mix' , templateUrl : './create-product-mix.component.html' , styleUrls : [ './create-product-mix.component.scss' ] } ) export class CreateProductMixComponent implements OnInit { /** Product mix form. */ productMixForm : FormGroup ; /** Products mix template data. */ productsMixTemplateData : any ; /** Product option data. */ productOptionData : any ; /** Product data. */ productData : any ; param { FormBuilder } formBuilder param { ProductsService } productsService param { ActivatedRoute } route param { Router } router constructor ( private formBuilder , private productsService , private route , private router ) { this . route . data . subscribe ( ( data ) => { this . productsMixTemplateData = data . productsMixTemplate ; } ) ; } /**
     * Creates the product mix form and conditional controls of the product mix form.
     */ ngOnInit ( ) { this . createProductMixForm ( ) ; this . setConditionalControls ( ) ; } /**
     * Creates the product mix form
     */ createProductMixForm ( ) { this . productOptionData = this . productsMixTemplateData . productOptions ; this . productMixForm = this . formBuilder . group ( { 'productId' : [ '' , Validators . required ] , 'restrictedProducts' : [ '' , Validators . required ] } ) ; } /**
     * Sets the conditional controls of the product mix form.
     */ setConditionalControls ( ) { this . productMixForm . get ( 'productId' ) . valueChanges . subscribe ( productId => { this . productData = undefined ; this . productMixForm . get ( 'restrictedProducts' ) . reset ( ) ; this . productsService . getProductMixTemplate ( productId ) . subscribe ( ( productMixTemplateData ) => { const restrictedProductsData = productMixTemplateData . restrictedProducts ; this . productData = [ ... restrictedProductsData , ... productMixTemplateData . allowedProducts ] ; this . productMixForm . get ( 'restrictedProducts' ) . setValue ( [ ... restrictedProductsData . map ( ( restrictedProduct ) => restrictedProduct . id ) ] ) ; } ) ; } ) ; } /**
     * Submits the product mix form and creates product mix,
     * if successful redirects to products mix.
     */ submit ( ) { const productMix = { restrictedProducts : this . productMixForm . value . restrictedProducts } ; const productMixId = this . productMixForm . value . productId ; this . productsService . createProductMix ( productMix , productMixId ) . subscribe ( ( response ) => { this . router . navigate ( [ '../' , response . resourceId ] , { relativeTo : this . route } ) ; } ) ; } }