import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {DomSanitizer} from '@angular/platform-browser';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

@Component({
  selector: 'app-conf-material-detail',
  templateUrl: './conf-material-detail.component.html',
  styleUrls: ['./conf-material-detail.component.scss']
})
export class ConfMaterialDetailComponent extends FrameDetailComponent implements OnInit {

  aspectArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder, sanitizer: DomSanitizer) {
    super(bsModalRef, sanitizer);
    this.detailForm = fb.group({
      'material_type': [null, Validators.compose([Validators.required])],
      'material_name': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(128)])],
      'upload_business': [null],
      'audit_business': [null],
      'material_status': [null, Validators.compose([Validators.required])],
      'audit_auto_status': [null, Validators.compose([Validators.required])],
      'audit_auto_param': [null],
      'material_order': [null, Validators.compose([Validators.required])],
      'material_explain': [null, Validators.compose([Validators.maxLength(2000)])],
      'apply_aspect_ids': [null]
    });
  }

  ngOnInit() {
    this.AjaxService.requestArray('conf/aspect/all.htm', {}, (array) => {
      this.aspectArray = array;
      for(const aspect of this.aspectArray) {
        aspect.aspect_full_name = aspect.aspect_code + '|' + aspect.aspect_name;
      }
    } )
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.audit_auto_status = 'InActive';
      this.currentEntity.material_status = 'Active';
    }
    const ids = [];
    if(this.currentEntity.apply_aspect_ids) {
      const aspect_ids =  this.currentEntity.apply_aspect_ids.split(',');
      for(const id of aspect_ids) {
        ids.push(parseInt(id));
      }
      this.currentEntity.apply_aspect_ids = ids;
    }
  }


}
