import {Component, OnInit} from '@angular/core';
import {FrameAbstractComponent} from "../../../frame/FrameAbstractComponent";
import {FlexTableComponent} from "../../../../shared/common/FlexTable.component";
import {BsModalService} from "ngx-bootstrap/modal";
import {FormBuilder, Validators} from "@angular/forms";
import { AppComponent } from "../../../../app.component";

declare var $: any;

const summernoteConf = {
  placeholder: '请输入内容',
  tabsize: 2,
  height: 180,
  widhth: 30,
  fontNames: ['宋体', '微软雅黑', '楷体', '黑体', '隶书', 'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New',
    'Helvetica Neue', 'Helvetica', 'Impact', 'Lucida Grande',
    'Tahoma', 'Times New Roman', 'Verdana'],
  toolbar: [
    ['font', ['fontname', 'fontsize', 'color', 'bold', 'italic', 'underline', 'strikethrough', 'clear']],
    ['para', ['style', 'ol', 'ul', 'paragraph', 'height']],
    ['insert', ['picture', 'link', 'video', 'table', 'hr']],
    ['view', ['fullscreen', 'codeview', 'undo', 'redo', 'help']]
  ]
}

@Component({
  selector: 'app-sys-app-news',
  templateUrl: './sys-app-news.component.html',
  styleUrls: ['./sys-app-news.component.scss']
})
export class SysAppNewsComponent extends FlexTableComponent implements OnInit {
  public query = {
    query: "",
  };

  public temp = {
    title: "",
    cover: "",
    id: "",
    author_name: "",
    published_at: "",
    content:"",
    more_url:"",
    topup:""
  };

  QUERY_URL = "sys/sysAppNews/page.htm";
  DELETE_URL = "sys/sysAppNews/delete.htm";
  GET_URL = "sys/sysAppNews/get.htm";
  ADD_URL = "sys/sysAppNews/add.htm";
  EDIT_URL = "sys/sysAppNews/edit.htm";

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      //content: [null, Validators.compose([Validators.required])],
      title: [null, Validators.compose([Validators.required])],
      cover: [null],
      id: [null],
      author_name: [null, Validators.compose([Validators.required])],
      published_at: [null, Validators.compose([Validators.required])],
      more_url: [null],
      topup: [null],
    });
  }

  ngOnInit() {
    
  }

  async preSaveEvent(): Promise<boolean> {
    this.item.content = $('#summernote').summernote('code');
    if(!this.item.content || this.item.content=="<p><br></p>"){
        AppComponent.alert({title:"信息提示",text:"请填写内容!"});
        return false;
    }

    return super.preSaveEvent();
  }

  async preGoEditEvent(entity?): Promise<boolean> {

    return super.preGoEditEvent(entity);
  }

  async aftGoEditEvent(entity?){
    if(!this.item||!this.item.id){
      this.item = {};
    }

    $('#summernote').summernote(summernoteConf);
    
    if (this.item && this.item.content){
      $('#summernote').summernote('code', this.item.content);
    }
    
  }

}

