<template>
	<view class="content">
		<view class="content1">
			<view class="left">
				<image src="../../static/c3.png" mode="widthFix" class="img"></image>
			</view>
			<view class="right">
				<view class="row1">
					{{cardInfo.name}}|{{cardInfo.post}}
				</view>
				<view class="">
					{{cardInfo.companyName}}
				</view>
				<view class="">
					地址
				</view>
				<view class="">
					联系电话
				</view>
				<view class="">
					备注
				</view>
			</view>
		</view>
		<view class="change-template">
			切换模板
		</view>
		<view class="content2">
			<view class="">
				名片信息
			</view>
			<u--form labelPosition="left" :model="cardInfo" ref="uForm">
				<u-form-item label="名片标识" prop="cardInfo.cardBusiness" borderBottom>
					<u--input v-model="cardInfo.cardBusiness" border="none" placeholder="输入名片标识"></u--input>
				</u-form-item>
				<u-form-item label="姓名" prop="cardInfo.name" borderBottom>
					<u--input v-model="cardInfo.name" border="none" placeholder="输入姓名"></u--input>
				</u-form-item>
				<u-form-item label="职务" prop="cardInfo.post" borderBottom>
					<u--input v-model="cardInfo.post" border="none" placeholder="输入职务,不超过8个字符"></u--input>
				</u-form-item>
				<u-form-item label="公司/机构名称" prop="cardInfo.companyName" borderBottom>
					<u--input v-model="cardInfo.companyName" border="none" placholder="输入公司/机构名称"></u--input>
				</u-form-item>
				</u-form-item>
				<!-- 		<u-form-item label="地址" prop="userInfo.name" borderBottom>
					<view @click="showPicker">选择地址</view>
					<view>{{ province }}{{ city }}{{ area }}</view>
				</u-form-item> -->
				<u-form-item label="定位" prop="cardInfo.name" borderBottom @click="placeSelect();hideKeyboard()">
					<text>选择地址> </text>
				</u-form-item>
				<u-form-item label="详细地址" prop="cardInfo.name" borderBottom>
					<u--input v-model="cardInfo.detailedAddress" border="none" placeholder="输入地址,不超过15个字"></u--input>
				</u-form-item>
				<u-form-item label="联系电话" prop="cardInfo.name" borderBottom>
					<u--input v-model="cardInfo.phone" border="none" placeholder="输入联系电话"></u--input>
				</u-form-item>
			</u--form>
		</view>
		<view class="content3">
			<view class="left">
				上传企业logo或个人头像
			</view>
			<view class="right">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1"></u-upload>
			</view>
		</view>
		<view class="bottom">
			<button @click="submit">提交</button>
		</view>
		<city-picker ref="picker" mode="multiSelector" :list="areaData" :level="3" @confirm="finishSelectAddress">
		</city-picker>
		<u-modal :show="isSubmit" :content='content' @confirm="$u.debounce(confirmSubmit, 500)" showCancelButton
			@cancel="isSubmit=false" @close="isSubmit=false" closeOnClickOverlay></u-modal>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	var that;
	import uploadImage from '@/components/ossutil/uploadFile.js';
	export default {
		onLoad() {
			that = this
			this.userInfo = uni.getStorageSync("userInfo")
			this.cardInfo.commonId = this.userInfo.id
		},
		data() {
			return {
				isSubmit: false,
				content: '确定提交名片信息？',
				userInfo: {},
				fileList1: [],
				cardInfo: {
					commonId: '',
					cardBusiness: '名片标识-1',
					name: '',
					post: '',
					companyName: '',
					location: '',
					detailedAddress: '',
					phone: '',
					headSculpture: ''
				}
			};
		},
		methods: {
			placeSelect() {
				uni.chooseLocation({
					success: function(res) {
						console.log(res);
						that.cardInfo.location = res.latitude + ',' + res.longitude
						let _address = that.$helper.formatLocation(res.address)
						that.cardInfo.province = _address.Province
						that.cardInfo.city = _address.City
						that.cardInfo.area = _address.Country
						that.cardInfo.detailedAddress = _address.Village
					}
				});
			},
			hideKeyboard() {
				uni.hideKeyboard()
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(res) {
				return new Promise((resolve, reject) => {
					uploadImage(res, 'cardImages/',
						result => {
							that.cardInfo.headSculpture = result
							resolve(res)
						}
					)
				})
			},
			confirmSubmit() {
				this.$request.baseRequest('admin.unimall.cardManagementInfo', 'add', {
					cardManagementInfo: JSON.stringify(this.cardInfo)
				}, failres => {
					this.$refs.uToast.show({
						type: 'error',
						message: failres.errmsg,
					})
					uni.hideLoading()
				}).then(res => {
					this.isSubmit = false
					this.$refs.uToast.show({
						type: 'success',
						message: '提交成功！',
					})
					this.$request.baseRequest('admin.unimall.personalHomepageInfo', 'list',{
						page:1,
						limit:10,
						commonId:uni.getStorageSync("userInfo").id
					}, failres => {
						console.log('res+++++', failres.errmsg)
						this.$refs.uToast.show({
							type: 'error',
							message: failres.errmsg,
						})
						uni.hideLoading()
					}).then(res => {
						if(res.data.items.length>0){
							uni.navigateBack()
						}else{
							uni.navigateTo({
								url: '/pages/mySet/myHome'
							})
						}
					})
					
				})
			},
			submit() {
				this.isSubmit = true

			}
		}
	}
</script>

<style lang="scss" scoped>
	.img {
		width: 100rpx;
	}
</style>