import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
import {FormGroup} from '@angular/forms';
import 'rxjs';
import {FormsService} from '../services/forms.service';
@Component({
selector: 'master-details-list-component',
template: `
`,
styles: [`
.list {
margin: 20px;
}
.list-header {
cursor: pointer;
font-size: 18px;
font-weight: 500;
width: 100%;
background: ghostwhite;
}
ul {
list-style: none;
max-height: 0px;
overflow: hidden;
}
ul.active {
max-height: 20000px;
}
li {
width: 100%;
padding: 10px;
margin-bottom: 1px;
background: aliceblue;
}
li:hover {
background: lightgrey;
}
li .input-container {
display: flex;
flex-direction: row;
}
li .input-label, li .input-container {
cursor: pointer;
}
.check-mark {
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
font-size: 28px;
}
`]
})
export class MasterDetailsListComponent implements OnChanges{
@Input()
public items = [];
@Output()
public onChanges: EventEmitter = new EventEmitter();
public lists = [];
public questions = [];
public form: FormGroup = new FormGroup({});
constructor(private forms: FormsService){}
public ngOnChanges(simpleChanges: SimpleChanges){
if (simpleChanges.items){
// if (simpleChanges.items && simpleChanges.items.firstChange){
this.items.forEach(item => {
const list = this.forms.buildList(item);
let controls = {};
this.forms.buildList(list).map(listItem => {
Object.assign(controls, {[listItem.key]: listItem.control});
});
const listFormGroup: ListFormGroup = {list: list, formGroup: new FormGroup(controls), isActive: false};
this.lists = [...this.lists, listFormGroup];
})
}
}
}
interface ListFormGroup {
list: any[],
formGroup: FormGroup
isActive: boolean
}