<template>
	<view class="pages">
		<uni-forms class="form" :value="dataObj" ref="formData" :rules="rules"  :border="true"
			label-width="100">
			<uni-forms-item label="服务地址" name="url" :required="true" label-width="100">
				<input v-model="dataObj.url" type="text"
					class="uni-input" placeholder="例如：192.168.1.125" />
			</uni-forms-item>
			<uni-forms-item label="端口号" name="port" :required="true" label-width="100">
				<input v-model="dataObj.port" type="text"
					   class="uni-input" placeholder="例如：8081" />
			</uni-forms-item>
		</uni-forms>
		<button class="but" type="default" :loading="loading" :disabled="loading" @tap="changePass">确认</button>
	</view>
</template>

<script>
	import {
		updateMobile,
	} from '@/common/api.js'
    import {
        api_config
    } from "@/config.js"
	export default {
		data() {
			return {
				dataObj: {
					url: "",
					port:""
				},
				loading: false,
				rules: {
                    url: {
						rules: [{
								required: true,
								errorMessage: '请输入服务地址',
							},
							{
								validateFunction: function(rule, value, data, callback) {
									if (!(/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/.test(data.url))) {
										callback('请输入正确的服务地址')
									}
									return true
								}
							}
						]
					},
                    port: {
                        rules: [{
                            required: true,
                            errorMessage: '请输入端口号',
                        },
                            {
                                validateFunction: function(rule, value, data, callback) {
                                    if (!(/^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/.test(data.port))) {
                                        callback('请输入正确端口号')
                                    }
                                    return true
                                }
                            }
                        ]
                    },
				}
			}
		},
		onReady() {
		        this.$refs.formData.setRules(this.rules)
		    },
        onShow() {
            this.dataObj.url = uni.getStorageSync("xjappserver").url
            this.dataObj.port = uni.getStorageSync("xjappserver").port
        },
		methods: {
			changePass() {
                this.$refs['formData'].validate().then(result => {
                    uni.setStorageSync("xjappserver", {
                    url:this.dataObj.url,
                    port:this.dataObj.port,
					})
                console.log("+++++++++++++++++++++++++++++++")
					plus.nativeUI.toast('操作成功')
                console.log("+++++++++++++++++++++++++++++++")
                // api_config = this.dataObj.url+":"+this.dataObj.port
                console.log(api_config)
					setTimeout(() =>{

						/*uni.navigateBack({

						})*/
                uni.redirectTo({
                    animationType: 'pop-in',
                    animationDuration: 300,
                    url: '../../pages/native/login',
                })
					},1000)
				})
				.catch(errors => {
						console.log("验证不通过=>", errors);
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.form {
		border-bottom: 1px solid #EEEEEE;
	}

	.but {
		width: 600rpx;
		margin-top: 420rpx;
		background-color: #0099FF;
		color: #fff;
	}
</style>
