<template>
  <view
    class="upload_content"
    :style="{
      borderTop: props.borderTop ? '1rpx solid #eee' : ''
    }"
  >
    <view class="id_card_title">上传身份证<text v-if="props?.required"> * </text></view>
    <view class="upload_body">
      <view>
        <view class="upload_item" @click="handleUpload(1)" :style="props?.styles">
          <image v-if="!frontImg?.url" class="icon" src="./IdCard1.png" mode="heightFix" />
          <image v-else class="showImg" :src="frontImg?.url" mode="heightFix" />
        </view>
        <view class="item_name">证件正面</view>
      </view>
      <view>
        <view class="upload_item" @click="handleUpload(2)" :style="props?.styles">
          <image v-if="!backImg?.url" class="icon" src="./IdCard2.png" mode="heightFix" />
          <image v-else class="showImg" :src="backImg?.url" mode="heightFix" />
        </view>
        <view class="item_name">证件反面</view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import watermark from '@/common/libraries/watermark'
import { upload } from '@/common/libraries/upload'
import { onMounted, ref, watch, defineProps, defineEmits } from 'vue'
const frontImg = ref<any>({})
const backImg = ref<any>({})
const props = defineProps([
  'valueFrontModel',
  'valueBackModel',
  'size',
  'display',
  'borderTop',
  'length',
  'required',
  'mediaType',
  'labelTitle',
  'count',
  'styles',
  'hasCover',
  'iconName',
  'iconSize'
])
const emit = defineEmits(['update:valueFrontModel', 'update:valueBackModel'])
const handleUpload = async (type: number) => {
  const res: any = await upload(1, 4, props?.mediaType || ['image'])
  console.log(res, 'res32')
  if (type === 1) {
    frontImg.value = res[0]
    emit('update:valueFrontModel', res)
  }
  if (type === 2) {
    backImg.value = res[0]
    emit('update:valueBackModel', res)
  }
}

const onPreviewMedia = (url?: string) => {
  console.log(url, 'url1')
  if (url) {
    uni.showLoading({
      title: '获取图片中...',
      mask: true
    })
    console.log(url, 'url2')
    uni.downloadFile({
      url: url,
      success: res => {
        const imagePath = res.tempFilePath // 下载成功的图片路径
        console.log(imagePath, 'imagePath')
        watermark.addWatermark(imagePath, '仅用于内部查看，请勿外传')
      },
      fail: err => {
        uni.showToast({
          title: '获取失败',
          duration: 1000
        })
        console.error('下载图片失败:', err)
      }
    })
  }
}
</script>
<style lang="scss" scoped>
.upload_content {
  padding: 0 0 10rpx 0;
  .id_card_title {
    padding: 30rpx 0 20rpx 0;
    font-size: 29rpx;
    color: #333;
    text {
      color: #f00;
    }
  }
}
.upload_body {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10rpx 0; // 可选，增加内边距以便更好地显示滚动条
  text-align: center;
  .no_data {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 120rpx;
    font-size: 25rpx;
    color: #666;
    background-color: #f8f8f8;
    border-radius: 6rpx;
  }
  .item_name {
    padding: 20rpx 0 0 0;
    font-size: 28rpx;
    color: #333;
  }
}
.upload_item {
  position: relative;
  flex-shrink: 0; // 防止子元素缩小
  display: flex;
  align-items: center;
  justify-content: center;
  width: 330rpx;
  height: 200rpx;
  background-color: #f3f3f3;
  border: 1px solid #f3f3f3;
  border-radius: 10rpx;
  overflow: hidden;
  image.icon {
    height: 134rpx;
  }
  image.showImg {
    width: 100%;
    height: 100%;
  }
}
</style>
