<template>
    <view class="van-rate custom-class" @touchmove="onTouchMove">
        <view class="van-rate__item" :style="'padding-right: ' + (index !== count - 1 ? utils.addUnit(gutter) : '')" v-for="(item, index) in innerCountArray" :key="index">
            <van-icon
                :name="index + 1 <= innerValue ? icon : voidIcon"
                class="van-rate__icon"
                :style="'font-size: ' + utils.addUnit(size)"
                custom-class="icon-class"
                :data-score="index"
                :color="disabled ? disabledColor : index + 1 <= innerValue ? color : voidColor"
                @click="onSelect($event, { score: index })"
            />

            <van-icon
                v-if="allowHalf"
                :name="index + 0.5 <= innerValue ? icon : voidIcon"
                :class="utils.bem('rate__icon', ['half'])"
                :style="'font-size: ' + utils.addUnit(size)"
                custom-class="icon-class"
                :data-score="index - 0.5"
                :color="disabled ? disabledColor : index + 0.5 <= innerValue ? color : voidColor"
                @click="onSelect($event, { score: index - 0.5 })"
            />
        </view>
    </view>
</template>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/dist/wxs/utils.wxs"></script>
<script>
import { VantComponent } from '../common/component';
import { canIUseModel } from '../common/version';
export default {
    data() {
        return {
            innerValue: 0,

            innerCountArray: Array.from({
                length: 5
            }),

            valueClone: 0
        };
    },
    field: true,
    classes: ['icon-class'],
    props: {
        value: {
            type: Number
        },
        readonly: Boolean,
        disabled: Boolean,
        allowHalf: Boolean,
        size: null,
        icon: {
            type: String,
            default: 'star'
        },
        voidIcon: {
            type: String,
            default: 'star-o'
        },
        color: {
            type: String,
            default: '#ffd21e'
        },
        voidColor: {
            type: String,
            default: '#c7c7c7'
        },
        disabledColor: {
            type: String,
            default: '#bdbdbd'
        },
        count: {
            type: Number,
            default: 5
        },
        gutter: null,
        touchable: {
            type: Boolean,
            default: true
        }
    },
    methods: {
        onSelect(event, _dataset) {
            /* ---处理dataset begin--- */
            this.handleDataset(event, _dataset);
            /* ---处理dataset end--- */
            const { data } = this;
            const { score } = event.currentTarget.dataset;
            if (!data.disabled && !data.readonly) {
                this.setData({
                    innerValue: score + 1
                });
                if (canIUseModel()) {
                    this.setData({
                        valueClone: score + 1
                    });
                }
                this.$nextTick(() => {
                    this.$emit('input', score + 1);
                    this.$emit('change', score + 1);
                });
            }
        },
        onTouchMove(event) {
            const { touchable } = this;
            if (!touchable) {
                return;
            }
            const { clientX } = event.touches[0];
            this.getRect('.van-rate__icon', true).then((list) => {
                const target = list.sort((item) => item.right - item.left).find((item) => clientX >= item.left && clientX <= item.right);
                if (target != null) {
                    this.onSelect(
                        Object.assign(Object.assign({}, event), {
                            currentTarget: target
                        })
                    );
                }
            });
        }
    },
    watch: {
        value: {
            handler: function (value) {
                this.valueClone = this.clone(this.value);
                if (value !== this.innerValue) {
                    this.setData({
                        innerValue: value
                    });
                }
            },

            immediate: true
        },

        count: {
            handler: function (value) {
                this.setData({
                    innerCountArray: Array.from({
                        length: value
                    })
                });
            },

            immediate: true
        }
    }
};
</script>
<style>
@import './index.css';
</style>
