<template>
  <div class="camera-upload">
    <div class="row">
      <svg-icon icon-class="paizhao" size="80" @click="openMedia" />
      <svg-icon icon-class="bendishangchuan" size="80" @click="selectFile" />
    </div>
    <div class="row">
      <svg-icon icon-class="paizhao" size="80" @click="clickReset" />
      <svg-icon
        icon-class="bendishangchuan"
        size="80"
        @click="clickDeleteAll"
      />
    </div>

    <input v-show="false" id="fileInput" accept="image/*" type="file" />
  </div>

  <el-dialog
    id="carmer-dialog"
    v-model="visible"
    :close-on-click-modal="false"
    :title="null"
    close-icon="null"
  >
    <video id="video" autoplay="autoplay" height="500px" width="500px"></video>
    <canvas id="canvas" height="500px" width="500px"></canvas>
    <img id="imgTag" alt="imgTag" src="" />
    <div class="bottom-btns">
      <el-button class="cancelBtn" @click="closeMedia">关闭</el-button>
      <el-button class="sureBtn" type="primary" @click="takePhoto"
        >拍照
      </el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { uploadFileApi } from "@/api/file";

let mediaStreamTrack = null; // 视频对象(全局)
let video;

const cameraEmit = defineEmits(["uploadFinish", "resetSelect", "deleteAll"]);

const openMedia = async () => {
  visible.value = true;
  nextTick(() => {
    let constraints = {
      video: { width: 500, height: 500 },
      audio: false,
    };
    //获得video摄像头
    video = document.getElementById("video");
    navigator.mediaDevices
      .getUserMedia(constraints)
      .then((mediaStream) => {
        // mediaStreamTrack = typeof mediaStream.stop === 'function' ? mediaStream : mediaStream.getTracks()[1];
        mediaStreamTrack = mediaStream.getVideoTracks();
        video.srcObject = mediaStream;
        video.play();
      })
      .catch((error) => {
        console.log("Error: " + error);
        ElMessage.error("没有找到设备，或者没有权限");
      });
  });
};

function dataURItoBlob(base64Data) {
  var byteString;
  if (base64Data.split(",")[0].indexOf("base64") >= 0)
    byteString = atob(base64Data.split(",")[1]);
  else byteString = unescape(base64Data.split(",")[1]);
  var mimeString = base64Data.split(",")[0].split(":")[1].split(";")[0];
  var ia = new Uint8Array(byteString.length);
  for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }
  return new Blob([ia], { type: mimeString });
}

// 拍照
function takePhoto() {
  //获得Canvas对象
  let video = document.getElementById("video");
  let canvas = document.getElementById("canvas");
  let ctx = canvas.getContext("2d");
  ctx.drawImage(video, 0, 0, 500, 500);

  // toDataURL  ---  可传入'image/png'---默认, 'image/jpeg'
  let base64Data = document.getElementById("canvas").toDataURL();
  let blob = dataURItoBlob(base64Data);
  let file = new File([blob], `photo.png`, { type: "image/png" });

  uploadFileApi(file).then((res) => {
    console.log("上传图片", res);
    cameraEmit("uploadFinish", res);
    closeMedia();
  });
}

// 关闭摄像头
function closeMedia() {
  let stream = document.getElementById("video").srcObject;
  let tracks = stream.getTracks();

  tracks.forEach(function (track) {
    track.stop();
  });

  document.getElementById("video").srcObject = null;
  visible.value = false;
}

const visible = ref(false);

const selectFile = () => {
  let input = document.getElementById("fileInput");
  input.click();
  input.onchange = function () {
    let file = this.files[0];
    console.log(file);
    if (file) {
      uploadFileApi(file).then((res) => {
        cameraEmit("uploadFinish", res.data);
      });
    }
  };
};

const clickReset = () => {
  cameraEmit("resetSelect");
};

const clickDeleteAll = () => {
  cameraEmit("deleteAll");
};
</script>

<style lang="scss" scoped>
.camera-upload {
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
  width: 292px;
  height: 292px;
  border-radius: 16px 16px 16px 16px;
  border: 2px dashed rgba(0, 0, 0, 0.2);

  .row {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
  }

  .icon-back:nth-child(1) {
    margin-right: 58px;
  }
}
</style>

<style lang="scss" scoped>
#carmer-dialog {
  background: #f1f3f5;
  box-shadow: 0px 0px 80px 10px rgba(0, 0, 0, 0.25);
  border-radius: 16px 16px 16px 16px;
  width: 924px;
  max-height: 80vh;

  .top-title {
    width: 100%;
    height: 38px;
    font-weight: 500;
    font-size: 38px;
    color: rgba(0, 0, 0, 0.9);
    text-align: center;
  }

  .center-content {
    margin-top: 24px;
    width: 100%;
    //height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    color: rgba(0, 0, 0, 0.9);
  }

  .body {
    width: calc(100% - 240px);
    max-height: calc(50vh - 80px);
    margin: 0 auto;
    overflow-y: auto;

    .box {
      width: 100%;
      height: 100px;
    }
  }

  .bottom-btns {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    margin-bottom: 20px;

    .button {
      margin-right: 20px;
    }

    .cancelBtn {
      width: 292px;
      height: 80px;
      background: rgba(0, 0, 0, 0.06);
      border-radius: 76px 76px 76px 76px;
    }

    .sureBtn {
      width: 292px;
      height: 80px;
      background: #0a59f7;
      border-radius: 76px 76px 76px 76px;
    }
  }
}
</style>
