import React, { useState, useEffect } from 'react';
import { FlowchartFormWrapper } from '@antv/xflow';
import { Position, Size, ColorPicker, InputNumberFiled, InputFiled, SelectField } from '../fields';
import { PREFIX } from '../constants';
import { connect } from 'dva';
import { UnityAction } from '@/utils/utils';
import { Button } from 'antd';

export interface IConfig {
  label?: string;
  x?: number;
  y?: number;
  width?: number;
  height?: number;
  count?: number;
  fontSize?: number;
  fontFill?: string;
  fill?: string;
  stroke?: string;
  flow_id?: string;
  flow_node_id?: string;
  process_code?: string;
}

const Component = (props: any) => {
  const { config, plugin = {}, auditList } = props;
  const { updateNode } = plugin;
  const [nodeConfig, setNodeConfig] = useState<IConfig>({
    ...config,
  });
  const onNodeConfigChange = (key: string, value: number | string | object) => {
    if (key) {
      setNodeConfig({
        ...nodeConfig,
        [key]: value,
      });
      updateNode({
        [key]: value,
      });
    } else if (value instanceof Object) {
      setNodeConfig({
        ...nodeConfig,
        ...value,
      });
      updateNode({
        ...value,
      });
    }
  };

  const onSave = () => {
    UnityAction.emit('NODE_SAVE', nodeConfig);
  };
  useEffect(() => {
    setNodeConfig({
      ...config,
    });
  }, [config]);

  return (
    <div className={`${PREFIX}-panel-body`}>
      <div className={`${PREFIX}-panel-group`}>
        <h5>内容</h5>
        <InputFiled
          label="标题"
          value={nodeConfig.label}
          onChange={value => {
            onNodeConfigChange('label', value);
          }}
        />
      </div>
      <div className={`${PREFIX}-panel-group`}>
        <h5>数据</h5>
        <SelectField
          label="审批流程"
          value={nodeConfig.flow_id}
          onChange={value => {
            let audit = auditList.find(item => item.list.id == value);

            onNodeConfigChange(null, {
              flow_node_id: audit.list.FlowNodes[0]?.id,
              flow_id: value,
              process_code: audit.list.process_code,
            });
          }}
          options={auditList.map(item => {
            const list = item.list;
            return {
              label: list.name || list.id,
              value: list.id,
            };
          })}
        />
      </div>
      <div className={`${PREFIX}-panel-group`}>
        <h5>样式</h5>
        <Position
          x={nodeConfig.x}
          y={nodeConfig.y}
          onChange={(key, value) => {
            onNodeConfigChange(key, value);
          }}
        />
        <Size
          width={nodeConfig.width}
          height={nodeConfig.height}
          onChange={(key, value) => {
            onNodeConfigChange(key, value);
          }}
        />
        <ColorPicker
          label="填充"
          value={nodeConfig.fill}
          onChange={(value: string) => {
            onNodeConfigChange('fill', value);
          }}
        />
        <ColorPicker
          label="边框"
          value={nodeConfig.stroke}
          onChange={(value: string) => {
            onNodeConfigChange('stroke', value);
          }}
        />
        <InputNumberFiled
          label="消息数量"
          value={nodeConfig.count}
          onChange={value => {
            onNodeConfigChange('count', value);
          }}
        />
      </div>

      <div className={`${PREFIX}-node-text-style`}>
        <InputNumberFiled
          label="字号"
          value={nodeConfig.fontSize}
          width={68}
          onChange={value => {
            onNodeConfigChange('fontSize', value);
          }}
        />
        <ColorPicker
          value={nodeConfig.fontFill}
          onChange={(value: string) => {
            onNodeConfigChange('fontFill', value);
          }}
        />
      </div>
      <Button style={{ marginTop: 20 }} type="primary" onClick={onSave}>
        保存
      </Button>
    </div>
  );
};

function RecthServe(props: any) {
  return (
    <FlowchartFormWrapper {...props}>
      {(config, plugin) => <Component {...props} plugin={plugin} config={config} />}
    </FlowchartFormWrapper>
  );
}

export default connect(({ xflow }) => ({ auditList: xflow.auditList }))(RecthServe);
