<template>
    <view class="updata-img">
        <view class="updata-title-box">
            <view class="updata-title-title font-title ">{{ data.title }} <span style="color: #f56c6c;"
                    v-if="data.required">*</span> </view>
            <view class="updata-title-text font-text ">{{ data.text }}</view>
        </view>
        <!-- <view class="updata-img-box" @click="updataFile">
            <image :src="img" style="width: 117px;height: 75px;" />
        </view> -->
        <view class="upload-box">
            <view class="upload-img-item" v-for="(item, index) in fileList" :key="item.url">
                <view class="delete-icon" @click="deletePic(index)"><up-icon name="close-circle-fill" color="#f64a1f"
                        size="18"></up-icon></view>
                <img class="upload-img" :src="item.url" :alt="item.fileName" srcset="">
            </view>
            <img src="/static/img/upload.png" alt="" class="upload-img" @click="uploadClick('img')"
                v-if="fileList.length < 10">
        </view>
    </view>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue';
import { wxUploadFile } from '@/utils/wxRequest.js'

const props = defineProps({
    data: {
        type: Object,
        default: {
            title: '上传头像',
            text: '上传您的头像',
            img: "/static/img/updata-user-img.png"
        },
    },
    fileList: {
        type: String,
        default: ''

    }
});
const emit = defineEmits(['onSubmit']);


const fileList = ref([]);

// 删除图片
const deletePic = (index) => {
    fileList.value.splice(index, 1);
    getFile();
};
const uploadClick = async (type) => {
    const res = await wxUploadFile(type);
    fileList.value = [...fileList.value, ...res];
    getFile();
    console.log('xxxxres', res, fileList.value);
}

function getFile() {
    const img_urls = fileList.value.map(item => item.url).join(',');
    console.log('img_urls',img_urls);
    emit('onSubmit', { key: props.data.key, url: img_urls })
    return img_urls
}

function setFile(files){
    console.log('fileList=>files',files);

    const arr_file = files.split(',').map(item=> {
        return {url:item}
    })
    console.log('fileList',files.split(','));
    fileList.value = arr_file
}
watch(()=>props.fileList,()=>{
    setFile(props.fileList);
})

defineExpose({
    getFile,
    setFile
});
</script>

<style lang="scss" scoped>
.updata-img {
    border-radius: 8px;
    background: rgba(255, 255, 255, 1);
    // height: 100px;
    padding: 16px;

    display: flex;
    align-content: center;
    justify-content: space-between;
    flex-direction: column;

    .updata-title-box {
        display: flex;
        align-content: center;
        flex-direction: column;
        justify-content: space-evenly;
        margin-bottom: 12px;
    }
}

.upload-img {
    height: 68px;
    width: 68px;
    margin-right: 12px;
    margin-bottom: 12px;
}

.upload-box {
    display: flex;
    flex-wrap: wrap;

    .upload-img-item {
        position: relative;

        .delete-icon {
            position: absolute;
            top: -7px;
            right: 7px;
            z-index: 1;
        }
    }
}
</style>