<template>
  <div class="selectMedia">
    <AiTopFixed>
      <div class="currentLeft-top">
        <div class="left">
          <AiSelect @data="selectType" :list="typeList" v-model="type"></AiSelect>
        </div>
        <u-search v-model="keyword" :clearabled="true" placeholder="请输入素材名称/创建人员" :show-action="false" bg-color="#F5F5F5" search-icon-color="#ccc" color="#666" height="58" @search="getList" @clear="handerClear"></u-search>
      </div>
      <div class="tab">
        <u-tabs :list="tab" :is-scroll="false" :current="currIndex" @change="change" height="96" :bar-style="barStyle"></u-tabs>
      </div>
    </AiTopFixed>
    <div class="record" v-if="currIndex == 0">
      <div class="item" v-for="(item, index) in list" :key="index" @click="choose(item)">
        <img src="./img/select-blue.png" alt="" @click.stop="check(index)" v-if="item.isCheck">
        <img src="./img/cir.png" alt="" @click.stop="check(index)" v-else>
        <div class="right">
          <div class="info">
            <p>{{ item.name ? item.name.split('.')[0] : '-' }}</p>
            <div>李毅  2022-06-09 09:43:45</div>
          </div>
        </div>
      </div>
    </div>
    <div class="record" v-else>
      <div class="item" v-for="(item, index) in list" :key="index" @click="choose(item)">
        <img src="./img/select-blue.png" alt="" @click.stop="check(index)" v-if="item.isCheck">
        <img src="./img/cir.png" alt="" @click.stop="check(index)" v-else>
        <div class="right">
          <div class="info">
            <p>{{ item.name }}</p>
            <div>{{ item.content }}</div>
          </div>
        </div>
      </div>
    </div>
    <AiEmpty v-if="!list.length"></AiEmpty>
    <div class="btn">
      <div @click="confirm">确定选择</div>
    </div>
    <u-popup v-model="isShow" mode="bottom">
      <div class="audio">
        <AiVideo :src="url" autoplay></AiVideo>
        <!-- <audio :src="url" ref="audio" :controls="true" :name="autioName" style="display: block;"></audio> -->
      </div>
    </u-popup>
  </div>
</template>
<script>

export default {
  name: "selectMedia",
  data() {
    return {
      tab: [{name: '音频素材'}, {name: '文本素材'}],
      list: [],
      currIndex: 0,
      current: 1,
      isMore: false,
      isShow: false,
      url: '',
      autioName: '',
      audio: null,
      barStyle: {width: '98px', bottom: '-3px', left: '-38px'},
      keyword: '',
      type: '0',
      typeList: [{label: '全部', value: '0'}, {label: '我创建的', value: '1'},],
      mediaId: '',
    }
  },
  onLoad(option) {
    this.mediaId = option.mediaId
    this.getList()
    uni.$on('getList', () => {
      this.isMore = false
      this.list = []
      this.current = 1

      this.$nextTick(() => {
        this.getList()
      })
    })
  },

  onShow() {
    document.title = '选择播发内容'
  },

  methods: {
    selectType(selecteds) {
      this.type = selecteds?.[0]?.value
      this.getListInit()
    },
    change(index) {
      this.currIndex = index
      this.getListInit()
    },

    choose(item) {
      this.url = item.url
      this.isShow = true

      
    },

    handerClear() {
      this.keyword = ''
      this.getListInit()
    },

    getListInit() {
      this.isMore = false
      this.list = []
      this.current = 1
      this.getList()
    },

    getList() {
      if (this.isMore) return

      this.$http.post(`/app/appdlbresource/list`, null, {
        params: {
          ...this.search,
          type: this.currIndex === 0 ? 1 : 3,
          current: this.current,
          size: 10
        }
      }).then(res => {
        if (res.code == 0) {
          res.data.records.map((item) => {
            item.isCheck = false
            if(item.id == this.mediaId) {
              item.isCheck = true
              this.mediaId = ''
            }
          })
          if (this.current > 1) {
            this.list = [...this.list, ...res.data.records]
          } else {
            this.list = res.data.records
          }

          uni.hideLoading()

          if (res.data.records.length < 10) {
            this.isMore = true

            return false
          }

          this.current = this.current + 1
        } else {
          uni.hideLoading()
        }
      }).catch(() => {
        uni.hideLoading()
      })
    },

    check(index) {
      this.list.map((item) => {
        item.isCheck = false
      })
      this.list[index].isCheck = true
    },
    confirm() {
      var selectInfo = {}
      this.list.map((item) => {
        if(item.isCheck) {
          selectInfo = item
        }
      })

      if(!selectInfo.id) {
        return this.$u.toast('请选择素材')
      }

      uni.$emit('chooseMedia', {
        mediaId: selectInfo.id,
        mediaName: selectInfo.name
      })
      uni.navigateBack({
        delta: 1
      })
    }
  },

  onReachBottom() {
    this.getList()
  }
}
</script>
<style lang="scss" scoped>
.selectMedia {
  padding-bottom: 128px;

  .tab {
    border-bottom: 1px solid #ddd;
    margin-bottom: 4px;
  }

  .audio {
    width: 100%;
    height: 400px;
    box-sizing: border-box;
    padding: 104px 0 46px;
  }

  .record {
    background-color: #fff;

    .item {
      width: 100%;
      display: flex;
      padding: 32px 0 0 30px;
      box-sizing: border-box;

      img {
        width: 44px;
        height: 44px;
        margin-right: 14px;
      }

      .right{
        flex: 1;
        border-bottom: 1px solid #ddd;
        padding: 0 30px 32px 0;
      }

      .info {
        width: calc(100% - 70px);
        line-height: 44px;
        font-size: 34px;
        font-family: PingFang-SC-Medium, PingFang-SC;
        font-weight: 500;
        color: #333;

        p {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          word-break: break-all;
        }
        div{
          margin-top: 8px;
          font-size: 26px;
          font-family: PingFang-SC-Medium, PingFang-SC;
          font-weight: 500;
          color: #999;
          line-height: 36px;
        }
      }
    }
  }

  .record-text {
    background-color: #fff;

    .item {
      width: 100%;
      border-bottom: 1px solid #ddd;
      padding: 32px 30px;
      box-sizing: border-box;
      font-size: 30px;

      div {
        font-size: 30px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin-bottom: 12px;

        .color-0063E5 {
          color: #0063E5;
        }

        .color-FF8100 {
          color: #FF8100;
        }

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

      p {
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #666;
        line-height: 40px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        word-break: break-all;
        -webkit-box-orient: vertical;
      }
    }
  }

  .add-img {
    width: 120px;
    position: fixed;
    bottom: 120px;
    right: 32px;
  }

  .currentLeft-top {
    display: flex;
    align-items: center;

    .left {
      width: 200px;
      display: flex;
      align-items: center;

      img {
        width: 48px;
        height: 48px;
      }
    }
  }
  ::v-deep .u-search {
    margin-bottom: 0 !important;
  }
  .btn {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 128px;
    background: #FFF;
    border-top: 1px solid #ddd;
    padding: 24px 32px 24px 0;
    box-sizing: border-box;

    div {
      width: 192px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      background: #3975C6;
      border-radius: 4px;
      color: #fff;
      font-size: 32px;
      float: right;
    }
  }
}
</style>
