<template>
	<view class="zd_container">
		<nav-bar :title="$t('login.settings')" :inner2="true" @goIn="goIn" :show="false"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view>
					<view class="filter_label">{{$t('setting.language')}}</view>
					<view class="filter_select">
						<uni-data-select :placeholder="$t('utils.selectPlaceholder')" :emptyTips="$t('utils.selectEmptyTips')" v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view>
					<view class="filter_label">{{$t('setting.server')}}</view>
					<input type="text" class="filter_input" v-model="addrip">
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
		  <button class="zd-col-24 button-primary" @click="_submit">{{$t('button.confirm')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	export default {
		components: {
			NavBar
		},
		data() {
			return {
				addrip: this.$store.getters.baseUrl,
				options1: [{text: '中文',value: 'zh-Hans'}, {text: 'English', value: 'en'}, {text: 'Tiếng Việt', value: 'vi'}],
				index1: '',
				systemLocale: '',
				applicationLocale: ''
			};
		},
		onLoad() {
		  let systemInfo = uni.getSystemInfoSync();
		  this.systemLocale = systemInfo.language;
		  this.applicationLocale = uni.getLocale();
		  this.index1 = this.applicationLocale.split('-')[0]
		  this.isAndroid = systemInfo.platform.toLowerCase() === 'android';
		  uni.onLocaleChange((e) => {
		    this.applicationLocale = e.locale;
		  })
		},
		methods: {
			onLocaleChange(e) {
			  if (this.isAndroid) {
			    uni.showModal({
			      content: this.$t('setting.language-change-confirm'),
			      success: (res) => {
			        if (res.confirm) {
			          uni.setLocale(e);
			        }
			      }
			    })
			  } else {
				uni.setLocale(e);
			    this.$i18n.locale = e;
			  }
			},
			selectChange1(e) {
				this.onLocaleChange(e)
				this.index1 = e
			},
			goIn () {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			},
			_submit () {
				if (this.addrip === '') {
					return
				}
				// 存值
				this.$store.dispatch('setConfig',{baseUrl: this.addrip})
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>
