<template>
    <template v-if="form.img">
        <div class="flex1" style="overflow: auto;">
            <TelViewTem @selectArea="selectArea" :enableDraw="enableDraw" bgColor="#f7f7f7" :bgSrc="form.img">
                <DragResizeRotate v-if="form.bgm" v-model="form.bgmRact" @deactivated="deactivated"></DragResizeRotate>
                <template v-for="(item, index) in form?.events" :key="index">
                    <DragResizeRotate v-model="form.events[index]" @delete="deleteItemEvents($event, index, item)" @activated="activated($event, index)" @deactivated="deactivated"></DragResizeRotate>
                </template>
            </TelViewTem>
        </div>
    </template>
    <template v-else>
        <div @click="addImgBg" class="bg-#f7f7f7 flex1 w-750 mb-10 c-s-p d-flex a-c j-c">
            <div class="c-666 f-s-18 u-s-n">点击此处上传图片</div>
        </div>
    </template>
</template>
<script setup lang="ts">
import { importFileGetUrls } from '@/utils/models';
const emit = defineEmits<{
    (e: 'update:modelValue', value: any): void,
    (e: 'activated', value: any): void
    (e: 'deactivated'): void
    (e: 'deleteItemEvents', value: any): void
}>();
const props = defineProps<{
    modelValue: any
}>();
const eventDefault = {
    id: '',
    eventName: '',
    params: {
        phone: '',
        url: '',
    },
    x: 0,
    y: 0,
    w: 100,
    h: 100,
};
const form = ref<any>(props.modelValue || { img: '', events: [] });
const enableDraw = ref(true);

const selectArea = (area: any) => {
    if (area.rect.w < 20 || area.rect.h < 20) {
        return;
    }
    form.value.events.push({ ...eventDefault, id: new Date().getTime() + '_btn_drap', ...area.rect });
}
const activated = (event, index) => {
    enableDraw.value = false;
    emit('activated', form.value.events[index] || { id: '' });
}
const deactivated = () => {
    enableDraw.value = true;
    emit('deactivated');
}
const deleteItemEvents = (event: any, index: number, item: any) => {
    form.value.events.splice(index, 1);
    emit('deleteItemEvents', item);
    emit('update:modelValue', form.value);
}
const clickInfo = () => {
    console.log(form.value);
}
const addImgBg = async () => {
    const res: any[] = await importFileGetUrls(['png', 'jpg', 'jpeg'], false);
    if (res.length) {
        res.forEach(element => {
            if (element.data?.url) {
                form.value.img = element.data.url;
            }
        });
    }
}
watch(() => props.modelValue, (val) => {
    form.value = val || { img: '', events: [] };
}, { deep: true });
</script>
<style scoped lang="scss"></style>
