<template>
  <div class="mod-prod-qrcodeCardNote">
    <!-- 搜索相关区域 -->
    <div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" label-width="auto" size="small">
        <!-- 表单项 -->
        <div class="input-row">
          <el-form-item prop="type" label="笔记类型:">
            <el-select v-model="searchForm.noteType" clearable filterable>
              <el-option key="NORMAL" value="NORMAL" label="普通语音"></el-option>
              <el-option key="AI" value="AI" label="AI语音"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="hasAudio" label="是否有语音:">
            <el-select v-model="searchForm.hasAudio" clearable filterable>
              <el-option key="0" :value="0" label="无语音"></el-option>
              <el-option key="1" :value="1" label="有语音"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="hasAudio" label="订单编号:">
            <el-input v-model="searchForm.orderNumber" clearable filterable>订单编号</el-input>
          </el-form-item>
          <el-form-item prop="model" label="声音模型:">
            <el-input v-model="searchForm.modelName" clearable filterable>声音模型</el-input>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange()">{{ $t('crud.searchBtn') }}</div>
            <div class="default-btn" @click="resetForm()">{{ $t('shop.resetMap') }}</div>
            <span style="color: red; margin-left: 20px; font-size: 18px">只返回已付款的记录</span>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 列表相关区域 -->
    <div class="main-container">
      <div class="operation-bar">
        <div class="default-btn primary-btn" @click="addOrUpdateHandle()">{{$t("crud.addTitle")}}</div>
        <div class="default-btn primary-btn" @click="ttsBatchHandle()">批量生成语音</div>
      </div>
      <div class="table-con spec-table">
        <el-table
          ref="specListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          @selection-change="handleSelectionChange"
          style="width: 100%">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="qrCodeUrl"
            label="二维码">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="resourcesUrl + scope.row.qrCodeUrl"
                v-if="scope.row.qrCodeUrl"
                fit="fill"
                @click="previewPic(scope.row.qrCodeUrl)"
              />
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="audioUrl"
                  label="语音地址"
                  width="300">
            <template slot-scope="scope">
              <video
                  :src="resourcesUrl + scope.row.cardNote.audioUrl"
                  v-if="scope.row.cardNote.audioUrl"
                  controls="controls"
                  style="width:260px;height:60px;"
              />
            </template>
          </el-table-column>
          <el-table-column label="作者名称" prop="authorName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.cardNote.authorName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="祝福话语" prop="personalSign" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.cardNote.personalSign}}</span>
            </template>
          </el-table-column>
          <el-table-column label="笔记编号" prop="noteNo" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.cardNote.noteNo}}</span>
            </template>
          </el-table-column>
          <el-table-column label="订单编号" prop="orderNumber" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.orderNumber}}</span>
            </template>
          </el-table-column>
          <el-table-column label="笔记类型" prop="noteNo" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.cardNote.type === 'NORMAL'" type="primary">普通语音</el-tag>
              <el-tag v-if="scope.row.cardNote.type === 'AI'" type="danger">AI语音</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime}}</span>
            </template>
          </el-table-column>
          <el-table-column label="模型" prop="modelName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.cardNote.modelName}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" :label="$t('publics.operating')" width="auto">
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div class="default-btn text-btn" @click="addOrUpdateHandle(scope.row.id)">详情</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
        <el-pagination
          v-if="dataList.length"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <el-dialog :visible.sync="previewPicDialogVisible" :modal="false" title="二维码预览" width="30%" top="3vh">
      <el-image :src="previewPicUrl" alt=""  style="width: 100%; height: 100%"/>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from '../order/qrcode-card-note-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {

      }, // 搜索
      dataListLoading: false,
      addOrUpdateVisible: false,
      qrCardNoteList: [],
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      previewPicUrl: null,
      previewPicDialogVisible: false,
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getDataList()
  },
  mounted () {
  },
  methods: {
    ttsBatchHandle(){
      if(this.qrCardNoteList.length <= 0){
        this.$message.error("至少选择一条记录")
      }
      this.$http({
        url: this.$http.adornUrl('/qrNoteMapping/createAIAudioBatch'),
        method: 'POST',
        data: this.qrCardNoteList
      }).then(({ data }) =>{
        if(data){
          this.$message.success("下发成功，请稍后回来查看语音")
        }else{

        }
      })
    },
    // 多选变化
    handleSelectionChange (val) {
      this.qrCardNoteList = val
    },
    previewPic(url){
      this.previewPicUrl = this.resourcesUrl + url
      this.previewPicDialogVisible = true;
    },
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/qrNoteMapping/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign({
            current: page == null ? this.page.currentPage : page.currentPage,
            size: page == null ? this.page.pageSize : page.pageSize,
            shopId: this.$store.state.user.shopId,
          },
            this.searchForm
          )
        )
      }).then(({data}) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    resetForm () {
      this.searchForm = {
        cardNote: {}
      }
    },
    // 刷新回调
    refreshChange () {
      this.getDataList(this.page)
    },
    searchChange () {
      // this.searchForm = params
      this.getDataList(this.page)
    },
    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style lang="scss">
.mod-prod-prodTemplate {
}
</style>
