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

@Component({
  selector: 'app-frame-res-detail',
  templateUrl: './frame-res-detail.component.html',
  styleUrls: ['./frame-res-detail.component.scss']
})
export class FrameResDetailComponent extends FrameEditComponent implements OnInit {

  resArray = [];
  constructor(bsModalRef: BsModalRef, fb: FormBuilder, san: DomSanitizer) {
    super(bsModalRef, san);
    this.valForm = fb.group({
      'res_id': [null, Validators.compose([Validators.required, Validators.max(99999999), Validators.min(10000)])],
      'res_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(32)])],
      'res_alias': [null, Validators.compose([Validators.required, Validators.minLength(4), Validators.maxLength(32)])],
      'res_url': [null],
      'res_level': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(4)])],
      'res_css': [null],
      'res_type': [null, Validators.required],
      'res_status': [null, Validators.required],
      'res_pid': [null, Validators.required],
      'res_order': [null, Validators.compose([Validators.required, Validators.min(1), Validators.max(99999999)])],
      'res_desc': [null]
    });
  }

  ngOnInit() {
    setTimeout(() => {
      const res = {'res_id': 0, 'res_name': '顶级资源', 'res_level': 0};
      FrameService.findChildRes(this.ResponseArray, res);
      const array = [];
      array.push(res);
      FrameService.makeResArray(res, array);
      for (const s of array) {
        if (s.res_type !== 'Fun') {
          if (s.res_level === 1) {
            s.res_note = `&nbsp;&nbsp;&nbsp;&nbsp;${s.res_name}`;
          } else if (s.res_level === 2) {
            s.res_note = `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${s.res_name}`;
          } else if (s.res_level === 3) {
            s.res_note = `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${s.res_name}`;
          } else if (s.res_level === 4) {
            s.res_note = `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${s.res_name}`;
          } else {
            s.res_note = s.res_name;
          }
          this.resArray.push(s);
        }
      }

    }, 10);
  }


  changeResPid($event) {
    for(const r of this.resArray) {
      if (r.res_id + '' === this.currentEntity.res_pid + '') {
        this.currentEntity.res_level = r.res_level + 1;
        return;
      }
    }
  }
}
