<template>
    <div class="option" :class="[option.type, { length: option.list.length }]">
        <div class="item-box" v-for="(item, index) in option.list" :key="index">
            <div class="item" @click="select(item.value)">
                <template v-if="option.type === 'row'">
                    <img :src="getFileUrl(item.img)" @click.stop="$refs.previewer.show(index)">
                    <el-radio :value="value" :label="item.value" v-if="radio">{{ item.name }}</el-radio>
                    <el-checkbox :value="value" :label="item.value" v-if="checkbox">{{ item.name }}</el-checkbox>
                    <div class="describe" v-if="item.describe">{{ item.describe }}</div>
                </template>
                <template v-else>
                    <el-radio :value="value" :label="item.value" v-if="radio"></el-radio>
                    <el-checkbox :value="value" :label="item.value" v-if="checkbox"></el-checkbox>
                    <img :src="getFileUrl(item.img)" @click.stop="$refs.previewer.show(index)">
                    <div class="text">
                        <div>{{ item.name }}</div>
                        <div class="describe" v-if="item.describe">{{ item.describe }}</div>
                    </div>
                </template>
            </div>
        </div>
        <div v-transfer-dom>
            <previewer :list="previewerList" ref="previewer"></previewer>
        </div>
    </div>
</template>

<script>
import { TransferDom, Previewer } from 'vux'
import { getFileUrl } from '@/assets/utils/host.js'
export default {
    model: { prop: 'value', event: 'change' },
    directives: { TransferDom },
    components: {
        Previewer,
        ElRadio: () => import('./ElRadio'),
        ElCheckbox: () => import('./ElCheckbox')
    },
    props: {
        value: [String, Number, Array],
        option: { type: Object, default: () => ({}) },
        radio: { type: Boolean, default: false },
        checkbox: { type: Boolean, default: false },
        min: { type: Number, default: 0 },
        max: { type: Number, default: 0 }
    },
    data () {
        return {
            getFileUrl,
            previewerList: this.option.list.map(item => ({ src: getFileUrl(item.img) }))
        }
    },
    methods: {
        select (value) {
            if (this.radio) {
                this.$emit('change', value)
            } else {
                const index = this.value.indexOf(value)
                if (index > -1) {
                    this.value.splice(index, 1)
                    this.$emit('change', this.value)
                } else if (!this.max || this.value.length < this.max) {
                    this.value.push(value)
                    this.$emit('change', this.value)
                }
            }
        }
    }
}
</script>

<style lang="less" scoped>
.option {
    display: flex;
    flex-wrap: wrap;
    word-break: break-all;
    line-height: 20px;
    margin-top: 3px;
    &.column {
        flex-direction: column;
    }
}
.item {
    display: flex;
    background-color: #fff;
    border: 1px solid #DCDFE6;
    img {
        vertical-align: top;
        object-fit: contain;
        background-color: #f5f5f5;
    }
    .el-radio, .el-checkbox {
        margin-right: 0;
        white-space: normal;
    }
    .describe {
        font-size: 14px;
        color: #909399;
    }
}
.row {
    &.length {
        margin: -2px -5px -5px;
    }
    .item-box {
        width: 50%;
        padding: 5px;
        box-sizing: border-box;
    }
    .item {
        flex-direction: column;
        position: relative;
        padding: 90px 10px 10px;
        img {
            width: 100%;
            height: 80px;
            position: absolute;
            top: 0;
            left: 0;
        }
        .text {
            padding: 5px;
        }
        .describe {
            margin-top: 0;
        }
    }
}
.column {
    .item-box + .item-box {
        margin-top: 5px;
    }
    .item {
        padding: 5px 10px;
        align-items: center;
    }
    .text {
        margin-left: 10px;
    }
    img {
        width: 32px;
        height: 32px;
    }
    .text {
        display: flex;
        flex-direction: column;
        justify-content: center;
        .describe {
            padding-left: 0;
            margin-top: 2px;
        }
    }
}
</style>
