import {Component, OnInit} from '@angular/core';
import {FrameDetailComponent} from "../../core/detail/frame.detail";
import {BsModalRef} from "ngx-bootstrap/modal";
import {FormBuilder, Validators} from "@angular/forms";
import {FrameResDetailComponent} from "../../res/frame-res-detail/frame-res-detail.component";
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from 'src/app/core/service/msg.service';

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

  resArray = [];
  detailArray = [];
  reportArray = [];
  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'report_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'report_alias': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(32)])],
      'res_id': [null],
      'report_sql': [null],
      'report_param': [null],
      'report_suffix': [null],
      'index_flag': [null],
      'report_type': [null],
      'with_thread': [null],
    });
  }

  ngOnInit() {
    this.resArray = FrameResDetailComponent.makeResArray(this.ResponseArray);
    this.requestDetail();
  }

  requestDetail() {
    if(this.ResponseEntity && this.ResponseEntity.report_name) {
      AjaxService.requestArray('frame/report/detail/list.htm', {report_name: this.ResponseEntity.report_name}, (array) => {
        this.detailArray = array;
      });
    }
  }

  deleteDetail(det) {
    const arr = [];
    if(this.detailArray && this.detailArray.length>0) {
      for(const detail of this.detailArray) {
        if(det.field_name !== detail.field_name ) {
          arr.push(detail);
        }
      }
    }
    this.detailArray = arr;
  }

  editReport() {
    this.reportArray.push(this.currentEntity);
    AjaxService.request('frame/report/edit.htm', {
      report: JSON.stringify(this.reportArray),
      json_value: JSON.stringify(this.detailArray)
    }, () => {
      MsgService.successToastr('修改完成！');
      this.closeWindow();
    });
  }

  fieldCollect() {
    let finalArr = [];
    const sql = this.currentEntity.report_sql;
    if(sql) {
      let trimSql = sql.replace(/\s+$/,'').replace(/^\s+/,'').toLowerCase();
     let sub = trimSql.substr(0,trimSql.indexOf('from'));
     let selectIndex = sub.indexOf('select');
     if(selectIndex > -1) {
      let result = sub.substr(selectIndex+6, sub.length).replace(/\s+$/,'').replace(/^\s+/,'');
      let arr = result.split(',');
      for(const a of arr) {
        let element = a.split(' ');
        finalArr.push(element[element.length-1].trim());
      }
     } else {
       MsgService.errorToastr('sql语句无法识别！');
       return;
     }
    }
    if(this.detailArray && this.detailArray.length>0) {
      MsgService.confirmAlert('识别字段会清空下面的报表字段，确认要清空吗？', () => {
        this.pushDetail(finalArr);
      });
    } else {
      this.pushDetail(finalArr);
    }
  }


  pushDetail(result) {
      this.detailArray = [];
      for(let i=0;i<result.length;i++) {
        this.detailArray.push({
          'report_name': this.currentEntity.report_name,
          'field_name': result[i],
          'field_order': i+1,
          'field_text': result[i],
          'field_date_type': 'java.lang.String',
          'field_length':10
        }
        );
      }
    }


  addDetail() {
    if(!this.currentEntity.report_name) {
      MsgService.warningToastr('请先填写报表编号！');
      return;
    }
    const detail = {report_name: this.currentEntity.report_name};
    this.detailArray.push(detail);
  }

  addReport() {
    if(!this.detailArray || this.detailArray.length == 0) {
      MsgService.warningToastr('请至少填写一条报表字段！');
      return;
    }
    this.reportArray.push(this.currentEntity);
    AjaxService.request('frame/report/add.htm', {
      report: JSON.stringify(this.reportArray),
      json_value: JSON.stringify(this.detailArray)
    }, () => {
      MsgService.successToastr('新增成功！');
      this.closeWindow();
    });
  }

}
