import Taro, { Component } from '@tarojs/taro'

import './index.scss'

export default class Star extends Component {
  static defaultProps = {
    max: 5,
    value: 0
  }

  render() {
    const { value, max } = this.props
    let _value = parseFloat(value).toFixed(1)
    const classNameArr = []
    const floorValue = Math.floor(_value)
    const ceilValue = Math.ceil(_value)
    for (let i = 0; i < max; i++) {
      if (floorValue > i) {
        classNameArr.push('icon-StarFilled')
      } else if (ceilValue - 1 === i) {
        classNameArr.push('icon-starhalfempty')
      } else {
        classNameArr.push('icon-pingfen')
      }
    }
    return (
      <View className="star">
        {classNameArr.map((cls,index) => <Text className={`star__icon iconfont ${cls}`} key={index} ></Text>)}
        <Text className="star__txt">{_value}</Text>
      </View>
    )
  }
}
