<template>
  <el-dialog
    :visible.sync="visible"
    :append-to-body="false"
    :close-on-click-modal="false"
    :modal="false"
    title="语音预览"
    top="5vh"
    width="40%"
    @close="closeDialog"
    v-if="visible">
    <el-form
      :model="dataForm"
      size="small">
      <el-form-item label="昵称">
        <el-input v-model="dataForm.cardNote.authorName" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="祝福的话语">
        <el-input v-model="dataForm.cardNote.personalSign" type="textarea" :disabled="personalSignEdit"></el-input>
      </el-form-item>
      <el-form-item label="训练原文" v-if="dataForm.cardNote.type == 'AI'">
        <el-button v-if="dataForm.cardNote.type === 'AI'" type="success" size="mini" :loading="transLoading" @click="translateTxt">翻译</el-button>
        <el-input v-model="dataForm.cardNote.trainPersonalSign" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="翻译文本" v-if="dataForm.cardNote.type == 'AI'">
        <el-button type="primary" size="mini" :loading="transLoading" @click="tts">生成语音</el-button>
        <el-button type="danger" size="mini" :loading="transLoading" @click="syncTTS">同步语音</el-button>
        <el-input v-model="dataForm.cardNote.foreignTxt" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="声音模型" v-if="dataForm.cardNote.type == 'AI'">
        <span style="color:red">{{dataForm.cardNote.modelName}}</span>
      </el-form-item>
      <el-row>
        <el-col :span="dataForm.cardNote.type === 'AI' ? 12 : 8">
          <el-form-item label="二维码" align="center">
            <el-image
              style="width: 300px; height: 300px"
              :src="resourcesUrl + dataForm.qrNoteMapping.qrCodeUrl"
              v-if="dataForm.qrNoteMapping.qrCodeUrl"/>
          </el-form-item>
        </el-col>
        <el-col :span="dataForm.cardNote.type === 'AI' ? 12 : 8">
          <el-form-item label="语音内容">
            <el-button size="mini" @click="changeAudio" v-if="dataForm.cardNote.type === 'AI' || dataForm.cardNote.type === 'NORMAL'">更换音频</el-button>
            <el-button size="mini" @click="changeAudio" v-if="dataForm.cardNote.type === 'AR'">更换视频</el-button>
            <br/>
            <video v-if="dataForm.cardNote.audioUrl"
                   style="width: 300px; height: 210px"
                   :src="resourcesUrl + dataForm.cardNote.audioUrl"
                   controls="controls"/>
          </el-form-item>
        </el-col>
        <el-col :span="dataForm.cardNote.type === 'AI' ? 12 : 8" v-if="newAudioUrl">
          <el-form-item label="新语音内容">
            <br/>
            <video
              style="width: 300px; height: 210px"
              :src="resourcesUrl + newAudioUrl"
              controls="controls"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <div style="width: 100%; text-align: center;">
          <el-button v-if="(dataForm.cardNote.type === 'AI' || dataForm.cardNote.type === 'NORMAL')" type="primary" size="large" @click="repairVideo">转换语音</el-button>
          <el-button v-if="(dataForm.cardNote.type === 'AR')" type="primary" size="large" @click="repairVideo">调整视频</el-button>
          <el-button type="success" size="large" @click="confirmChangeVideo">确认更换</el-button>
          <el-button type="success" size="large" @click="onSave">保存</el-button>
        </div>
      </el-form-item>
    </el-form>
    <audio-box v-if="audioBoxVisible" ref="audioBox" @refreshPic="refreshAudio"></audio-box>
  </el-dialog>
</template>

<script>
  import AudioBox from '@/components/AudioBox'
  export default {
    components: {
      AudioBox
    },
    name: "YuyinPreview",
    data() {
      return {
        authorNameEdit: true,
        personalSignEdit: true,
        audioBoxVisible: false,
        visible: false,
        dataForm:{},
        newAudioUrl: null,
        resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
        transLoading: false
      };
    },
    mounted() {
    },
    methods: {
      changeAudio(){
        this.audioBoxVisible = true
        this.$nextTick(() => {
          this.$refs.audioBox.init(1)
        })
      },
      refreshAudio(filePath){
        this.newAudioUrl = filePath;
      },
      onSave(){
        this.$http({
          url: this.$http.adornUrl('/cardNote'),
          method: 'PUT',
          data: this.$http.adornData(this.dataForm.cardNote)
        }).then(({ data }) =>{
          if(data){
            this.visible = false;
            this.$message.success("保存成功")
          }else{
            this.$message.error("保存失败")
          }
        })
      },
      syncTTS(){
        this.$http({
          url: this.$http.adornUrl('/qrNoteMapping/getAIAudio'),
          method: 'POST',
          data: this.$http.adornData(this.dataForm.qrNoteMapping)
        }).then(({ data }) =>{
          if(data){
            this.dataForm.cardNote.audioUrl = data
          }else{
            this.$message.error("同步失败")
          }
        })
      },
      tts(){
        this.dataForm.qrNoteMapping.cardNote = this.dataForm.cardNote
        this.$http({
          url: this.$http.adornUrl('/qrNoteMapping/createAIAudio'),
          method: 'POST',
          data: this.$http.adornData(this.dataForm.qrNoteMapping)
        }).then(({ data }) =>{
          if(data){
            this.$message.success("下发成功，请稍后回来查看语音")
          }else{

          }
        })
      },
      translateTxt(){
        this.$http({
          url: this.$http.adornUrl('/cardNote/translate'),
          method: 'POST',
          data: this.$http.adornData(this.dataForm.cardNote)
        }).then(({ data }) =>{
          if(data){
            this.dataForm.cardNote.foreignTxt = data
          }else{

          }
        })
      },
      openCanvas(){
        this.initCanvas();
        this.drawImage();
        this.addMouseListeners();
      },
      init(id){
        this.getCardNoteInfo(id);
        this.visible = true;
      },
      getCardNoteInfo(id) {
        this.$http({
          url: this.$http.adornUrl('/qrNoteMapping/getById/' + id),
          method: 'GET',
        }).then(({ data }) =>{
          if(data){
            this.dataForm = data
            this.dataForm.cardNote.trainPersonalSign = data.cardNote.personalSign
          }else{

          }
        })
      },
      repairVideo() {
        this.$confirm('确定要修复语音吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/qrNoteMapping/repairVideo'),
            method: 'POST',
            data: this.dataForm.cardNote
          }).then(({ data }) =>{
            if(data){
              this.newAudioUrl = data.newAudioUrl
            }
          })
        });
      },
      confirmChangeVideo(){
        this.dataForm.cardNote.audioUrl = this.newAudioUrl;
        /*this.$confirm('确定要保存新语音吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.dataForm.cardNote.newAudioUrl = this.newAudioUrl;
          this.$http({
            url: this.$http.adornUrl('/qrNoteMapping/changeAudio'),
            method: 'POST',
            data: this.dataForm.cardNote
          }).then(({ data }) =>{
            if(data){
              this.$message.success("转换成功")
            }else{
              this.$message.error("转换失败")
            }
          })
        });*/
      },
      closeDialog(){
        this.visible = false
        this.dataForm = {cardNote:{}, qrNoteMapping:{}}
        this.newAudioUrl = null
      }
    },
  }
</script>

<style scoped>

</style>
