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

declare var $: any;
const summernoteConf = {
  placeholder: '请输入',
  tabsize: 2,
  height: 200,
  widhth: 500,
  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']]
  ],
  popover: {
    image: [['resize', ['resizeFull', 'resizeHalf', 'resizeQuarter', 'resizeNone']],
            ['float', ['floatLeft', 'floatRight', 'floatNone']],
            ['remove', ['removeMedia']]],
    link: [],
    air: []
  },
  callbacks: {
    onImageUpload: function(files, editor, welEditable) {
      debugger;
      for (let i = 0; i < files.length; i++) {
        let out = new FormData();
        out.append('file', files[i], files[i].name);
        $.ajax({
          data: out,
          type: "POST",
          url: 'cf/material/def/image/upload.htm',
          //请求路径
          cache: false,
          contentType: false,
          processData: false,
          dataType: "json",
          success: function (data) {
            debugger;
            $('#contentDiv_sn').summernote('insertImage',data.entity);  
          },error:
              function (url) {
                  alert(JSON.stringify(url.message))
              }
        });
      }
    },
    onEnter: function() {
      debugger;
      alert('Enter/Return key pressed');
    }
  }
}
@Component({
  selector: 'app-cf-promise-detail',
  templateUrl: './promise-detail.component.html',
  styleUrls: ['./promise-detail.component.scss']
})
export class CfPromiseDetailComponent extends FrameDetailComponent implements OnInit {
  schoolArray = [];
  categoryArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'count_down_secs': [null,Validators.compose([Validators.required])],
      'biz_type': [null,Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
    AjaxService.requestArray("cf/school/list.htm",{},(array)=>{
      this.schoolArray = array;
      if (this.schoolArray.length == 1) {
        this.currentEntity.school_id = this.schoolArray[0].school_id;
      }
    }); 
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.count_down_secs = 30;//默认30秒
      setTimeout(() => {
        $('#contentDiv_sn').summernote(summernoteConf);
      }, 15);
    } else {
      setTimeout(() => {
        $('#contentDiv_sn').summernote(summernoteConf);
        $('#contentDiv_sn').summernote('code', this.currentEntity.promise_text); 
      }, 15);  
    }
  }

  beforeSubmitForm() {
    debugger;
    this.currentEntity.promise_text = $('#contentDiv_sn').summernote('code');;
    return true;
  }

}
