<template><page-meta :root-font-size="fontSize+'px'"></page-meta>
    <seller-base :show="false"><view style="position: absolute;top:0;right:0;left:0;bottom:0" class="scroll-view-wrapper div container">
        <view class="div common-header-wrap">
            <view class="status-holder"></view>
            <view :style="'height:'+navHeight+'px'"></view>
            <view class="common-header-holder"></view>
            <view class="common-header-fixed">
            <title-header />
            <uni-nav-bar title="直播列表" class="common-header" left-icon="back" @clickLeft="goBack">
            </uni-nav-bar>
        </view>
        </view>
        <view class="scroll-view div pb-30" style="position:relative">
            <scroll-view style="position: absolute;top:0;right:0;left:0;bottom:0" v-if="ifComponent" class="div mt-5" @scrolltolower="loadMore" scroll-y="true">
                <view class="div" v-if="miniproLiveList && miniproLiveList.length">
                    <view class="div live-item mb-5" v-for="(item,index) in miniproLiveList" :key="item.minipro_live_id">
                        <view class="div content">
                            <view class="div live-header">
                                <view class="div live-name">{{item.minipro_live_name}}[直播ID:{{item.minipro_live_id}}]</view>
                                <view class="div live-state">
                                    <text class="span">已通过</text>
                                </view>
                            </view>
                            <view v-if="item.goods_list" class="div goods-list">
                                <view class="div goods-item" v-for="(goods,i) in item.goods_list" :key="i">
                                    <image mode="aspectFit" class="img goods-image" :src="goods.goods_image_url" />
                                </view>
                            </view>
                            <view class="div live-btn">
                                <view v-if="ifComponent" class="div common-btn ds-button-small edit-wrapper" @click="onUse(index,item.minipro_live_id)">应用</view>
                            </view>
                        </view>
                    </view>
                </view>
                <empty-record v-else />
            </scroll-view>
            <view class="div" v-else>
                <image mode="aspectFit" class="img" src="../../../static/image/minipro_live.jpg" style="width:100%" />
            </view>
        </view>
			<view class='common-add-btn-wrapper'><view class="div common-btn common-add-btn"  @click="onAdd">新增</view></view>
    </view>
</seller-base>
</template>

<script>
import {getFontSize} from '@/util/common'
import TitleHeader from '../../TitleHeader'
import { urlencode } from '@/util/common'

import SellerBase from '../SellerBase'

import EmptyRecord from '../../EmptyRecord'
import { getRoomList } from '../../../api/sellerMiniproLive'
export default {
  name: 'CostList',
  props: {
    ifComponent: {
      type: Boolean,
      default: false
    }
  },
  components:{
    TitleHeader,
    SellerBase,
    EmptyRecord
  },
  data(){
    return {
      navHeight: 0,
      params: { 'page': 0, 'per_page': 10 },
      loading: false, // 是否加载更多
      isMore: true, // 是否有更多
      miniproLiveList: false, // 记录列表
      inMinipro: 2
    }
  },
  onShow () {
	this.reload()
  },
  computed:{
    fontSize(){
      return getFontSize()
    },
    getBannerStyle: function () {
      const res = uni.getSystemInfoSync()
      var width = res.windowWidth
      var height = res.windowHeight
      let itemWidth = width
      let itemHeight = height - 44
      return "max-width:"+itemWidth +"px;max-height:"+itemHeight +"px"
    }

  },
  mounted(){
  // #ifdef  MP-WEIXIN
  this.inMinipro = 1
  this.navHeight = uni.getMenuButtonBoundingClientRect().height
  // #endif
  },
  methods: {
    goMiniproLive (room_id) {


      if (this.inMinipro == 2) {
		uni.showToast({icon:'none',title: '请在小程序中打开'})
        return
      }
      if (this.inMinipro == 1) {
        wx.navigateTo({ url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=' + room_id })
      }
    },
    onUse (index, minipro_live_id) {
      this.$emit('useMiniproLive', minipro_live_id)
    },
    onAdd () {
      if (this.ifComponent) {
        this.$emit('addMiniproLive')
      } else {
        uni.navigateTo({url:'/pages/seller/minipro_live/MiniproLiveForm'+'?'+urlencode({ action: 'add' })})
      }
    },
    goBack () {
      if (this.ifComponent) {
        this.$emit('closeMiniproLiveList')
      } else {
        uni.navigateBack({delta:1})
      }
    },
    getMiniproLiveList (ispush) {
      uni.showLoading({ title: '加载中' })
      let params = this.params
      getRoomList(params).then(res => {
        uni.hideLoading()

        if (ispush && this.miniproLiveList) {
          this.miniproLiveList = this.miniproLiveList.concat(res.result.minipro_live_list)
        } else {
          this.miniproLiveList = res.result.minipro_live_list
        }
        if (res.result.hasmore) {
          this.isMore = true
        } else {
          this.isMore = false
        }
        this.loading = false
      }).catch(error=>{
          uni.hideLoading()
          uni.showToast({icon:'none',title: error.message})
      })
    },
    loadMore () {
      if (this.loading) {
        return
      }
      this.loading = true
      this.params.page = ++this.params.page
      if (this.isMore) {
        this.getMiniproLiveList(true)
      }
    },
    reload () {
      // 重新加载数据
      this.loading = false
      this.params.page = 0
      this.isMore = true
      this.miniproLiveList = false
      this.loadMore()
    }
  }
}
</script>

<style scoped lang="scss">
.scroll-view-wrapper{display: flex;flex-direction: column;}
.scroll-view{flex:1}
.common-header{
        .btn{background: #000;color: #fff;box-shadow: 0px 2px 4px #d2d2d2;}
    }
.live-item{padding:.5rem;border-bottom:1px solid #eee;background:#fff;
    .live{width:8rem;height: 6rem;background: #000}
    .content{font-size:$subFontSize;padding-left:.5rem;
        .live-header{display: flex;
            .live-name{flex:1}
            .live-state{color:$primaryColor}
        }
        .goods-list{white-space: nowrap;width: 100%;overflow: hidden;overflow-x: auto;margin-top: .5rem;
            .goods-item{display: inline-block;margin-right: .5rem;width: 5rem;height: 5rem;
                .goods-image{width: 100%;height: 100%}
            }
        }
        .live-btn{padding-top:.5rem;overflow: hidden;
            .edit-wrapper{margin-left:1rem;float: right}
        }
    }
}
.my-popup {
    text-align: center;
    position: fixed;
    background: #fff;
    top: 50%;
    left: 50%;
    -webkit-transform: translate3d(-50%, -50%, 0);
    transform: translate3d(-50%, -50%, 0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-transition: .2s ease-out;
    transition: .2s ease-out;}
</style>
