import React, { useState, useEffect } from 'react';
import { FlowchartFormWrapper, MODELS, useXFlowApp } from '@antv/xflow';
import { Position, Size, ColorPicker, InputNumberField, InputField, SelectField } from '../fields';
import { PREFIX } from '../constants';
import { connect } from "umi";
import { UnityAction } from '@/utils/utils';
import { Button } from 'antd';
import { IProps } from '@/components/Flow/index';

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 app = useXFlowApp();
  const [meta, setMeta] = useState<IProps['meta']>(null);
  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]);

  useEffect(() => {
    MODELS.GRAPH_META.useValue(app.modelService).then((meta: IProps['meta']) => {
      setMeta(meta);
    });
  }, []);

  return (
    <div className={`${PREFIX}-panel-body`}>
      <div className={`${PREFIX}-panel-group`}>
        <h5>内容</h5>
        <InputField
          label="标题"
          value={nodeConfig.label}
          disabled={meta?.editMode == 2}
          onChange={value => {
            onNodeConfigChange('label', value);
          }}
        />
      </div>
      <div className={`${PREFIX}-panel-group`}>
        <h5>数据</h5>
        <SelectField
          label="审批流程"
          value={nodeConfig.flow_id}
          disabled={meta?.editMode == 2}
          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>

      {meta?.editMode != 2 && (
        <>
          <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);
              }}
            />
            <InputNumberField
              label="消息数量"
              value={nodeConfig.count}
              onChange={value => {
                onNodeConfigChange('count', value);
              }}
            />
          </div>

          <div className={`${PREFIX}-node-text-style`}>
            <InputNumberField
              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);
