import Icon from '../icon/icon.jsx';
import { Namespace } from '../../utils/namespace';

const namespace = Namespace.Create('radio');

export default {
    name: namespace.name,
    components: {
        Icon
    },
    props: {
        value: {
            type: Boolean,
            default: false
        },
        text: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        },
        theme: {
            type: String,
            default: 'brand'
        },
        icon:{
            type: String,
            default:'checked'
        },
        activeIcon:{
            type:String,
            default:'unchecked'
        },
        iconClassPrefix:{
            type: String,
            default:'mx-icon'
        },
        color: {
            type: String,
            default: '#1989fa'
        }
    },
    computed: {
        cls() {
            return namespace.derive([
                this.theme,
                {
                    disabled: this.disabled
                }
            ]);
        },
        contentCls() {
            return namespace.derive('content');
        }
    },
    model: {
        prop: 'value',
        event: 'change'
    },
    methods: {
        onClick() {
            if (this.disabled || this.$el.hasAttribute('disabled')) {
                return;
            }
            this.emitChange(!this.value);
        },
        emitChange(value) {
            this.$emit('change', value);
        }
    },
    render(){
        return (
            <div class={this.cls} vOn:click_stop={this.onClick}>
                <Icon name={this.value ? this.activeIcon: this.icon} class-prefix={this.iconClassPrefix} size="20" theme={this.theme} color={this.color} />
                {
                    this.$slots.default || (
                        this.text && <div class={this.contentCls}>{this.text}</div>
                    )
                }
            </div>
        )
    }
}