<template>
	<view>
		<z-slide-card title="编辑个人资料" rightBtnText="保存" @rightClick="submit" :visible.sync="isvisible" @beforeClose="beforeClose">
			<z-cell label="头像" arrow @click.native="changeImage">
				<image class="head_img" :src="info.headImageSrc||'../../../static/touxiang_2.png'"></image>
			</z-cell>
			<z-cell label="姓名" :content="info.userName"></z-cell>
			<z-cell label="账号" :content="info.phone"></z-cell>
			<z-cell label="部门" content="销售部"></z-cell>
			<z-cell label="职务" :content="info.roleName"></z-cell>
		</z-slide-card>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	const app = getApp({
		allowDefault: true
	});
	export default {
		props: {
			multiple: Boolean,
			value: String | Array,
			visible: Boolean
		},
		model: {
			prop: 'value',
		},
		data() {
			return {
				isvisible: false,
				info: {
					headImageSrc: ''
				},
			}
		},
		watch: {
			visible(newval, oldval) {
				this.isvisible = newval;
			}
		},
		mounted() {
			this.info=uni.getStorageSync('storeInfo');
		},
		methods: {
			changeImage() {
				var that = this;
				uni.chooseImage({
					count: 1,
					success: function(res) {
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							//192.168.202.1:8083 api.order.ci2.cn
							url: 'http://api.order.ci2.cn/file/upload',
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								type: 1
							},
							header: {
								"Authorization": app.globalData.authorization
							},
							success: (uploadFileRes) => {
								var data = JSON.parse(uploadFileRes.data);
								if (data.status) {
									that.$set(that.info, 'headImageSrc', data.data.url)
								} else {
									this.$refs.uToast.error(data.msg)
								}

							},
							fail: (err) => {
								this.$refs.uToast.error(err)
							}
						});
					}
				});
			},
			beforeClose() {
				this.$emit('update:visible', false)
			},
			submit() {
				console.log(this.info.headImageSrc)
				console.log(this.info)
				this.$axios.post('app/user/updateUserInfo', {
					"image": this.info.headImageSrc
				}).then(res => {
					console.log(res)

					uni.setStorage({
						key: 'storeInfo',
						data: this.info
					})
					this.$emit('changeImage',this.info.headImageSrc)
					this.$emit('update:visible', false)
				}).catch(err => {
					this.$refs.uToast.error(err.msg)
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.head_img {
		width: 76rpx;
		height: 76rpx;
		border-radius: 50%;
		background: grey;
		margin-top: 10rpx;
	}
</style>
