<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="transportList && transportList.length">
            <view class="div item" v-for="item in transportList" :key="item.transport_id">

                    <view class="div bottom-wrapper">
                        <view class="div bottom-left-wrapper">
                            <label class="subtitle">
                                {{ item.transport_title }}
                            </label>
                        </view>
                        <view class="div bottom-right-wrapper">
                            <view v-if="ifComponent" class="div edit-wrapper" @click="onUse(item.transport_id,item.transport_title)">
                                <view class="div common-btn btn-2">应用</view>
                            </view>
                            <view class="div edit-wrapper" @click="onEdit(item.transport_id)">
                                <view class="div common-btn btn-2 ml-5">编辑</view>
                            </view>
                            <view class="div edit-wrapper delete-wrapper" @click="onDelete(item.transport_id)">
                                <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>
</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 { getTransportList, delTransport } from '../../../api/sellerTransport'
export default {
  name: 'CostList',
  props: {
    ifComponent: {
      type: Boolean,
      default: false
    },
      startLoading: {
          type: Boolean,
          default: false
      }
  },
  components:{
    TitleHeader,
    SellerBase,
    EmptyRecord
  },
  data(){
    return {
      navHeight: 0,
      dialog:{},
      params: { 'page': 0, 'per_page': 10 },
      loading: this.startLoading, // 是否加载更多
      isMore: true, // 是否有更多
      transportList: false // 记录列表
    }
  },
  computed:{
    fontSize(){
      return getFontSize()
    },
    storecost_state () {
      return function (state) {
        if (state === 0) {
          return '未结算'
        } else {
          return '已结算'
        }
      }
    }
  },
  onShow () {
  	this.reload()
  },
  mounted(){
  // #ifdef  MP-WEIXIN
  this.navHeight = uni.getMenuButtonBoundingClientRect().height
  // #endif
  },
  methods:{
    closeDialog(){
    },
    confirmDialog(value){
        delTransport(this.dialog.data).then(res => {
          this.reload()
        }).catch(error => {
          uni.showToast({icon:'none',title: error.message})
        })
    },
    onUse (transport_id, transport_name) {
      this.$emit('useTransport', transport_id, transport_name)
    },
    goBack () {
      if (this.ifComponent) {
        this.$emit('closeTransortList')
      } else {
        uni.navigateBack({delta:1})
      }
    },
    onAdd () {
      if (this.ifComponent) {
        this.$emit('addTransport')
      } else {
        uni.navigateTo({url:'/pages/seller/transport/transportForm'+'?'+urlencode({ action: 'add' })})
      }
    },
    onEdit (transport_id) {
      if (this.ifComponent) {
        this.$emit('editTransport', transport_id)
      } else {
        uni.navigateTo({url:'/pages/seller/transport/transportForm'+'?'+urlencode({ action: 'edit', transport_id: transport_id })})
      }
    },
    onDelete (transport_id) {
      this.dialog={content:'您确定要删掉此售卖地区吗？',data:transport_id}
      this.$refs.confirm.open()
    },
    getTransportList (ispush) {
      uni.showLoading({ title: '加载中' })
      let params = this.params
      getTransportList(params).then(res => {
        uni.hideLoading()

        if (ispush && this.transportList) {
          this.transportList = this.transportList.concat(res.result.transport_list)
        } else {
          this.transportList = res.result.transport_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.getTransportList(true)
      }
    },
    reload () {
      // 重新加载数据
      this.loading = false
      this.params.page = 0
      this.isMore = true
      this.transportList = 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}
    .item {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: stretch;
        background-color: #fff;
        border-bottom: 1px solid #e8eaed;
    }
    .bottom-wrapper {
        height: 2.5rem;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        align-items: stretch;
    }
    .bottom-left-wrapper {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;
    }
    .bottom-right-wrapper {
        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;
    }
    .delete-wrapper {
        margin-right: 0.5rem;
    }
    .subtitle {
        font-size:$subFontSize;
        color: #7c7f88;
        margin-top: 0.5rem;
        margin-bottom: 0.5rem;
        margin-left: 1rem;
        margin-right: 0.5rem;
    }
    .subtitle .i{font-size:$h2;margin-right:.5rem;}
</style>
