<template>
	<view class="app">
		<view class="Commodity" @tap="uploadImag">
			<image class="picture" :src="myimg" mode=""></image>
			<view class="">
				点击编辑头像
			</view>
		</view>
		<view class="centercontent" v-if="appPersonallist">
			<view class="content">
				<view class="name">
					<span>姓名：</span>
					<input type="text" maxlength="10" placeholder="请输入" v-model="appPersonallist.nickName" disabled />
				</view>
				<view class="name">
					<span>性别：</span>
					<text v-if='appPersonallist.sex'> {{appPersonallist.sex}}
					</text>
				</view>
				<view class="name">
					<span>年龄：</span>
					<input type="text" maxlength="4" placeholder=" " v-model="appPersonallist.age" disabled />
				</view>
				<!-- <view class="name">
					<span class="">
						账号：
					</span>
					<input type="text" disabled maxlength="10" placeholder="请填写" v-model="appPersonallist.userName" />
				</view> -->
				<view class="name">
					<span>手机号：</span>
					<input disabled type="text" maxlength="11" placeholder="请输入" v-model="appPersonallist.phonenumber"
						disabled />
				</view>
				<view class="nursetype">
					<span>职称级别：</span>
					<text v-if="appPersonallist.positionalTitleLevel">
						{{appPersonallist.positionalTitleLevel}}
					</text>
				</view>
				<view class="nursetype" style="">
					<span>家庭住址：</span>
					<text v-if="appPersonallist.address"> {{appPersonallist.address}}
					</text>
				</view>
				<view class="nursetype " style="">
					护理站：
					<text v-for='(uitem,index) in appPersonallist.nurseStationSysUserVOList'> {{uitem.nurseStationName}}
						<text v-if="index!=appPersonallist.nurseStationSysUserVOList.length-1">，</text>
					</text>
					<!-- {{getNurseStationName(appPersonallist)}} -->
				</view>
				<view class="nursetype " style="">
					擅长项目：
					<text v-if="appPersonallist.geniusItem"> {{appPersonallist.geniusItem}}
					</text>
				</view>
				<view class="nursetype " style="border: none;">
					证书上传：
					<view class="upload" @tap='uploadcertificateimg' v-if="certificateimg.length==0">
						<view class="">
							<u-icon name="plus" color='#E6E6E6' label='拍摄上传' label-color='#E6E6E6'></u-icon>
						</view>
					</view>
					<view class="rate" v-else>
						<view class="itemimgs" v-for="(item,index) in certificateimg">
							<image class="delimg" src="../../static/gb2.png" mode="" @tap='delimg(item)'>
							</image>
							<image class="itemimg" :src="item" mode=""></image>
						</view>
						<view class="item" @tap='uploadcertificateimg'>
							<image src="../../static/paishe.png" mode=""></image>
							<view class="title">
								拍摄上传
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="finish" @tap="uploadcertificateimgs">保存</view>
		<u-select v-model="sexshow" :list="sexlist" mode="single-column" @confirm="sexconfirm"></u-select>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import {
		updateNursePersonCheck,
		personNurseStationLists
	} from '@/api/Modifyinformation/Modifyinformation.js';
	import {
		nurseAppletPersonCenter,
	} from '@/api/personnal/personal.js';
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				timer: null, //timer
				nursePersonId: 34, //护理员id
				sexshow: false, //性别选择
				baseurl: '', //url
				sexlist: [{
						value: 'MALE',
						label: '男'
					},
					{
						value: 'FEMALE',
						label: '女'
					}
				],
				appPersonallist: {
					nurseStationPersonCheckList: []
				}, //用户信息
				image: null, //判断是否上传头像
				certificateimg: [], //证书img
				myimg: null, //头像img
			}
		},
		computed: {
			// getNurseStationName() {
			// 	return data => data?.nurseStationSysUserVOList?. [0]?.nurseStationName || ''
			// },
		},
		onShow() {
			this.baseurl = baseurl;
		},
		onLoad(options) {
			let that = this
			// const value = uni.getStorageSync('nursePersonId');
			// if (value) {
			// that.nursePersonId= value
			that.myInfo()
			// }
		},
		methods: {
			//删除证书图片
			delimg(item) {
				this.certificateimg = this.certificateimg.filter(e => e != item)
			},
			//上传证书
			uploadcertificateimg() {
				var that = this;
				uni.chooseImage({
					count: 9,
					success(res) {
						res.tempFilePaths.forEach(e => {
							that.certificateimg.push(e)
						})
					}
				})
			},
			//上传头像
			uploadImag() {
				var that = this;
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success(res) {
						that.appPersonallist.avatar = res.tempFilePaths[0]
						that.myimg = res.tempFilePaths[0]
						that.image = true
					}
				})
			},
			//个人信息
			myInfo() {
				nurseAppletPersonCenter(this.nursePersonId).then(res => {
					res.data.sex == 'MALE' ? res.data.sex = '男' : ''
					res.data.sex == 'FEMALE' ? res.data.sex = '女' : ''
					this.myimg = baseurl + res.data.avatar
					res.data.positionalTitleLevel == 'PRIMARY_LEVEL' ? res.data.positionalTitleLevel = '初级' : ''
					res.data.positionalTitleLevel == 'MIDDLE_LEVEL' ? res.data.positionalTitleLevel = '中级' : ''
					res.data.positionalTitleLevel == 'HIGH_LEVEL' ? res.data.positionalTitleLevel = '高级' : ''
					res.data.nurseStationPersonCertificateList.forEach(e => {
						e.certificateUrl = baseurl + e.certificateUrl
						this.certificateimg.push(e.certificateUrl)
					})
					res.data.id = this.nursePersonId
					res.data.nurseStationPersonCheckList = []
					this.appPersonallist = res.data
				})
			},
			//上传证书
			uploadcertificateimgs() {
				let that = this
				if (this.certificateimg.length > 0) {
					this.certificateimg.forEach(e => {
						uni.uploadFile({
							url: baseurl +
								'/nurseApplet/personCenter/uploadNurseStationPersonCertificateUrl',
							filePath: e, //file: 二进制(filePath)
							name: 'file',
							formData: { //多余值
								'nursePersonId': that.nursePersonId,
							},
							timeout: 5000,
							success(res) {
								var img = {
									certificateUrl: JSON.parse(res.data).imgUrl
								}
								that.appPersonallist.nurseStationPersonCheckList.push(img)
								if (that.appPersonallist.nurseStationPersonCheckList.length == that
									.certificateimg.length) {
									that.gofinish();
								}
							}
						})
					})
				} else {
					that.gofinish();
				}
			},
			//修改
			gofinish() {
				var that = this
				if (this.image) {
					uni.uploadFile({
						url: baseurl + '/nurseApp/personLogin/updateHeadAvatarHead',
						filePath: that.myimg, //file: 二进制(filePath)
						name: 'file',
						formData: { //多余值
							'userId': that.appPersonallist.userId,
						},
						timeout: 5000,
						success(res) {
							that.appPersonallist.avatar = JSON.parse(res.data).imgUrl
							that.updateNursePerson();
						}
					})
				} else {
					that.updateNursePerson();
				}
			},
			updateNursePerson() {
				var that = this
				var obj = {
					avatar: that.appPersonallist.avatar,
					userId: that.appPersonallist.userId,
					id: that.nursePersonId,
					positionalTitleLevelCheck: that.appPersonallist.positionalTitleLevel,
					geniusItemCheck: that.appPersonallist.geniusItem,
					nurseStationPersonCheckList: that.appPersonallist.nurseStationPersonCheckList
				}
				updateNursePersonCheck(obj).then(res => {
					if (res.code == 200) {
						that.myInfo();
						that.$refs.uToast.show({
							title: '修改成功',
							type: 'success',
							duration: '1500',
						})
						if (that.timer) {
							clearTimeout(that.timer)
						}
						that.timer = setTimeout(e => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
					} else {
						that.$refs.uToast.show({
							title: res.msg,
							type: 'error'
						})
					}
				})
			},

			confirm(e) {
				this.appPersonallist.nurseStationSysUserVOList[0].nurseStationId = e[0].value
				this.appPersonallist.nurseStationSysUserVOList[0].nurseStationName = e[0]['label']
			},
			//开启选择性别
			sexshowtrue() {
				this.sexshow = true
			},
			//选择性别
			sexconfirm(e) {
				this.appPersonallist.sex = e[0].label
			},
		},
	}
</script>
<style lang="scss">
	@import './Modifyinformation.scss';
</style>
