<template>
	<view class="info">
		<u-top-tips ref="uTips"></u-top-tips>
		<u-form :model="form" ref="uform" label-width="150">
			<u-form-item label="平台名称" prop="name" :required="true">
				<u-input v-model="form.name" placeholder="请输入其他平台名称"></u-input>
			</u-form-item>
			<u-form-item label="上传图片" prop="image" :required="true">
				<u-upload
				:file-list="fileList"
				:action="action"
				:header="header"
				@on-success="uploaded"
				@on-error="uploaded"
				@on-remove="remove"
				max-count="1"></u-upload>
			</u-form-item>
		</u-form>
		<view class="btn">
			<u-button hover-class="none" shape="circle" :custom-style="btnStyle" @click="save">保存</u-button>
		</view>
	</view>
</template>

<script>
	import {apiBaseUrl} from '../../../api/request.js'
	import {loading,hidden,toast} from '../../../tips/myTips.js'
	import {edit,getUrl} from '../../../api/apis.js'
	export default {
		onLoad(){
			uni.$u.mpShare.title = '页面分享';
			this.init()
		},
		onReady(){
			this.$refs.uform.setRules(this.rules)
		},
		data(){
			return {
				btnStyle:{
					background:"#7D5AFC",
					color:'#FFFFFF'
				},
				form:{
					name:'',
					image:''
				},
				rules:{
					name:[
						{
							required:true,
							message: '请输入其他平台名称',
							trigger: 'blur,change'
						}
					],
					image:[
						{
							required:true,
							message: '请上传图片',
							trigger: 'blur,change'
						}
					]
				},
				fileList:[],
				header:{test:11},
				params:{
					IsDeleteEntry:true,
					Model:{
						FID:this.$store.state.userInfo.Id,
						FPlatformName:'',
						FPlatformImage:''
					}
				},
			}
		},
		methods:{
			init(){
				let that = this
				let userInfo = this.$store.state.userInfo
				
				this.form.name = userInfo.FPlatformName
				this.params.Model.FPlatformName = userInfo.FPlatformName
				if(userInfo.FPlatformImage){
					this.form.image = userInfo.FPlatformImage
					this.params.Model.FPlatformImage = userInfo.FPlatformImage
					loading()
					getUrl(userInfo.FPlatformImage).then((res) => {
						hidden()
						if(res.data.code == 0){
							console.log("获取第三方凭证图片:",res)
							let tempObj = {url:''}
							tempObj.url = res.data.result[0].fileurl
							that.fileList.push(tempObj)
						}else{
							console.log("获取第三方凭证图片失败:",res)
						}
					})
				}
			},
			uploaded(data, index, lists, name){
				if(data.code == 100){
					console.log("上传失败:",data)
					this.$refs.uTips.show({
						title:data.message,
						type:'error'
					})
					lists.splice(index,1)
				}
				if(data.code == 0){
					console.log("上传成功:",data)
					this.params.Model.FPlatformImage = data.result.fileid
					this.form.image = data.result.fileid
				}
			},
			remove(){
				this.fileList = []
				this.params.Model.FPlatformImage = ''
				this.form.image = ''
			},
			save(){
				this.$refs.uform.validate(valid => {
					if(valid){
						console.log('验证通过')
						this.params.Model.FPlatformName = this.form.name
						loading()
						edit(this.params).then((res) => {
							hidden()
							if(res.data.code == 0){
								toast('修改成功')
								uni.navigateBack()
							}else{
								toast('修改失败')
								uni.navigateBack()
							}
						})
					}else{
						console.log('验证失败')
					}
				})
			}
		},
		computed:{
			action:function(){
				let url = apiBaseUrl+'/api/UploadAttachments'
				return url
			}
		}
	}
</script>

<style>
	.info{
		padding: 5%;
	}
</style>
