<template>
  <div class="addPlay">
    <div v-if="type == '1'">
      <div class="content">
        <div class="item">
          <div class="label">音频文件</div>
          <div class="value" @click="toRecord">
            <span class="color-999" :style="{ color: file ? '#333' : '' }">{{ file ? '已选择' : '请选择' }}</span>
            <img src="./img/right-img.png" alt="">
          </div>
        </div>
      </div>
      <div class="radio-content">
        <div class="title">素材标题</div>
        <textarea rows="2" placeholder="请输入（30字以内）" v-model="name" style="width:100%;height:80px;"
                  maxlength="30"></textarea>
      </div>
    </div>
    <div v-else>
      <div class="radio-content mar-b16">
        <div class="title">素材标题</div>
        <textarea rows="2" placeholder="请输入（30字以内）" v-model="name" style="width:100%;height:80px;"
                  maxlength="30"></textarea>
      </div>
      <div class="radio-content">
        <div class="title">文本内容</div>
        <textarea rows="8" placeholder="请输入文本内容（12000字以内）" v-model="content" style="width:100%;height:300px;"
                  maxlength="12000"></textarea>
      </div>
    </div>
    <div class="btn" @click="confirm">确认</div>

  </div>
</template>

<script>

export default {
  name: "addPlay",
  data() {
    return {
      type: '1',
      file: null,
      name: '',
      content: ''
    }
  },

  onLoad(query) {
    this.type = query.type
    uni.$on('record', e => {
      this.file = e
    })
  },
  onShow() {
    document.title = this.type == 1 ? '添加音频' : '添加文本'
  },

  methods: {
    toRecord() {
      uni.navigateTo({
        url: `./recording`
      })
    },

    dataURLtoFile(dataurl, filename) {
      var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1]
      var bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n)

      while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
      }

      return new File([u8arr], filename, {type: mime})
    },

    confirm() {
      if (!this.file && this.type === '1') {
        return this.$u.toast('请选择音频文件')
      }

      if (!this.name) {
        return this.$u.toast('请输入素材标题')
      }

      if (!this.content && this.type === '3') {
        return this.$u.toast('请输入文本内容')
      }

      uni.showLoading()
      let formData = {}
      formData = new FormData()
      if (this.type === '1') {
        formData.append('file', this.dataURLtoFile(this.file, this.name + '.mp3'))
        formData.append('type', this.type)
        formData.append('content', this.content)
        formData.append('name', this.name)
      } else {
        formData.append('type', this.type)
        formData.append('content', this.content)
        formData.append('name', this.name)
      }
      this.$http.post(`/app/appdlbresource/addResourceWithFile`, formData).then((res) => {
        uni.hideLoading()
        if (res.code === 0) {
          this.$u.toast('添加成功')
          uni.$emit('getList')
          uni.navigateBack({
            delta: 1
          })
        }
      }).catch(err => {
        this.$u.toast('添加失败')
        uni.hideLoading()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.addPlay {
  padding-bottom: 128px;

  .content {
    padding-left: 32px;
    background-color: #fff;

    .item {
      width: 100%;
      padding: 34px 0;
      font-size: 32px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      line-height: 44px;
      border-bottom: 1px solid #ddd;
      display: flex;
      color: #333;
      justify-content: space-between;

      .label {
        width: 198px;
        font-size: 32px;
      }

      .value {
        font-size: 28px;
        width: calc(100% - 198px);
        padding-right: 32px;
        box-sizing: border-box;
        text-align: right;

        img {
          width: 32px;
          height: 32px;
          vertical-align: middle;
          margin-left: 6px;
        }
      }

      .color-999 {
        color: #999;
      }
    }
  }

  .radio-content {
    padding: 34px 32px 38px;
    background-color: #fff;

    .title {
      font-size: 32px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #333;
      line-height: 44px;
      margin-bottom: 32px;

      span {
        font-size: 24px;
        font-weight: 400;
      }
    }
  }

  .mar-b16 {
    margin-bottom: 16px;
  }

  .btn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 112px;
    line-height: 112px;
    text-align: center;
    background: #3975C6;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFFFFF;
  }
}
</style>
