<template><page-meta :root-font-size="fontSize+'px'"></page-meta>
	<seller-base :show="false">
	<view class="div distributor-address-list">
		<view class="div common-header-wrap">
			<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="div pb-30" v-if="address_list.length>0">
			<view class="div item" v-for="item in address_list" :key="item.daddress_id">
				<view class="div container">
					<view class="div top-wrapper">
						<view class="div selected-wrapper">
							<view class="div line">
								<text class="i icon iconfont">&#xe6d3;</text>
								<view class="div line-content">
									<view class="div title-wrapper">
										<label class="title">{{item.seller_name}}</label>
										<label class="title" style="margin-left:1rem;">{{item.daddress_telphone}}</label>
									</view>
									<view class="div desc address-text">{{item.area_info}}{{item.daddress_detail}}</view>
								</view>
							</view>
						</view>
						<view class="div bottom-line"></view>
					</view>
					<view class="div bottom-wrapper">
						<view class="div bottom-left-wrapper" @click="onDefault(item)">
							<label class="subtitle">
								<text v-if="item.daddress_isdefault == 1" class="i iconfont">&#xe69d;</text><text v-else class="i iconfont">&#xe69e;</text>默认地址
							</label>
						</view>
						<view class="div bottom-right-wrapper">
							<view class="div edit-wrapper" @click="onEdit(item.daddress_id)">
								<view class="div common-btn btn-2 mr-5">编辑</view>
							</view>
							<view class="div edit-wrapper delete-wrapper" @click="onDelete(item.daddress_id)">
								<view class="div common-btn btn-1">删除</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="div"  v-else>
			<empty-record></empty-record>
		</view>
		<view class='common-add-btn-wrapper'><view class="div common-btn common-add-btn"  @click="goAdd">新增</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 { getAddressList, delAddress, editAddress } from '../../../api/sellerAddress'
export default {
  components:{
    TitleHeader,
    SellerBase,
    EmptyRecord
  },
  name: 'SellerAddressList',
  computed:{
  fontSize(){
    return getFontSize()
  },
  },
  data(){
    return {
      navHeight: 0,
      address_list: []
    }
  },

  onShow: function () {
    this.getAddressList()
  },
  mounted(){
  // #ifdef  MP-WEIXIN
  this.navHeight = uni.getMenuButtonBoundingClientRect().height
  // #endif
  },
  methods: {
    goBack () {
      uni.navigateBack({delta:1})
    },
    goAdd () {
      uni.navigateTo({url:'/pages/seller/address/AddressForm'+'?'+urlencode({ action: 'add' })})
    },
    onDefault (address_info) {
      address_info.daddress_isdefault = 1
      editAddress(address_info, address_info.daddress_id).then(
        (response) => {
          uni.hideLoading()
          this.getAddressList()
        }, (error) => {
          uni.hideLoading()
          uni.showToast({icon:'none',title: error.message})
        })
    },
    onEdit (addressId) {
      uni.navigateTo({url:'/pages/seller/address/AddressForm'+'?'+urlencode({ address_id: addressId })})
    },
    onDelete (addressId) {
      uni.showLoading({ title: '加载中' })
      delAddress(addressId).then(
        (response) => {
          this.getAddressList()
          uni.hideLoading()
        }, (error) => {
          uni.hideLoading()
          uni.showToast({icon:'none',title: error.message})
        })
    },
    getAddressList () {
      getAddressList().then(res => {
        this.address_list = res.result.address_list
      }).catch(function (error) {
        uni.showToast({icon:'none',title: error.message})
      })
    }
  }
}
</script>
<style  lang="scss" scoped>
	.distributor-address-list {
		.common-header{
			.btn{background: #000;color: #fff;box-shadow: 0px 2px 4px #d2d2d2;}
		}
		.line{display: flex;
			.line-content{flex:1}
		}
		.item{margin-bottom:$modelSpace}
		.container{background: #fff;padding:0 $pageSpace}
		.btn-1{border:1px solid $primaryColor;color:$primaryColor;min-width: 2rem}
		.btn-2{border:1px solid #333;color:#333;min-width: 2rem}
		.top-wrapper {
			position: relative;
			flex: 1;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			background-color: #fff;
		}
		.title-wrapper {
			height: 1rem;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
		}
		.icon {
			font-size:$h1;
			margin-right: .2rem;
			color:$primaryColor
		}
		.title {
			font-size:$h3;
			color: #333;
			font-weight: 700;
		}
		.default {
			width: 1.4rem;
			margin-left: 0.5rem;
			margin-right: 0.5rem;
			border: 1px solid $primaryColor;
			color: $primaryColor;
			font-size:$h6;
			text-align: center;
			border-radius: 0.1rem;
		}
		.desc {
			color: $descTextColor;
			font-size:$subFontSize;
		}
		.address-text {
			margin-top: .3rem;
			font-weight: normal;
		}
		.selected-wrapper{
			padding:1rem 0;
		}
		.bottom-line {
			position: absolute;
			left: 0;
			bottom: 0;
			right: 0;
			border-bottom:1px dashed #eee
		}
		.bottom-wrapper {
			height: 2.5rem;
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			align-items: stretch;
			align-items: center;
		}
		.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: $descTextColor;
		}
		.subtitle .i{font-size:$h2;margin-right:.5rem;}
	}

</style>
