<template>
    <div>
        <div @click="visibleFun">
            <slot>
                <div class="comoros-input">
                    <div v-if="!Object.keys(data).length" class="comoros-input-empty">
                        <div>选择云手机</div>
                        <img src="@/assets/image/transferDuration/arrow-right.png" alt="" class="comoros-input-empty-img" />
                    </div>
                    <div v-else>
                        <div class="comoros-list-item_left" style="margin: 0 12px;">
                            <img :src="require(`@/assets/image/transferDuration/${data.buyVipType}_icon.png`)" alt=""
                                class="comoros-list-item_left-img" />
                            <div>
                                <div class="comoros-list-item_left-title">{{ data.diskName }}</div>
                                <div v-if="data.userCardType === 1" class="comoros-list-item_left-tips">
                                    剩{{ timeStamp(data.validTime, item.userCardType) }}
                                </div>
                                <div v-else-if="data.validTime > 0"
                                    :class="['comoros-list-item_left-tips', { 'red-time': item.validTime <= 4320 }]">
                                    剩{{ timeStamp(data.validTime, data.userCardType) }}</div>
                                <div v-else class="comoros-list-item_left-tips red-time">已过期</div>
                            </div>
                        </div>
                    </div>
                </div>
            </slot>
        </div>
        <van-popup v-model="visible" position="bottom">
            <div class="comoros-list">
                <div style="display: flex;justify-content: space-between;margin-bottom: 16px;">
                    <div>请选择云手机</div>
                </div>
                <div class="comoros-list-container">
                    <van-checkbox-group v-model="id" ref="checkboxGroup" v-if="filterTransferPhoneList.length">
                        <div v-for="item in filterTransferPhoneList" :key="item.id" class="comoros-list-item">
                            <div class="comoros-list-item_left">
                                <img :src="require(`@/assets/image/transferDuration/${item.buyVipType}_icon.png`)" alt=""
                                    class="comoros-list-item_left-img" />
                                <div>
                                    <div class="comoros-list-item_left-title">{{ item.diskName }}</div>
                                    <div v-if="item.userCardType === 1" class="comoros-list-item_left-tips">
                                        剩{{ timeStamp(item.validTime, item.userCardType) }}
                                    </div>
                                    <div v-else-if="item.validTime > 0"
                                        :class="['comoros-list-item_left-tips', { 'red-time': item.validTime <= 4320 }]">
                                        剩{{ timeStamp(item.validTime, item.userCardType) }}</div>
                                    <div v-else class="comoros-list-item_left-tips red-time">已过期</div>
                                </div>
                            </div>
                            <div class="comoros-list-item_right"><van-checkbox :name="item.id" shape="square"
                                    icon-size="15px" @click="changeCheckbox(item)"></van-checkbox></div>
                        </div>
                    </van-checkbox-group>
                    <div v-else style="height: 100%;display: flex;align-self: center;justify-content: center;">
                        <van-empty description="暂时没有数据哦~"></van-empty>
                    </div>
                </div>
                <div class="comoros-list-btn">
                    <van-button type="info" block :disabled="!id.length" @click="confirm">确认</van-button>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<script>
export default {
    name: 'comoros',
    props: {
        // 云机数据
        transferPhoneList: {
            type: Array,
            default: () => {
                return []
            }
        },
        // 已选择的云机类型
        buyVipType: {
            type: String,
            default: ''
        },

        // 过滤类型
        filterType: {
            type: String,
            default: ''
        },

        value: {
            type: [String, Number],
            default: ''
        },

        // 不能勾选需要过滤的云机
        disabledId: {
            type: [String, Number],
            default: ''
        },
        // 云机名字
        name: {
            type: String,
            default: ''
        },
        // distinguishBool  false是减少、true是增加时长云机列表
        distinguishBool: {
            type: Boolean,
            default: false
        },
        dayTime: {
            type: [String, Number],
            default: ''
        }
    },
    data() {
        return {
            // 显示弹窗
            visible: false,
            // 选中的数据
            id: [],
            // vip类型
            vipType: '',
            data: {},
            item: {}
        };
    },
    computed: {
        // 过滤云机
        filterTransferPhoneList() {
            return this.transferPhoneList.filter(item => {
                if (this.distinguishBool) {
                    return this.filterType ? this.disabledId !== item.id : true
                } else {
                    return this.dayTime ? item.validTime > +this.dayTime : true
                }
            })
        }
    },
    methods: {
        // 点击显示弹框
        visibleFun() {
            this.visible = true
            this.id = this.value ? [this.value] : []
            this.vipType = this.buyVipType
            setTimeout(() => {
                const documents = document.getElementsByClassName('comoros-list-container')
                const length = documents.length
                for (let i = 0; i < length; i++) {
                    documents[i].scrollTop = 0
                }
            })
        },
        // 转化时长
        timeStamp(StatusMinute, userCardType, nextSendTime) {
            var day = parseInt(StatusMinute / 60 / 24);
            var hour = parseInt((StatusMinute / 60) % 24);
            var min = parseInt(StatusMinute % 60);
            StatusMinute = '';
            if (day > 0) {
                StatusMinute = day + '天';
            }
            if (hour > 0) {
                StatusMinute += hour + '小时';
            }
            if (day === 0 && min > 0) {
                StatusMinute += parseFloat(min) + '分钟';
            }
            return StatusMinute.length ? StatusMinute : userCardType === 1 ? '0小时' : '';
        },
        // 点击复选框触发
        changeCheckbox(data) {
            if (!this.id.length) return
            if (this.id.includes(data.id)) {
                this.id = []
            }
            this.id = [data.id]
            this.vipType = data.buyVipType
            this.item = data
        },

        // 确认选择云机
        confirm() {
            this.visible = false
            this.$emit('input', this.id[0])
            this.$emit('update:buyVipType', this.vipType)
            this.data = this.item
            this.$emit('update:name', this.item.diskName)
            this.$emit('confirm')
        }
    },
};
</script>

<style lang="scss" scoped>
.comoros-input {
    margin-top: 8px;
    height: 52px;
    background: #2C2C2D;
    border-radius: 8px;
    // line-height: 52px;
    display: flex;
    align-items: center;
    font-size: 12px;

    .comoros-input-empty {
        width: 100%;
        margin: 0 12px;
        display: flex;
        justify-content: space-between;

        .comoros-input-empty-img {
            width: 16px;
            height: 16px;
        }
    }


}

.comoros-list {
    background: #fff;
    margin: 0 10px;
    background: #2C2C2D;
    border-radius: 20px 20px 0 0;
    padding: 24px 16px 8px;
    color: #CFD1D4;
    height: 60vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;

    ::v-deep .van-checkbox__label {
        color: #CFD1D4;
    }

    .comoros-list-container {
        flex: 1;
        overflow-y: auto;

        .comoros-list-item {
            box-sizing: border-box;
            padding: 16px 16px 16px 10px;
            height: 65px;
            background: #363636;
            border-radius: 6px;
            margin-top: 12px;
            display: flex;
            justify-content: space-between;

            .comoros-list-item_right {
                display: flex;
                align-items: center;
            }



        }
    }

    .comoros-list-btn {
        margin: 16px 0 9px;
    }


}


.comoros-list-item_left {
    display: flex;
    vertical-align: middle;
    font-size: 14px;
    line-height: 18px;

    .comoros-list-item_left-img {
        width: 34px;
        height: 34px;
        vertical-align: middle;
        margin-right: 8px;
    }

    .comoros-list-item_left-tips {
        &>img {
            width: 12px;
            height: 12px;
            margin-bottom: 2px;
            margin-right: 5px;
            vertical-align: middle;
        }

        font-size: 12px;
        font-weight: 100;
        vertical-align: middle;

        &.red-time {
            color: #F04646;
        }
    }
}

::v-deep .van-popup {
    background: transparent;
}
</style>