import React, { useEffect, useState } from 'react';
import {
  ProForm,
  ProFormDigit,
  ProFormText
} from '@ant-design/pro-components';
import { Form, Modal } from 'antd';
import { useIntl } from '@umijs/max';

import FilesManager from '@/components/FilesManage/index';
import { ContentUtils } from 'braft-utils'
// 引入编辑器组件
import BraftEditor from 'braft-editor'
// 引入编辑器样式
import 'braft-editor/dist/index.css'

const agreementForm: React.FC = (props: any) => {

  const [editorState, setEditorState] = useState(BraftEditor.createEditorState(''))

  const [form] = Form.useForm();

  const { values } = props;

  useEffect(() => {
    setEditorState(BraftEditor.createEditorState(values.protocolContent ? values.protocolContent : ''))
    form.resetFields();
    form.setFieldsValue(values);
  }, [form, props]);

  const handleOk = () => {
    form.submit();
  };
  const handleCancel = () => {
    props.onCancel();
  };
  const handleFinish = async (values: any) => {
    let data = JSON.parse(JSON.stringify(values))
    data.protocolContent = editorState.toHTML()
    props.onSubmit(data);
  };


  const controls = [
    'undo', 'redo', 'separator',
    'font-size', 'line-height', 'letter-spacing', 'separator',
    'text-color', 'bold', 'italic', 'underline', 'strike-through', 'separator',
    'superscript', 'subscript', 'remove-styles', 'emoji', 'separator', 'text-indent', 'text-align', 'separator',
    'headings', 'list-ul', 'list-ol', 'blockquote', 'code', 'separator',
    'link', 'separator', 'hr', 'separator',
    'clear'
  ]

  const onChange = async (e) => {
    const videoExtensions = ['.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.webm', '.m4v', '.3gp', '.3g2'];
    // 将URL转换为小写，以便进行不区分大小写的比较
    const lowerCaseUrl = e.toLowerCase();
    // 检查链接是否以任何一个视频扩展名结尾
    if (videoExtensions.some(e => lowerCaseUrl.endsWith(e))) {
      setEditorState(ContentUtils.insertMedias(editorState, [{
        type: 'VIDEO',
        url: e
      }]));
    } else {
      setEditorState(ContentUtils.insertMedias(editorState, [{ type: 'IMAGE', url: e, },]))
    }
  }


  const extendControls = [
    'separator',
    {
      key: 'FilesManagerImage', // 控件唯一标识，必传
      title: '上传图片/视频', // 指定鼠标悬停提示文案
      html: null, // 指定在按钮中渲染的html字符串
      text: <FilesManager
        fileType="image"
        mode=""
        imagesShow={false}
        onChange={onChange}
        count={1}
      />, // 指定按钮文字，此处可传入jsx，若已指定html，则text不会显示
      onClick: () => {
        console.log('Hello World!');
      },
    }
  ]


  return (
    <Modal
      width={1200}
      title={'协议内容'}
      open={props.open}
      forceRender
      destroyOnClose
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <ProForm
        form={form}
        submitter={false}
        layout="horizontal"
        onFinish={handleFinish}>
        <ProFormDigit
          name="id"
          label={'ID'}
          disabled
          hidden={true}
        />
        <ProFormText
          width={300}
          name="protocolName"
          label="协议名称"
          placeholder="协议名称"
          rules={[{ required: true, message: '请输入' }]}
        />
        <ProForm.Group>
          <ProForm.Item
            name="protocolContent"
          >
            <div className="border-solid border-2 border-indigo-600">
              <BraftEditor value={editorState} controls={controls} extendControls={extendControls} onChange={(val) => {
                setEditorState(val)
              }} />
            </div>
          </ProForm.Item>
        </ProForm.Group>
      </ProForm>
    </Modal>
  );
};

export default agreementForm;
