import React, { useState, useEffect } from 'react';
import { Form, Input, Switch } from 'antd';
import cs from 'classnames';
import { findValuesByNamePath } from '../../utils';
import { PREFIX_CLS } from '../../config';
import './index.less';

const prefixCls = `${PREFIX_CLS}-href-editor`;

export default ({ node, propKey, config, onChangePathValue }) => {
  const { label, name } = config;
  const [state, setState] = useState({});
  const [more, setMore] = useState(false);

  const monitor = node[propKey];

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

  const handleTargetChange = e => {
    const value = e ? '_blank' : '_self';
    setState({
      ...state,
      [name.target]: value,
    });
    onChangePathValue({ path: name.target, value });
  };

  // const handleMoreClick = e => {
  //   setMore(e);
  // };

  useEffect(() => {
    const { href, target = '_blank', wxHref, appHref } = findValuesByNamePath(
      name,
      monitor,
    );
    setState({
      [name.target]: target,
      [name.href]: href,
      [name.wxHref]: wxHref,
      [name.appHref]: appHref,
    });
    if (wxHref || appHref) {
      setMore(true);
    } else {
      setMore(false);
    }
  }, [monitor]);

  return (
    <Form.Item label={label}>
      <div
        className={cs(
          `${prefixCls}-wrapper`,
          more ? `${prefixCls}-wrapper--active` : '',
        )}
      >
        <div className={prefixCls}>
          <Input
            className={`${prefixCls}__input`}
            value={state[name.href]}
            onChange={e => {
              handleChange({ path: name.href, value: e.target.value });
            }}
          />
          <Switch
            checkedChildren="新窗口"
            unCheckedChildren="本窗口"
            checked={state[name.target] === '_blank'}
            onChange={handleTargetChange}
          />
        </div>
        {/* <Row className={`${prefixCls}-row`}>
          <Col flex="50px">高级</Col>
          <Col flex="auto">
            <Switch
              checkedChildren="开启"
              unCheckedChildren="关闭"
              checked={more}
              onChange={handleMoreClick}
            />
          </Col>
        </Row>
        <div
          className={cs(
            `${prefixCls}-more`,
            more ? `${prefixCls}-more--active` : '',
          )}
        >
          <Row className={`${prefixCls}-row`}>
            <Col flex="50px">小程序</Col>
            <Col flex="auto">
              <Input
                className={`${prefixCls}__input`}
                value={state[name.wxHref]}
                onChange={e => {
                  handleChange({ path: name.wxHref, value: e.target.value });
                }}
              />
            </Col>
          </Row>
          <Row className={`${prefixCls}-row`}>
            <Col flex="50px">app</Col>
            <Col flex="auto">
              <Input
                className={`${prefixCls}__input`}
                value={state[name.appHref]}
                onChange={e => {
                  handleChange({ path: name.appHref, value: e.target.value });
                }}
              />
            </Col>
          </Row>
        </div>
       */}
      </div>
    </Form.Item>
  );
};
