<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" class="div" @scrolltolower="loadMore" scroll-y="true">
                <view class="div" v-if="liveApplyList && liveApplyList.length">
                    <view class="div live-item mb-5" v-for="(item,index) in liveApplyList" :key="item.live_apply_id">
                        <view class="div content">
                            <view class="div live-header">
                                <view class="div live-name">{{item.live_apply_name}}[直播ID:{{item.live_apply_id}}]</view>
                                <view class="div live-state">
                                    <text class="span" v-if="item.live_apply_state==0">申请中</text>
                                    <text class="span" v-if="item.live_apply_state==1">已通过</text>
                                    <text class="span" v-if="item.live_apply_state==2">已拒绝</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" />
                                </view>
                            </view>
                            <view class="div live-btn">
                                <view class="div edit-wrapper delete-wrapper" @click="onDelete(item.live_apply_id)"><view class="div common-btn btn-2">删除</view></view>
                                <view v-if="ifComponent" class="div edit-wrapper" @click="onUse(index,item.live_apply_id,item.live_apply_video)"><view class="div common-btn btn-1 ml-5">应用</view></view>
                                <view v-if="!ifComponent && item.minipro_code" class="div edit-wrapper" @click="openLiveApply(item)"><view class="div common-btn btn-1 ml-5">直播</view></view>
                            </view>
                        </view>
                    </view>
                </view>
                <empty-record v-else />
            </scroll-view>
			<view class='common-add-btn-wrapper'><view class="div common-btn common-add-btn"  @click="onAdd">新增</view></view>
        </view>
        <uni-popup background-color="#fff" ref="isshow" class="popup" >
            <image mode="aspectFit" class="img" :src="showimage" style="width:15rem;height: 15rem;" @click="hidePopup('isshow')">
        </uni-popup>
    </view>
<uni-popup background-color="#fff" ref="confirm" type="dialog">
    <uni-popup-dialog :mode="dialog.mode" :title="dialog.title" :content="dialog.content" :placeholder="dialog.content" @confirm="confirmDialog" @close="closeDialog"></uni-popup-dialog>
</uni-popup>
</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 { getLiveApplyList, delLiveApply } from '../../../api/sellerLiveApply'

export default {
  name: 'CostList',
  props: {
    ifComponent: {
      type: Boolean,
      default: false
    }
  },
  components:{
    TitleHeader,
    SellerBase,
    EmptyRecord
  },
  computed:{
  fontSize(){
    return getFontSize()
  },
  },
  data(){
    return {
      navHeight: 0,
      dialog:{},
      showimage: '',
      params: { 'page': 0, 'per_page': 10 },
      loading: false, // 是否加载更多
      isMore: true, // 是否有更多
      liveApplyList: false // 记录列表
    }
  },
  onShow () {
	this.reload()
  },
  mounted(){
  // #ifdef  MP-WEIXIN
  this.navHeight = uni.getMenuButtonBoundingClientRect().height
  // #endif
  },
  methods:{
    openLiveApply(item){
      // #ifdef  H5
      this.openImage(item.minipro_code)
      // #endif
      // #ifndef  H5
      uni.navigateTo({url:'/pages/seller/live_apply/LiveApplyDetail'+'?'+urlencode({ live_apply_id: item.live_apply_id })})
      // #endif
    },
    closeDialog(){
    },
    confirmDialog(value){
        delLiveApply(this.dialog.data).then(res => {
          this.reload()
        }).catch(error => {
          uni.showToast({icon:'none',title: error.message})
        })
    },
    showPopup(id){
      this.$refs[id].open()
    },
    hidePopup(id){
      this.$refs[id].close()
    },
    openImage (src) {
      this.showimage = src
	  this.showPopup('isshow')
    },
    onUse (index, live_apply_id, live_name) {
      if (this.liveApplyList[index].live_apply_state != 1) {
		uni.showToast({icon:'none',title: '只能应用已通过审核的直播'})
        return
      }
      this.$emit('useLiveApply', live_apply_id, live_name)
    },
    onAdd () {
      if (this.ifComponent) {
        this.$emit('addLiveApply')
      } else {
        uni.navigateTo({url:'/pages/seller/live_apply/LiveApplyForm'+'?'+urlencode({ action: 'add' })})
      }
    },
    goBack () {
      if (this.ifComponent) {
        this.$emit('closeLiveApplyList')
      } else {
        uni.navigateBack({delta:1})
      }
    },
    onDelete (live_apply_id) {
      this.dialog={content:'您确定要删掉此直播吗？',data:live_apply_id}
      this.$refs.confirm.open()
    },

    getLiveApplyList (ispush) {
      uni.showLoading({ title: '加载中' })
      let params = this.params
      getLiveApplyList(params).then(res => {
        uni.hideLoading()

        if (ispush && this.liveApplyList) {
          this.liveApplyList = this.liveApplyList.concat(res.result.live_apply_list)
        } else {
          this.liveApplyList = res.result.live_apply_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.getLiveApplyList(true)
      }
    },
    reload () {
      // 重新加载数据
      this.loading = false
      this.params.page = 0
      this.isMore = true
      this.liveApplyList = 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;}
    }
    .btn-1{border:1px solid $primaryColor;color:$primaryColor;min-width: 2rem}
    .btn-2{border:1px solid #333;color:#333;min-width: 2rem}
    .live-item{padding:.5rem;background:#fff;
        .live{width:8rem;height: 6rem;background: #000}
        .content{font-size:$subFontSize;
            .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%;border-radius: .4rem}
                }
            }
            .live-btn{
                padding-top:.5rem;
                flex: 1;
                display: flex;
                flex-direction: row;
                justify-content: flex-end;
                align-items: stretch;
                .edit-wrapper{
                    display: flex;
                    flex-direction: row;
                    justify-content: flex-start;
                    align-items: center;
                }
            }
        }
    }
    .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>
