import { Component, OnInit, ViewChild, Output, EventEmitter, Input, TemplateRef } from "@angular/core";
import { BsModalService } from "ngx-bootstrap/modal";
import { AppComponent } from "../..";
import { FlexTableComponent } from "./FlexTable.component";

@Component({
  selector: "FlexModal",
  templateUrl: './FlexTable.component.html'
})
export class FlexModalComponent extends FlexTableComponent implements OnInit {

  radioItem = {}

  @ViewChild('queryTemplateModal')
  queryTemplateModal: TemplateRef<any>

  @Input()
  selectType: string;// s 单选 m 多选

  @Input()
  doubleClickSelect: boolean //双击点击表格选择 关闭modal

  @Input()
  size: string// sm md lg

  @Output() 
  confirm = new EventEmitter(); //点击确定按钮事件

  @Output() 
  concel = new EventEmitter();//点击取消按钮事件
  
  constructor(
    public bsModalService: BsModalService,
  ) {
    super();
  }

  public show(){
    this.radioItem = {};
    let size = this.size ?`modal-${this.size}`:"";
    this.shouModalWin(this.queryTemplateModal,size);
  }

  public _confirm(){
    let length = this.framePager.getCheckedArray().length || this.radioItem['id']?1:0;
    if(length === 0){
      AppComponent.alert({title:"信息提示",text:"没有选择数据!"})
      return;
    }
    if(this.selectType == "s" || !this.selectType){
      if(length > 1){
        AppComponent.alert({title:"信息提示",text:"只允许选一条数据!"})
        return;
      }
    }
    
    if(this.confirm.observers.length > 0){
      this.confirm.emit(this);
    }else{
      this.hideModal()
    }
  }

  public _concel(){
    if(this.concel.observers.length > 0){
      this.concel.emit(this);
    }else{
      this.hideModal();
    }
  }

  public toggleCheck(item){
    if(this.isSelectTypeS)
      item.isChecked = !(item.isChecked)
    if(this.isSelectTypeM)
      this.radioItem = item;
  }

  public toogleRadio(item){
    this.radioItem = item;
  }

  public getCheckData(){
    let checkArray;
    if(this.isSelectTypeS()){
      checkArray = this.radioItem;
    }
    if(this.isSelectTypeM){
      checkArray = this.framePager.getCheckedArray() 
    }
    return checkArray;
  }

  public isSelectTypeS(){ return  this.selectType == "s" || !this.selectType}
  public isSelectTypeM(){ return  this.selectType == "m" }

  ngOnInit() {}
}
