//公用饼图
import EmptySpace from '@/components/EmptySpace';
import { Flex } from 'antd';
import classNames from 'classnames';
import { CSSProperties } from 'react';
import Chart from './Chart';
import styles from './index.less';
interface PieChartDto {
  data?: API.BaseChartInfo[]; //数据源
  labelGap?: number; //label间距
  style?: CSSProperties; //样式
  chartStyle?: CSSProperties; //chartStyle样式
  labelClassName?: string; //label样式
  color?: string[]; //颜色
}
const PieChart: React.FC<PieChartDto> = ({
  style,
  labelGap = 8,
  chartStyle = { width: '260px', height: '300px' },
  data = [],
  labelClassName,
  color = ['#FF9B5D', '#FFE335', '#29B5FF'],
}) => {
  return (
    <Flex style={{ ...style }} align="center" gap={20}>
      {data.length > 0 ? (
        <>
          <Chart data={data} color={color} style={{ ...chartStyle }} />
          <div className={styles.label} style={{ gap: labelGap }}>
            {data.map((it, index) => (
              <Flex
                align="center"
                justify="space-between"
                key={it.name}
                className={classNames(styles.labelItem, labelClassName)}
              >
                <div className={styles.dot} style={{ backgroundColor: color[index] }}></div>
                <div>{it.name}</div>
                <p className={styles.valueWrap}>
                  <b>{it.value}</b>
                  <span>件</span>
                </p>
                <p>
                  <b>{parseFloat(Number(it.percent).toFixed(1))}</b>%
                </p>
              </Flex>
            ))}
          </div>
        </>
      ) : (
        <EmptySpace describe="暂无数据" style={{ height: '160px' }} />
      )}
    </Flex>
  );
};
export default PieChart;
