<template>
	<fs-form ref="formRef" :model="userInfo">
		<fs-cell title="头像" justify="right" border tighten>
			<template #value>
				<fs-avatar bgColor="#EBEFF5" :src="userInfo.photo" @click="handleUpload">
					<fs-icon type="icon-photo"></fs-icon>
				</fs-avatar>
			</template>
		</fs-cell>
		<fs-form-item required label="姓名">
			<fs-field placeholder="请输入姓名" v-model="userInfo.name"></fs-field>
		</fs-form-item>
		<fs-form-item required label="手机号">
			<fs-field placeholder="请输入手机号" v-model="userInfo.mobile"></fs-field>
		</fs-form-item>
		<fs-form-item label="性别">
			<fs-radio-group inline v-model="userInfo.sex">
				<fs-radio value="1">男</fs-radio>
				<fs-radio value="2">女</fs-radio>
			</fs-radio-group>
		</fs-form-item>
		<fs-gutter height="100rpx"></fs-gutter>
		<fs-button block round @click="handleSave">保存</fs-button>
	</fs-form>
</template>

<script setup>
import { ref, computed, getCurrentInstance } from 'vue'
import useForm from '@/hooks/useForm'
import useValidator from '@/hooks/useValidator'
import { useUserStore } from '@/stores/user'
import utils from '@/utils/utils'

const user = useUserStore()
const userInfo = computed(() => user.userInfo)
const validator = useValidator()

const rules = {
	name: {
		required: true,
		message: '请输入姓名'
	},
	mobile: { validator: validator.mobile }
}
const formRef = ref(null)
const form = useForm(rules, 'formRef')

const { proxy } = getCurrentInstance()
const handleUpload = () => {
	utils
		.chooseAndUploadImage(
			{},
			{
				url: proxy.$uploadUrl,
				editable: true
			}
		)
		.then(res => {
			userInfo.value.photo = res
		})
}

const handleSave = () => {
	form.validate().then(() => {
		user.setUserInfo(userInfo.value)
		uni.switchTab({
			url: '/pages/my/my'
		})
	})
}
</script>

<style lang="scss" scoped></style>
