<template>
    <van-radio-group class="app-mobile-radio-list" v-model="curValue" :disabled="disabled" direction="horizontal" @change="change">
        <van-radio v-for="(item,index) in options" :key="index" :name="item.value">{{item.text}}</van-radio>
    </van-radio-group>
</template>

<script lang="ts">
import { Vue, Component, Prop } from "vue-property-decorator";
import { CodeListService } from "@/ibiz-core";

@Component({
    components: {}
})
export default class AppMobRadio extends Vue {

    /**
     * 代码表服务对象
     *
     * @type {CodeListService}
     * @memberof AppMobRadio
     */
    public codeListService: CodeListService = new CodeListService();

    /**
     * 代码表标识
     *
     * @type {string}
     * @memberof AppMobRadio
     */
    @Prop() public tag!: string;

    /**
     * 代码表类型
     *
     * @type {string}
     * @memberof AppMobRadio
     */
    @Prop() public type!: string;

    /**
     * 代码表列表项
     *
     * @type {Array<any>}
     * @memberof AppMobRadio
     */
    public options?: Array<any> = [];

    /**
     * 输入值
     *
     * @type {any}
     * @memberof AppMobRadio
     */
    @Prop() public value?: any;

    /**
     * 输入值变化后的值
     *
     * @type {any}
     * @memberof AppMobRadio
     */
    public curValue?: any = this.value;

    /**
     * 禁用
     *
     * @type {boolean}
     * @memberof AppMobRadio
     */
    @Prop() public disabled?: boolean;

    /**
     * change事件
     *
     * @param {*} data
     * @memberof AppMobRadio
     */
    public change(data: any) {
        this.$emit("change", this.curValue);
    }

    /**
     *  vue 生命周期
     *
     * @returns
     * @memberof AppMobRadio
     */
    public created() {
        if (!this.tag || !this.type) {
            return;
        }
        this.loadItems();
    }

    /**
     * 加载 数据
     *
     * @private
     * @returns {Promise<any>}
     * @memberof AppMobRadio
     */
    private async loadItems(): Promise<any> {
        if (Object.is(this.type, 'dynamic')) {
            const response: any = await this.codeListService.getItems(this.tag);
            if (response) {
                this.options = response;
            } else {
                this.options = [];
            }
        } else {
            this.options = this.$store.getters.getCodeListItems(this.tag);
        }
    }
}
</script>

<style lang="less">
    .van-radio-group--horizontal{
        justify-content: flex-end;
    }
    .van-radio{
        margin:5px;
    }
</style>