<template>
    <label :class="wrapClasses">
        <span :class="radioClasses">
            <span :class="innerClasses"></span>
            <input
                type="radio"
                :class="inputClasses"
                :disabled="disabled"
                :checked="currentValue"
                :name="groupName"
                :for="groupName"
                @change="handleChange">
        </span>
        <slot>{{ label }}</slot>
    </label>
</template>
<script>
    import Emitter from '../../../mixins/emitter'
    import { findComponentUpward, oneOf } from '../../../utils/assist'

    const prefixCls = 'yy-radio'
    export default {
        name: 'YYRadio',
        mixins: [ Emitter ],
        props: {
            value: {
                type: [String, Number, Boolean],
                default: false
            },
            size: {
                validator (value) {
                    return oneOf(value, ['small', 'large'])
                }
            },
            trueValue: {
                type: [String, Number, Boolean],
                default: true
            },
            falseValue: {
                type: [String, Number, Boolean],
                default: false
            },
            label: {
                type: [String, Number]
            },
            disabled: {
                type: Boolean,
                default: false
            },
            name: {
                type: String
            }
        },
        data () {
            return {
                currentValue: this.value,
                group: false,
                groupName: this.name,
                parent: findComponentUpward(this, 'YYRadioGroup')
            }
        },
        computed: {
            wrapClasses () {
                return [
                    `${prefixCls}-wrapper`,
                    {
                        [`${prefixCls}-group-item`]: this.group,
                        [`${prefixCls}-wrapper-checked`]: this.currentValue,
                        [`${prefixCls}-wrapper-disabled`]: this.disabled,
                        [`${prefixCls}-wrapper-${this.size}`]: !!this.size
                    }
                ]
            },
            radioClasses () {
                return [
                    `${prefixCls}`,
                    {
                        [`${prefixCls}-checked`]: this.currentValue,
                        [`${prefixCls}-disabled`]: this.disabled
                    }
                ]
            },
            innerClasses () {
                return `${prefixCls}-inner`
            },
            inputClasses () {
                return `${prefixCls}-input`
            }
        },
        watch: {
            value (val) {
                if (val === this.trueValue || val === this.falseValue) {
                    this.updateValue()
                }
            }
        },
        methods: {
            updateValue () {
                this.currentValue = this.value === this.trueValue
            },
            handleChange (event) {
                if (this.disabled) return
                const checked = event.target.checked
                const value = checked ? this.trueValue : this.falseValue

                this.currentValue = checked
                this.$emit('input', value)

                if (!this.group) {
                    this.$emit('on-change', value)
                    return this.dispatch('YYFormItem', 'on-form-change', value)
                }
                if (this.label !== undefined) {
                    this.parent.handleChange({
                        value: this.label,
                        checked: this.value
                    })
                }
            }
        },
        mounted () {
            if (this.parent) {
                this.group = true
                if (!(this.name && this.name !== this.parent.name)) {
                    this.groupName = this.parent.name 
                }
            }

            if (this.group) {
                this.parent.updateValue()
            } else {
                this.updateValue()
            }
        }
    }
</script>
