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-material-def-detail',
  templateUrl: './material-def-detail.component.html',
  styleUrls: ['./material-def-detail.component.scss']
})
export class CfMaterialDefDetailComponent extends FrameDetailComponent implements OnInit {
  schoolArray = [];
  categoryArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'school_id': [null,Validators.compose([Validators.required])],
      'category_id': [null,Validators.compose([Validators.required])],
      'material_name': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(64)])],
      'material_type': [null,Validators.compose([Validators.required])],
      'material_desc': [null],
      'remark': [null],
      'need_upload' : [null,Validators.compose([Validators.required])],
      'need_review' : [null,Validators.compose([Validators.required])],
      'min_size': [null],
      'max_size': [null],
      'min_upload_cnt' : [null,Validators.compose([Validators.required])],
      'max_upload_cnt' : [null,Validators.compose([Validators.required])],
      'ord': [null,Validators.compose([Validators.required])],
      'status': [null,Validators.compose([Validators.required])],
    });
  }

  ngOnInit() {
    if (this.detailMode === 'add') {

    } else {
      this.queryCategory();

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

  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.need_upload = '1';//默认上传
      this.currentEntity.status = '1';//默认启用
      this.currentEntity.need_review = '1';//默认启用
      setTimeout(() => {
        $('#contentDiv_sn').summernote(summernoteConf);
      }, 15);
    } else {
      this.currentEntity.need_upload = this.currentEntity.need_upload+'';
      this.currentEntity.need_review = this.currentEntity.need_review+'';
      this.currentEntity.status = this.currentEntity.status+'';
      setTimeout(() => {
        $('#contentDiv_sn').summernote(summernoteConf);
        $('#contentDiv_sn').summernote('code', this.currentEntity.material_desc);
      }, 15);
    }
  }

  changeSchool() {
    this.queryCategory();
  }

  queryCategory() {
    AjaxService.requestArray("cf/material/category/list.htm",{school_id: this.currentEntity.school_id},(array)=>{
      this.categoryArray = array;
    });
  }
  beforeSubmitForm() {
    debugger;
    this.currentEntity.material_desc = $('#contentDiv_sn').summernote('code');;
    return true;
  }

}
