<template>
  <div class="select-commodity">
    <a class="close" @click="$emit('cancle')">+</a>
    <h3 class="title">{{title}}</h3>
    <div class="filter">
      <div>
        <input
          type="text"
          placeholder="输入关键词"
          v-model="key"
          @keyup.enter="isClickSearch = !!key, (paging.current = 1) && $emit('updateList',key)">
          <i class="iconfont iconsearch" @click="isClickSearch = !!key, (paging.current = 1) && $emit('updateList',key)"></i>
      </div>
    </div>

   <template>
      <div class="table-layer">
      <table class="list">
        <tr>
          <th>
            <div class="checkbox" v-if="!hideAll">
              <input
                type="checkbox"
                v-model="allSelect"
                @click="e => selectAll(e.target)">
              <span></span>
            </div>
          </th>
          <th v-for="(item,i) in tabHeaders" :key="i">{{item.name}}</th>
        </tr>
        <tr v-for="(item,i) in list" :key="i">
          <td>
            <div class="checkbox">
              <input
                type="checkbox"
                @change="e => selectItem(item, e.target)"
                :checked="select.some(i => i[primaryKey] === item[primaryKey])"
              >
              <span></span>
            </div>
          </td>
          <td v-for="(sub,idx) in tabHeaders" :key="idx">
            <div v-if="sub.type=='image'" class="list-img">
              <img :src="item[sub.key]" alt="">
            </div>
            <div class="audio" v-else-if="sub.type=='audio'" >
              <v-audio :vkey="item.id" :idleft="`_${$randomWord(true,8,8)}`" :idright="`_${$randomWord(true,8,8)}`" :myAudioUrl="item[sub.key]"></v-audio>
          </div>
            <span class="shenglvhao" v-else>{{item[sub.key]}}</span>
          </td>
        </tr>
        <tr v-if="list.length === 0">
          <td colspan="10">
            <div class="nodata">
              <img :src="$noresult" alt="">
              <span>{{isClickSearch?'未搜索到结果~':'暂无素材，快去上传吧'}}</span>
            </div>
          </td>
        </tr>
      </table>
    </div>

    <div class="paging">
      <p></p>
      <Paging
        v-if="paging.total>0"
        style="float: right"
        :paging="paging"
        @changeCurrent="changeCurrent"
        />
    </div>
   </template>

    <div class="btns">
      <a @click="$emit('cancle')">取消</a>
      <a :class="{disable:disable}" @click="$emit('submit', select)" >确定</a>
    </div>
  </div>
</template>

<script>
import Paging from './paging'
import vAudio from '@/components/audio'

export default {
props:{
  list:{
    default(){
      return []
    },
    type:Array
  },

  tabHeader:{
    default(){
      return []
    },
    type:Array
  },
 
  paging: {
    default:function () {
      return {}
    },
    type: Object
  },
  selected:{
    default(){
      return []
    },
    type:Array
  },
  title:{
    default:'',
    type:String
  },
  current: {
    default:''
  },
  primaryKey: {
    default:'id'
  },
  hideAll:{
    type:Boolean,
    default:false
  }
},
  components:{
    Paging,
    vAudio
  },
  watch:{
    select(){
    }
  },
  computed:{
    disable(){
      return !this.select.length
    },
    tabHeaders(){
      return this.tabHeader.filter(item=>{
        return item.key!='detail'
      })
    }
  },
  data () {
    return {
      categs: [],
      isClickSearch:'',
      select: [...this.selected],
      allSelect: false,
      key:'',
      tmppaging:{...this.paging}
    }
  },

  methods: {
    changeCurrent(data){
      this.$emit('changeCurrent',data)
    },
    selectAll(isSelect) {
      this.list.forEach(item => {
        this.selectItem(item, isSelect)
      })
    },
    selectItem(item, e) {
      let isSelect = e.checked
      if (this.hideAll) {
        if (item.isUse == '1') {
          e.checked = false
          return this.$alert({content:'选中素材不能超过600kb'})
        }
        this.select = [item]
      }
      else{
        let index = this.select.findIndex(i => i[this.primaryKey] === item[this.primaryKey])
        if (isSelect) {
          !~index && this.select.push(item)
        } else {
          ~index && this.select.splice(index, 1)
        }
      }

    },
    
  },
  mounted() {
    this.key = ''
  }
}
</script>

<style lang="less" scoped>
::-webkit-scrollbar-track {
    box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
    border-radius: 4px;
    background: #fff;
}
.shenglvhao{
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  max-width: 200px;
  display: inline-block;
}
.select-commodity {
  position: absolute;
  z-index: 3;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
  color: #202020;
  background: #FFFFFF;
  max-width: 960px;
  width: 100%;
  border-radius: 6px;
}

.select-commodity > * {
  padding: 15px;
}

.title {
  font-size: 16px;
  line-height: 20px;
  font-weight: 400;
  margin: 0;
  font-weight: bold;
  border-bottom: 1px solid rgba(255,255,255,0.3);
}

.close {
  position: absolute;
  right: -8px;
  top: -15px;
  font-size: 25px;
  color: #909090;
  transform: rotate(45deg);
  cursor: pointer;
}

.filter {
  padding: 15px;
}

.filter > div {
  width: 210px;
  height: 34px;
  background: rgba(22,26,26,1);
  border-radius: 2px;
  display: inline-block;
  margin-right: 10px;
  position: relative;
  vertical-align: middle;
}

.filter > div > select,
.filter > div > input {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  border: none;
  background: none;
  color: #202020;
  padding: 8px 10px;
  outline: none;
  background: #FFFFFF;
  border: 1px solid #EBEBEB;
}

.filter > div > input {
  padding-right: 40px;
}

.filter > div > i {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 10px;
  cursor: pointer;
  color:@color;
}

.scene-layer {
  min-height: 400px;
  max-height: 60vh;
  overflow-y: auto;
  position: relative;
  .nodata{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
  }
}

.table-layer {
  min-height: 400px;
  max-height: 60vh;
  overflow-y: auto;
}


.list {
  border-collapse: collapse;
  width: 100%;
}

.list td,
.list th {
  font-size:14px;
  line-height:20px;
  text-align: center;
  padding: 10px 0;
  color: #909090;
  border-bottom: 1px solid #EBEBEB;
}

.list th {
  font-weight: bold;
  color: #202020;
}


.list-img {
  height: 40px;
  line-height: 40px;
  position: relative;
}

.list-img > img {
  width: 40px;
  height: 40px;
}

.checkbox {
  position: relative;
  width:14px;
  height:14px;
  border-radius:2px;
}

.checkbox > input,
.checkbox > span {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}

.checkbox > input {
  z-index: 1;
  opacity: 0;
  cursor: pointer;
}

.checkbox > span {
  z-index: 2;
  background:#fff;
  border:1px solid #ccc;
  pointer-events: none;
}

.checkbox > input:checked + span {
  background:#fff;
  border:1px solid rgba(0,200,175,1);
  background:@color url(/static/img/g.png) no-repeat center center;
}

.checkbox > input:disabled {
  cursor: not-allowed;
}

.checkbox > input:disabled + span {
  background:#CCCCCC;
  background:rgba(0,200,175,0.3) no-repeat center center;
}

.paging {
  display: flex;
  align-items: center;
  justify-content: space-between;
  p {
    font-size: 12px;
  }
}

.btns {
  text-align: center;
  > a {
    width:120px;
    height:40px;
    text-align: center;
    line-height: 40px;
    opacity:1;
    border-radius:20px;
    font-size:12px;
    cursor: pointer;
    display: inline-block;
    color: @color;
    margin: 0 10px;
    &:nth-child(2) {
      background-color: @color;
      color: #fff;
    }
    &:nth-child(1) {
      border:1px solid @color;
    }
  }
  .disable{
    pointer-events: none!important;
    opacity: 0.5!important;
  }
}
</style>