<template>
	<view :style="'background-color: '+theme.bg+';color: '+theme.text" style="height: 100vh;">
		<myNav :title="id?i18n.mine.address.modify:i18n.mine.address.add"></myNav>
		
		<view @click="coinType=true" class="width-100b height-96 padding-w-60 flex align-center justify-between">
			<text class="font-30 bold-font">{{i18n.mine.address.curr}}</text>
			<text class="font-30 bold-font color-0165EE">{{curr_id?curr_id:i18n.mine.address.subtit}}</text>
		</view>
		
		<view class="width-100b padding-w-60">
			<view class="width-100b height-96 line-height-96">
				<text class="font-30 bold-font">{{i18n.mine.address.address}}</text>
				<text class="font-26 numbold-font color-0165EE margin-l-20">{{address|addressFilter}}</text>
			</view>
			<view class="width-100b height-96">
				<input class="width-100b height-96 font-30" type="text" v-model="address" :placeholder="i18n.mine.address.p1" placeholder-class="color-C3C3C3"/>
			</view>
		</view>
		
		<view class="width-100b padding-w-60">
			<view class="width-100b height-96 line-height-96">
				<text class="font-30 bold-font">{{i18n.mine.address.remark}}</text>
			</view>
			<view class="width-100b height-96">
				<input class="width-100b height-96 font-30" type="text" v-model="dsc" :placeholder="i18n.mine.address.p2" placeholder-class="color-C3C3C3"/>
			</view>
		</view>
		
		<u-picker @confirm="selectCoin" v-model="coinType" mode="selector" range-key="full_name" :range="coinSelector"
		:confirm-text="i18n.mine.address.confirmText" :cancel-text="i18n.mine.address.cancelText"></u-picker>
		
		<button @click="add()" class="cu-btn loginBtn">{{i18n.mine.address.save}}</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id:"",
				address:"",
				dsc:"",
				// userId:"",
				curr_id:'',
				name:'',
				
				coinType:false,
				coinSelector: [''],
				
				flag:true,
				theme:'',
			}
		},
		onLoad:function(option){
			// if(getApp().globalData.type = "address"){
			// 	this.id = getApp().globalData.data.id
			// 	this.dsc = getApp().globalData.data.description
			// 	this.address = getApp().globalData.data.address
			// 	this.userId = getApp().globalData.data.userId
			// 	getApp().globalData.data = ""
			// 	getApp().globalData.type = ""
			// 	console.log(this.id)
			// }
			console.log(option)
			this.get_curr()
			if(option.id){
				this.id = option.id
				this.getAddress(option.id)
			}
		},
		onShow:function(){
			if (uni.getStorageSync("theme") == "dark") {
				this.theme = this.$store.state.darkTheme
			}else{
				this.theme = this.$store.state.lightTheme
			}
		},
		computed: {
			i18n() {
				return this.$t('message');
			},
		}, 
		filters:{
			addressFilter(item){
				if(item.length < 10) return
				if(item.slice(0,1) == 'T'){
					return 'TRC20'
				}
				if(item.slice(0,2) == '0x'){
					return 'ERC20'	
				}
			},
		},
		methods: {
			add(){
				if(!this.flag) return
				if(this.curr_id==""){
					this.tos(this.i18n.mine.address.p0)
					return
				}
				if(this.address==""){
					this.tos(this.i18n.mine.address.p1)
					return
				}
				if(this.dsc==""){
					this.tos(this.i18n.mine.address.p2)
					return
				}
				uni.showLoading()
				this.flag = false
				if(this.id == ''){
					this.newData()
				}else{
					this.change()
				}
			},
			selectCoin(index){
				// console.log(this.coinSelector[index])
				this.curr_id = this.coinSelector[index].full_name
				// this.name = this.coinSelector[index].name_lian
			},
			get_curr(){
				this.post('/my/get_lian').then(res => {
					console.log('提现币种列表',res)
					if(res.code == 1){
						this.coinSelector = res.data
					}
				})
			},
			getAddress(id){
				this.post('/my/address_con',{id: id}).then(res => {
					console.log('地址详情',res)
					if(res.code == 1){
						this.curr_id = res.data.full_name
						this.address = res.data.address
						this.dsc = res.data.notice
						// this.name = res.data.name_lian
					}
				})
			},
			newData(){
				this.post('/my/add_address', {
					full_name: this.curr_id,
					address: this.address,
					notice: this.dsc,
				}).then(res => {
					this.flag = true
					uni.hideLoading()
					if(res.code == 1){
						this.tos(res.msg)
						setTimeout(()=>{
							uni.navigateBack()
						},500)
					}
				})
			},
			change(){
				this.post('/my/address_upd', {
					id: this.id,
					full_name: this.curr_id,
					address: this.address,
					notice: this.dsc,
				}).then(res => {
					this.flag = true
					uni.hideLoading()
					if(res.code == 1){
						this.tos(res.msg)
						setTimeout(()=>{
							uni.navigateBack()
						},500)
					}
				})
			}
		}
	}
</script>

<style>
	.loginBtn{
		width: 600rpx;
		height: 96rpx;
		background: linear-gradient(180deg, #52F3C9 0%, #3ECEB2 100%) !important;
		border-radius: 12rpx;
		line-height: 96rpx;
		color: #FFFFFF;
		font-size: 32rpx;
		text-align: center;
		position: absolute;
		bottom: 5%;
		left: 50%;
		margin-left: -300rpx;
	}
</style>
