<template>
	<view class="common_page" :style="{'height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title="个人资料" bgColor="#FFFFFF"></cus-header>
		<view class="form" v-if="userInfo">
			<view class="avatar">
				<view class="title">头像</view>
				<view class="imgbox">
					<up-upload
						:fileList="fileList"
						@afterRead="afterRead"
						@delete="deletePic"
						multiple
						:maxCount="1"
						width="122rpx"
						height="122rpx"
					>
						<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/09/29/b04d6b0e-0d30-4043-a2b1-d639e15e2aac.png" 
						mode="widthFix" style="width: 122rpx;height: 122rpx;"></image>
					</up-upload>
				</view>
			</view>
			<view class="item adfacjb">
				<view class="left">用户名</view>
				<view class="right">
					<up-input v-model="userInfo.realName" border="none" placeholder="请输入用户名"></up-input>
				</view>
			</view>
			<!-- <view class="item adfacjb">
				<view class="left">手机号码</view>
				<view class="right">
					<up-input v-model="userInfo.phone" border="none" placeholder="请输入手机号码"></up-input>
				</view>
			</view> -->
			<view class="item adfacjb">
				<view class="left">家庭公益名称</view>
				<view class="right">
					<up-input v-model="userInfo.welfareName" border="none" placeholder="请输入家庭公益名称"></up-input>
				</view>
			</view>
			<view class="item adfacjb">
				<view class="left">家庭公益口号</view>
				<view class="right">
					<up-input v-model="userInfo.welfareSlogan" border="none" placeholder="请输入家庭公益口号"></up-input>
				</view>
			</view>
		</view>
		<view class="btn" @click="save">保存</view>
	</view>
</template>

<script setup name="">
	import { BaseApi2 } from '../common/api/baseApi';
	import CusHeader from '@/components/CusHeader/index.vue'
	import { ref, onMounted, getCurrentInstance } from 'vue'
	const { proxy } = getCurrentInstance()
	
	const fileList = ref([])
	const userInfo = ref(null)
	
	const save = () => {
		userInfo.value.avatarPath = fileList.value[0]?.url||''
		if(!userInfo.value?.avatarPath) return proxy.$showToast('请上传用户头像')
		if(!userInfo.value?.realName) return proxy.$showToast('请输入用户名')
		// if(!proxy.$reg.mobile(userInfo.value.phone)) return proxy.$showToast('请输入正确的手机号')
		if(!userInfo.value?.welfareName) return proxy.$showToast('请输入家庭公益名称')
		if(!userInfo.value?.welfareSlogan) return proxy.$showToast('请输入家庭公益口号')
		
		userInfo.value.id = uni.getStorageSync('userInfo')&&JSON.parse(uni.getStorageSync('userInfo')).id;
		proxy.$api.put('/wx/update',userInfo.value).then(({data:res})=>{
			if(res.code!==0) return proxy.$showToast(res.msg)
			proxy.$showToast('保存成功')
			setTimeout(()=>{
				uni.reLaunch({
					url:'/pages/my'
				})
			},1000)
		})
	}
	
	const deletePic = (event) => {
	  fileList.value.splice(event.index, 1);
	  userInfo.value.avatarPath = '';
	};
	
	// 新增图片
	const afterRead = async (event) => {
	  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
	  let lists = [].concat(event.file);
	  let fileListLen = fileList.value.length;
	  lists.map((item) => {
	    fileList.value.push({
	      ...item,
	      status: 'uploading',
	      message: '上传中',
	    });
	  });
	  for (let i = 0; i < lists.length; i++) {
	    const result = await uploadFilePromise(lists[i].url);
		if(result){
			let item = fileList.value[fileListLen];
			fileList.value.splice(fileListLen, 1, {
			  ...item,
			  status: 'success',
			  message: '',
			  url: result,
			});
		}
	    fileListLen++;
	  }
	};
	
	const uploadFilePromise = (url) => {
	  return new Promise((resolve, reject) => {
	    let a = uni.uploadFile({
	      url: BaseApi2+'/upload',
	      filePath: url,
	      name: 'file',
	      success: (res) => {
	        setTimeout(() => {
				let data = JSON.parse(res.data)
				if(data&&data.code===0){
					resolve(data.data);
				}else proxy.$showToast(data?.msg)
	        }, 1000);
	      },
		  fail: err =>{
			resolve('');
		  }
	    });
	  });
	};
	
	const getUserInfo = () => {
		try{
			proxy.$api.get(`/wx/${JSON.parse(uni.getStorageSync('userInfo')).id}`).then(({data:res})=>{
				if(res.code!==0) return proxy.$showToast(res.msg)
				userInfo.value = res.data;
				if(userInfo.value?.avatarPath) fileList.value = [{name:'',url:userInfo.value?.avatarPath}];
			})
		}catch(e){
			userInfo.value = null
		}
	}
	
	onMounted(()=>{
		getUserInfo()
	})
</script>

<style scoped lang="scss">
	::v-deep .u-input__content input{
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 30rpx !important;
		color: #252525 !important;
		line-height: 42rpx !important;
		text-align: right !important;
	}
	
	.common_page{
		.form{
			background: #FFFFFF;
			border-radius: 24rpx;
			padding: 14rpx 22rpx 0;
			margin-top: 20rpx;
			
			.avatar{
				.title{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #252525;
					line-height: 42rpx;
				}
				.imgbox{
					padding: 34rpx 0 47rpx;
				}
				border-bottom: 1rpx solid #E6E6E6;
			}
			.item{
				height: 110rpx;
				box-shadow: inset 0rpx -1rpx 0rpx 0rpx #E6E6E6;
				.left{
					width: 200rpx;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #252525;
					line-height: 42rpx;
					padding-right: 20rpx;
					box-sizing: border-box;
				}
				.right{
					width: calc(100% - 200rpx);
				}
			}
		}
		
		.btn{
			width: calc(100% - 80rpx);
			height: 90rpx;
			background: #B7F358;
			border-radius: 45rpx;
			font-family: PingFang-SC, PingFang-SC;
			font-weight: bold;
			font-size: 32rpx;
			color: #151B29;
			line-height: 90rpx;
			text-align: center;
			letter-spacing: 2rpx;
			position: fixed;
			left: 40rpx;
			bottom: 64rpx;
		}
	}
</style>