<template>
	<view class="content">
		<view class="content1 relative">
			<view class="relative">
				<image :src="imgList[selectIndex].dictLabel" mode="widthFix" style="width: 100%;"></image>
			</view>
			<view class="absolute card-content style1" v-if="selectIndex==0">
				<view class="left">
					<u--image :showLoading="true" :src="cardInfo.headSculpture" width="66px" height="66px" shape="circle" v-if="cardInfo.headSculpture"></u--image>
					<u--image :showLoading="true" src="/./../static/imgs/card/defaulthead.png" width="66px" height="66px" shape="circle" v-else></u--image>
				</view>
				<view class="right">
					<view class="row1">
						<text class="name">{{cardInfo.name}}</text>
						<text class="post">{{cardInfo.post}}</text>
					</view>
					<view class="row2">
						{{cardInfo.companyName}}
					</view>
					<view class="row3 flex">
						<uni-icons type="map-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.province }}{{ cardInfo.city }}{{ cardInfo.area }}
					</view>
					<view class="row4 flex">
						<uni-icons type="phone-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.phone }}
					</view>
					<!-- <view class="">
						备注
					</view> -->
				</view>
			</view>
			<view class="absolute card-content style1 flex-between" v-if="selectIndex==1">
				<view class="right">
					<view class="row1">
						<text class="name">{{cardInfo.name}}</text>
						<text class="post">{{cardInfo.post}}</text>
					</view>
					<view class="row2">
						{{cardInfo.companyName}}
					</view>
					<view class="row3 flex">
						<uni-icons type="map-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.province }}{{ cardInfo.city }}{{ cardInfo.area }}
					</view>
					<view class="row4 flex">
						<uni-icons type="phone-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.phone }}
					</view>
					<!-- <view class="">
						备注
					</view> -->
				</view>
				<view class="left">
					<u--image :showLoading="true" :src="cardInfo.headSculpture" width="66px" height="66px" shape="circle" v-if="cardInfo.headSculpture"></u--image>
					<u--image :showLoading="true" src="/./../static/imgs/card/defaulthead.png" width="66px" height="66px" shape="circle" v-else></u--image>
				</view>
			</view>
			<view class="absolute card-content style2" v-if="selectIndex==2">
					<view class="top">
						<u--image :showLoading="true" :src="cardInfo.headSculpture" width="66px" height="66px" shape="circle" v-if="cardInfo.headSculpture"></u--image>
						<u--image :showLoading="true" src="/./../static/imgs/card/defaulthead.png" width="66px" height="66px" shape="circle" v-else></u--image>
					</view>
					<view class="bottom flex">
						<view class="left">
							<view class="row1">
								<text class="name">{{cardInfo.name}}</text>
								<text class="post">{{cardInfo.post}}</text>
							</view>
							<view class="row2">
								{{cardInfo.companyName}}
							</view>
						</view>
						<view class="right">
							<view class="row1 flex">
								<uni-icons type="map-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.province }}{{ cardInfo.city }}{{ cardInfo.area }}
							</view>
							<view class="row2 flex">
								<uni-icons type="phone-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.phone }}
							</view>
						</view>
					</view>
			</view>
			<view class="absolute card-content style2" v-if="selectIndex==3">
					<view class="top">
						<view class="row1">
							<text class="name">{{cardInfo.name}}</text>
							<text class="post">{{cardInfo.post}}</text>
						</view>
						<view class="row2">
							{{cardInfo.companyName}}
						</view>
						
					</view>
					<view class="bottom flex">
						<view class="left">
						<u--image :showLoading="true" :src="cardInfo.headSculpture" width="66px" height="66px" shape="circle" v-if="cardInfo.headSculpture"></u--image>
						<u--image :showLoading="true" src="/./../static/imgs/card/defaulthead.png" width="66px" height="66px" shape="circle" v-else></u--image>
						</view>
						<view class="right">
							<view class="row1 flex">
								<uni-icons type="map-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.province }}{{ cardInfo.city }}{{ cardInfo.area }}
							</view>
							<view class="row2 flex">
								<uni-icons type="phone-filled" size="20" style="margin-right: 20rpx;" color="#5e6d82"></uni-icons>{{ cardInfo.phone }}
							</view>
						</view>
					</view>
			</view>
		</view>
		<view class="change-template">
			切换模板
		</view>
		<view class="img-list">
			<view class="img-item" v-for="(item,index) in imgList" @click="changeTemplate(index)">
				<image :src="item.dictValue" class="img-item" :key="index"></image>
				<image src="../../static/imgs/card/dh.png" class="dh" v-if="selectIndex==index"></image>
			</view>
		</view>
		<view class="change-template">
			名片信息
		</view>

		<view class="content2">
			<view class="flex flex-between row">
				<view class="left">
					名片标识
				</view>
				<u--input v-model="cardInfo.cardBusiness" border="none" placeholder="输入名片标识" inputAlign='right'></u--input>
			</view>
			<view class="flex flex-between row">
				<view class="left">
					姓名
				</view>
				<u--input v-model="cardInfo.name" border="none" placeholder="输入姓名" inputAlign='right'></u--input>
			</view>
			<view class="flex flex-between row">
				<view class="left">
					职务
				</view>
				<u--input v-model="cardInfo.post" border="none" placeholder="输入职务,不超过8个字符" inputAlign='right'></u--input>
			</view>
			<view class="flex flex-between row">
				<view class="left">
					公司/机构名称
				</view>
				<u--input v-model="cardInfo.companyName" border="none" placeholder="输入公司/机构名称" inputAlign='right'></u--input>
			</view>
			<view class="flex flex-between row" @click="placeSelect();hideKeyboard()">
				<view class="left">
					地址
				</view>
				<view v-if="cardInfo.province">{{ cardInfo.province }}{{ cardInfo.city }}{{ cardInfo.area }}</view>
				<view class="" v-else>
					<text>选择地址 </text>
					<image src="../../static/imgs/card/yjt.png" mode="" style="margin-left:10rpx;width:12rpx;height: 21rpx;"></image>
				</view>
				
			</view>
			<view class="flex flex-between row">
				<view class="left">
					详细地址
				</view>
				<u--input v-model="cardInfo.detailedAddress" border="none" placeholder="输入详细地址,不超过15个字" inputAlign='right'></u--input>
			</view>
			<view class="flex flex-between row">
				<view class="left">
					联系电话
				</view>
				<u--input v-model="cardInfo.phone" border="none" placeholder="输入联系电话" inputAlign='right'></u--input>
			</view>
			<!-- 		<u-form-item label="地址" prop="userInfo.name" borderBottom>
						<view @click="showPicker">选择地址</view>
						<view>{{ province }}{{ city }}{{ area }}</view>
					</u-form-item> -->
			<view class="flex flex-between row">
				<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>
		<view class="bottom">
			<button @click='$u.debounce(submit, 500)' class='button' type="default">提交</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
			this.getList()
		},
		data() {
			return {
				selectIndex: 0,
				imgList:[],
				// imgList: [{
				// 		dictLabel: '/./../static/1.jpg',
				// 		dictValue: '/./../static/1.jpg'

				// 	},
				// 	{
				// 		dictLabel: '/./../static/2.jpg',
				// 		dictValue: '/./../static/2.jpg'

				// 	},
				// 	{
				// 		dictLabel: '/./../static/3.jpg',
				// 		dictValue: '/./../static/3.jpg'

				// 	}, {
				// 		dictLabel: '/./../static/4.jpg',
				// 		dictValue: '/./../static/4.jpg'

				// 	}, {
				// 		dictLabel: '/./../static/5.jpg',
				// 		dictValue: '/./../static/5.jpg'

				// 	}
				// ],
				isSubmit: false,
				content: '确定提交名片信息？',
				userInfo: {},
				fileList1: [],
				cardInfo: {
					city:'',
					area:'',
					province:'',
					commonId: '',
					cardBusiness: '名片标识',
					name: '',
					post: '',
					companyName: '',
					location: '',
					detailedAddress: '',
					phone: '',
					headSculpture: '',
					currentBackground:'',
					cuttentTemplate:''
				}
			};
		},
		methods: {
			changeTemplate(index) {
				this.selectIndex = index
				this.cardInfo.currentBackground = this.imgList[index].dictLabel
				this.cardInfo.cuttentTemplate = this.imgList[index].dictSort
			},
			getList() {
				this.$request.baseRequest('admin.dictData', 'list', {
					dictType: "card_template"
				}, failres => {
					console.log('res+++++', failres.errmsg)
					this.$refs.uToast.show({
						type: 'error',
						message: failres.errmsg,
					})
					uni.hideLoading()
				}).then(res => {
					console.log("img", res)
					this.imgList = res.data.items
					if(!this.cardInfo.currentBackground&&!this.cardInfo.cuttentTemplate){
						this.cardInfo.currentBackground = this.imgList[0].dictLabel
						this.cardInfo.cuttentTemplate = this.imgList[0].dictSort
					}
				})
			},
			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>
	.content {
		padding: 20rpx;
	}

	.bgc0 {
		background: url('../../static/1.jpg') no-repeat center;
		height: 380rpx;
		border-radius: 20rpx;
	}

	.bgc1 {
		background: url('../../static/2.jpg') no-repeat center;
		height: 380rpx;
		border-radius: 20rpx;
	}

	.bgc2 {
		background: url('../../static/3.jpg') no-repeat center;
		height: 380rpx;
		border-radius: 20rpx;
	}

	.bgc3 {
		background: url('../../static/4.jpg') no-repeat center;
		height: 380rpx;
		border-radius: 20rpx;
	}

	.bgc4 {
		background: url('../../static/5.jpg') no-repeat center;
		height: 380rpx;
		border-radius: 20rpx;
	}

	.img {
		width: 100rpx;
	}

	.change-template {
		margin: 46rpx 0 30rpx 0;
		font-size: 32rpx;
		font-weight: bold;
		color: #19191A;
	}

	.img-list {
		display: block;
		white-space: nowrap;
		overflow-x: auto;

		.dh {
			width: 32rpx;
			height: 32rpx;
			position: absolute;
			right: 8rpx;
			bottom: 8rpx;
		}

		.img-item {
			position: relative;
			width: 186rpx;
			height: 120rpx;
			display: inline-block;
			margin-right: 2 0rpx;
			border-radius: 20rpx;
			margin-right: 20rpx;
		}

	}

	.content2 {
		background-color: #fff;
		border-radius: 20rpx;
		padding:0 20rpx 0 20rpx;
		.row{
			padding: 20rpx;
			border-bottom: 1px solid #E6E5E5;
		}
	}
	.button{
		color: #fff;
		background-color: #112253;
		border-radius: 20rpx;
		margin: 40rpx 0;
	}
	.card-content{
		top: 20rpx;
		left: 20rpx;
	}
	.content1{
		.style1{
			display: flex;
			padding: 20rpx 30rpx;
			width: 85%;
			.left{
				width: 132rpx;
				height: 132rpx;
				border-radius: 50%;
			}
			.right{
				margin-left: 40rpx;
				.row1{
					.name{
						font-size: 38rpx;
						font-weight: bold;
						color: #040000;
						margin-right: 20rpx;
					}
					.post{
						font-size: 26rpx;
						font-weight: 500;
						color: #666666;
					}
				}
				.row2{
					margin-top: 20rpx;
					font-size: 24rpx;
					font-weight: bold;
					color: #323333;
				}
				.row3{
					margin-top: 30rpx;
					font-size: 24rpx;
					font-weight: 500;
					color: #323333;
					
				}
			}
		}
		.style2{
				padding:30rpx;
				.top{
					.row1{
						.name{
							font-size: 38rpx;
							font-weight: bold;
							color: #040000;
							margin-right: 20rpx;
						}
						.post{
							font-size: 26rpx;
							font-weight: 500;
							color: #666666;
						}
					}
					.row2{
						margin-top: 20rpx;
						font-size: 24rpx;
						font-weight: bold;
						color: #323333;
					}
				}
				.bottom{
					margin-top: 48rpx;
					.left{
						margin-right: 40rpx;
						.row1{
							.name{
								font-size: 38rpx;
								font-weight: bold;
								color: #040000;
								margin-right: 20rpx;
							}
							.post{
								font-size: 26rpx;
								font-weight: 500;
								color: #666666;
							}
						}
						.row2{
							margin-top: 20rpx;
							font-size: 24rpx;
							font-weight: bold;
							color: #323333;
						}
					}
					.right{
						.row1,.row2{
							font-size: 24rpx;
							font-weight: 500;
							color: #323333;
						}
						.row2{
							margin-top: 20rpx;
						}
					}
				}
				
		}
	}
</style>