import React, { useState, useEffect } from 'react';
import cs from 'classnames';
import Icon from '../../../../icon';
import { PREFIX_CLS } from '../../../../config';
import { findValuesByNamePath } from '../../../../utils';
import './index.less';

const prefixCls = `${PREFIX_CLS}-popbutton`;
const list = ['default', 'top-bar'];
export default ({ node, propKey, config, onChangePathValue = () => {} }) => {
  const { name } = config;
  const [, setCloseModel] = useState();

  const handleClick = type => {
    onChangePathValue({ path: name.closeModel, value: type });
  };

  const data = {
    style: {
      iconfont: {
        type: 'icon-guanbi',
        fontSize: 28,
        color: '#fff',
      },
    },
  };

  useEffect(() => {
    const { closeModel } = findValuesByNamePath(name, node[propKey]);
    setCloseModel(closeModel);
  }, [node[propKey]]);

  return (
    <div className={`${prefixCls}-close-modal-template`}>
      {list.map(item => {
        return (
          <div className={`${prefixCls}-close-modal-template-item`}>
            <div
              className={cs(
                `${prefixCls}__close`,
                `${prefixCls}__close--${item}`,
              )}
              onClick={() => {
                handleClick(item);
              }}
            >
              <Icon className={`${prefixCls}__close-icon`} data={data} />
            </div>
            <div className={`${prefixCls}-close-modal-template__name`}>
              {item}
            </div>
          </div>
        );
      })}
    </div>
  );
};
