<template>
  <view class="form-video">
    <view v-if="videoUrl" class="video-preview">
      <video :src="videoUrl" class="preview-video" controls />
      <view class="delete-btn" @click="deleteVideo">
        <text class="delete-icon">&times;</text>
      </view>
    </view>
    <view v-else class="upload-btn" @click="chooseVideo">
      <text class="plus-icon">+</text>
    </view>
  </view>
</template>

<script>
export default {
  name: 'FormItemVideo',
  props: {
    value: { type: String, default: '' }
  },
  data: function () {
    return {
      videoUrl: ''
    };
  },
  watch: {
    value: {
      handler: function (val) {
        this.videoUrl = val || '';
      },
      immediate: true
    }
  },
  methods: {
    chooseVideo: function () {
      var self = this;
      uni.chooseVideo({
        sourceType: ['album', 'camera'],
        maxDuration: 60,
        success: function (res) {
          self.videoUrl = res.tempFilePath;
          self.$emit('input', self.videoUrl);
        }
      });
    },
    deleteVideo: function () {
      this.videoUrl = '';
      this.$emit('input', '');
    }
  }
};
</script>

<style scoped>
.form-video {
  width: 100%;
}

.video-preview {
  width: 200rpx;
  height: 200rpx;
  position: relative;
}

.preview-video {
  width: 200rpx;
  height: 200rpx;
  border-radius: 8rpx;
}

.delete-btn {
  position: absolute;
  top: -20rpx;
  right: -20rpx;
  width: 40rpx;
  height: 40rpx;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.delete-icon {
  color: #ffffff;
  font-size: 32rpx;
}

.upload-btn {
  width: 200rpx;
  height: 200rpx;
  background-color: #ffffff;
  border: 2rpx dashed #dddddd;
  border-radius: 8rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.plus-icon {
  font-size: 60rpx;
  color: #999999;
}
</style>
