<template>
  <view class="form-video">
    <view v-if="videoUrl" class="video-preview">
      <video :src="videoUrl" class="video-player" controls />
      <view class="delete-btn" @click="deleteVideo">
        <text class="delete-icon">&times;</text>
      </view>
    </view>
    <view v-else class="video-add" @click="chooseVideo">
      <text class="add-icon">+</text>
      <text class="add-text">选择视频</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: 100%;
  position: relative;
}
.video-player {
  width: 100%;
  height: 400rpx;
  border-radius: 8rpx;
}
.delete-btn {
  position: absolute;
  top: 16rpx;
  right: 16rpx;
  width: 48rpx;
  height: 48rpx;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.delete-icon {
  color: #ffffff;
  font-size: 32rpx;
}
.video-add {
  width: 300rpx;
  height: 300rpx;
  background-color: #f5f5f5;
  border: 2rpx dashed #cccccc;
  border-radius: 8rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.add-icon {
  font-size: 60rpx;
  color: #999999;
}
.add-text {
  font-size: 24rpx;
  color: #999999;
  margin-top: 8rpx;
}
</style>
