import React, { useState, useEffect } from 'react';
import { Form, Collapse, Radio, Switch, InputNumber } from 'antd';
import { findValuesByNamePath } from '../../../../utils';
import Colors from '../../../../basic-editor/colors';
import './index.less';

const { Panel } = Collapse;
const { Item } = Form;
const typeList = [
  { key: 'hidden', name: '隐藏' },
  { key: 'type1', name: '竖琴' },
  { key: 'type2', name: '波点' },
  { key: 'type3', name: '圆点' },
];
export default ({ propKey, node, config, onChangePathValue }) => {
  const { name } = config;
  const [state, setState] = useState();
  const [advSetting, setAdvSetting] = useState(false);

  const changeState = param => {
    setState({ ...state, ...param });
  };
  const handleChange = ({ key, value }) => {
    changeState({ [key]: value });
    onChangePathValue({ path: key, value });
  };

  useEffect(() => {
    const {
      type,
      bottom,
      backgroundColor,
      backgroundColor1,
    } = findValuesByNamePath(name, node[propKey]);
    setState({
      [name.type]: type,
      [name.bottom]: bottom,
      [name.backgroundColor]: backgroundColor,
      [name.backgroundColor1]: backgroundColor1,
    });
  }, [node[propKey]]);

  if (!state) {
    return null;
  }

  return (
    <Collapse style={{ width: '100%' }}>
      <Panel header="轮播点设置">
        <Item
          label="类型"
          labelCol={{ xs: { span: 24 }, sm: { span: 6 } }}
          style={{ marginBottom: '10px' }}
        >
          <Radio.Group
            size="small"
            value={state[name.type]}
            onChange={e => {
              handleChange({ key: name.type, value: e.target.value });
            }}
          >
            {typeList.map(item => {
              return <Radio.Button value={item.key}>{item.name}</Radio.Button>;
            })}
          </Radio.Group>
        </Item>
        <Item
          label="位置"
          labelCol={{ xs: { span: 24 }, sm: { span: 6 } }}
          style={{ marginBottom: '10px' }}
        >
          <InputNumber
            value={state[name.bottom]}
            onChange={e => {
              handleChange({ key: name.bottom, value: e });
            }}
          />
        </Item>
        <Item
          label="高级设置"
          labelCol={{ xs: { span: 24 }, sm: { span: 6 } }}
          style={{ marginBottom: '10px' }}
        >
          <Switch checked={advSetting} onChange={e => setAdvSetting(e)} />
        </Item>
        {advSetting && (
          <>
            <div className="cell">
              <div className="cell-row__title">未激活样式设置</div>
              <Item
                label="背景色"
                labelCol={{ xs: { span: 24 }, sm: { span: 6 } }}
                style={{ marginBottom: '10px' }}
              >
                <Colors
                  name={name.backgroundColor}
                  value={state[name.backgroundColor]}
                  onChangePathValue={onChangePathValue}
                />
              </Item>
            </div>
            <div className="cell">
              <div className="cell-row__title">激活样式设置</div>
              <Item
                label="背景色"
                labelCol={{ xs: { span: 24 }, sm: { span: 6 } }}
                style={{ marginBottom: '10px' }}
              >
                <Colors
                  name={name.backgroundColor1}
                  value={state[name.backgroundColor1]}
                  onChangePathValue={onChangePathValue}
                />
              </Item>
            </div>
          </>
        )}
      </Panel>
    </Collapse>
  );
};
