<template>
	<view>
		<view v-if='!editstatus&&fileList.length>0' class="swiper-wrap">
			<u-swiper :list="fileList" indicator indicatorMode="dot"
			autoplay
			imgMode='aspectFit'
			height='300'
		    @change="change" @click="click">
			</u-swiper>	
		</view>
		<!-- <u-skeleton
			    rows="3"
			    title
				loading
		></u-skeleton> -->
		<view class="company-title">
			<view v-if='editstatus'>公司/机构名称</view>
		 	<u--input v-if='editstatus' v-model="companyData.companyName"  placeholder="请输入公司名称" border="none" ></u--input>
			<view v-else>{{companyData.companyName}}</view>
		</view>
		<view class="intro">
			<view v-if='editstatus'>公司/机构简介</view>
			<u--input type='textarea' v-if='editstatus' v-model="companyData.companyIntroduction" placeholder="请输入公司简介" border="none" ></u--input>
			<view v-else>{{companyData.companyIntroduction}}</view>
		</view>
		<view class="scope">
			<view>业务范围</view>
			<u--input type='textarea' v-if='editstatus' v-model="companyData.scopeOfBusiness" placeholder="输入业务范围，不超过500个字" border="none" ></u--input>
			<view v-else>{{companyData.scopeOfBusiness}}</view>
		</view>
		<u-list>
			<u-list-item>
				<view v-if='editstatus'>公司地址</view>
				<u--input v-if='editstatus' v-model="companyData.companyAddress" placeholder="输入地址" border="none" ></u--input>
				<view v-else>{{companyData.companyAddress}}</view>
			</u-list-item>
			<u-list-item>
				<view v-if='editstatus'>联系电话</view>
				<u--input v-if='editstatus' v-model="companyData.phone" placeholder="输入联系电话" border="none" ></u--input>
				<view v-else>{{companyData.phone}}</view>
			</u-list-item>
			<u-list-item>
				<view v-if='editstatus'>传真</view>
				<u--input v-if='editstatus' v-model="companyData.portraiture" placeholder="输入传真号" border="none" ></u--input>
				<view v-else>{{companyData.portraiture}}</view>
			</u-list-item>
			<u-list-item>
				<view v-if='editstatus'>邮箱</view>
				<u--input v-if='editstatus' v-model="companyData.postbox" placeholder="输入邮箱" border="none" ></u--input>
				<view v-else>{{companyData.postbox}}</view>
			</u-list-item>
		</u-list>
		<view v-if='editstatus'>
			<view>企业风采</view>
			<!-- <image v-for='item in fileList' :src="item.url" mode=""></image> -->
			<u-upload
					@afterRead="afterRead"
					:fileList="fileList1"
					@delete="deletePic"
					name="1"
					multiple
					:maxCount="10"
				></u-upload>
		</view>
		<view class="footer">
			<view v-if='!editstatus' @click='edit' class='button'>编辑</view>
			<view v-else @click='$u.debounce(buttonsubmit, 500)' class='button'>提交</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import uploadImage from '@/components/ossutil/uploadFile.js'; 
	export default {
		data() {
			return {
				companyData:{
					commonId:''
				},
				userInfo:{},
				fileList:[],
				fileList1:[],
				editstatus:false,
			}
		},
		onLoad() {
			uni.showLoading({
					title: '数据加载中'
				})
				this.getList()
		},
		onShow() {
				this.userInfo = uni.getStorageSync("userInfo")
				this.companyData.commonId=uni.getStorageSync("userInfo").id
				console.log(1111111111)
				
			
			// this.getList()
			
		},
		methods: {
			getList(){
				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 => {
					console.log(res)
					if(res.data.items.length>0){
						this.companyData=res.data.items[0]
						if(res.data.items[0].addressUrl){
							var arr=res.data.items[0].addressUrl.split(',')
							this.fileList1=[]
							for(var i=0;i<arr.length;i++){
								if(arr[i]){
									this.fileList1.push({url:arr[i]})
								}
							}
							this.fileList=arr
						}
						console.log(this.fileList1)
						this.editstatus=false
					}else{
						this.editstatus=true
					}
					// if (res.errno == 200) {
					uni.hideLoading()
					
					// }
				})
			},
			edit(){
				this.editstatus=true
			},
			change(){
				
			},
			click(index){
				console.log(index)
				uni.previewImage({
						current: index,     // 当前显示图片的索引值
						urls: this.fileList,    // 需要预览的图片列表，photoList要求必须是数组
						loop:true,          // 是否可循环预览
					})
			},
			buttonsubmit(){
				uni.showLoading({
					title: '数据加载中',
					mask:true
				})
				if(this.companyData.id){
					this.$request.baseRequest('admin.unimall.personalHomepageInfo', 'update',{
						personalHomepageInfo:JSON.stringify(this.companyData)
					}, failres => {
						console.log('res+++++', failres.errmsg)
						this.$refs.uToast.show({
							type: 'error',
							message: failres.errmsg,
						})
						uni.hideLoading()
					}).then(res => {
						// if (res.errno == 200) {
						uni.hideLoading()
						this.$refs.uToast.show({
							type: 'success',
							message: '编辑成功',
						})
						this.getList()
						// }
					})
				}else{
					this.$request.baseRequest('admin.unimall.personalHomepageInfo', 'add',{
						personalHomepageInfo:JSON.stringify(this.companyData)
					}, failres => {
						console.log('res+++++', failres.errmsg)
						this.$refs.uToast.show({
							type: 'error',
							message: failres.errmsg,
						})
						uni.hideLoading()
					}).then(res => {
						// if (res.errno == 200) {
						uni.hideLoading()
						this.$refs.uToast.show({
							type: 'success',
							message: '提交成功',
						})

						this.getList()
						
						// }
					})
				}
				
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.companyData.addressUrl =this[`fileList${event.name}`].toString()
			},
			// 新增图片
			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: '上传中'
				     })
				    })
				// console.log(event,111111,fileListLen)
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.companyData.addressUrl = this.companyData.addressUrl?this.companyData.addressUrl+','+result:result
					this[`fileList${event.name}`][fileListLen]={}
					let item = this[`fileList${event.name}`][fileListLen]
					console.log(item)
					 this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					  status: 'success',
					  message: '',
					  url: result
					 }))
					 fileListLen++
				}
				console.log(this[`fileList${event.name}`])
			},
			uploadFilePromise(url) {
				console.log(url)
				return new Promise((resolve, reject) => {
					uploadImage(url, 'cardImages/',
						result => {
							console.log(result,22222)
							
							resolve(result)
						}
					)
					// let a = uni.uploadFile({
					// 	url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
					// 	filePath: url,
					// 	name: 'file',
					// 	formData: {
					// 		user: 'test'
					// 	},
					// 	success: (res) => {
					// 		setTimeout(() => {
					// 			resolve(res.data.data)
					// 		}, 1000)
					// 	}
					// });
				})
			},
		}
	}
</script>

<style>

</style>
