import {Component, OnInit, ViewChild} from '@angular/core';
import {FrameDetailComponent} from "../../core/detail/frame.detail";
import {FramePager} from "../../../../core/frame/pager/frame-pager/frame-pager";
import {BsModalRef} from "ngx-bootstrap/modal";
import {FormBuilder, Validators} from "@angular/forms";
import {MsgService} from "../../../../core/service/msg.service";
import {AjaxService} from "../../../../core/service/ajax.service";

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

  showDetail = false;
  dictMode: any;
  currentDict:any;
  @ViewChild('pager', {static: false}) pager: FramePager;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'dict_value': [null, Validators.compose([Validators.required, Validators.maxLength(32)])],
      'dict_text': [null, Validators.compose([Validators.required, Validators.maxLength(32)])],
      'dict_order': [null, Validators.required]
    });
    this.currentDict = {};
  }

  ngOnInit() {

  }

  saveDict() {
    if (!this.validForm()) {
      return;
    }
    this.currentDict['dict_name'] = this.ResponseEntity['dict_name'];

    let url = 'frame/dict/add.htm';
    let msg = '确定要增加此字典项吗？';
    if (this.dictMode === 'edit') {
      url = 'frame/dict/edit.htm';
      msg = '确定要修改字典项吗？';
    }

    MsgService.confirmAlert(msg, () => {
      AjaxService.request(url, this.currentDict, () => {
        MsgService.successToastr('字典保存成功！如需生效，请重新加载字典信息！');
        this.pager.reload();
        this.dictMode = undefined;
        this.showDetail = false;
        this.currentDict = {};
        this.detailForm.markAsUntouched();
      }, {RequestWithLoading: true});
    });

  }

}
