<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 type="select" v-model="form.name" @click="show = true" placeholder="请选择证书类型"></u-input>
				<u-select v-model="show" mode="single-column" :list="list" @confirm="confirm"></u-select>
			</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 {getCertificate,edit,getUrl} from '../../../api/apis.js'
	export default {
		onLoad(){
			this.init()
			uni.$u.mpShare.title = '页面分享';
		},
		onReady(){
			this.$refs.uform.setRules(this.rules)
		},
		data(){
			return {
				btnStyle:{
					background:"#7D5AFC",
					color:'#FFFFFF'
				},
				show:false,
				carInfo:[],
				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,
						FCertificateEntity:[
							{
								FCertificate:{
									FNumber:''
								},
								//证书的fileid
								FAttachmentCertificateFile:''
							}
						]
					}
				},
				
			}
		},
		methods:{
			init(){
				let that = this
				let userInfo = this.$store.state.userInfo
				loading()
				if(userInfo.FCertificateEntity.length !== 0){
					console.log(userInfo.FCertificateEntity[0])
					this.form.name = userInfo.FCertificateEntity[0].FCertificate.FDataValue[0].Value
					this.form.image = userInfo.FCertificateEntity[0].FAttachmentCertificateFile
					this.params.Model.FCertificateEntity[0].FCertificate.FNumber = userInfo.FCertificateEntity[0].FCertificate.FNumber
					this.params.Model.FCertificateEntity[0].FAttachmentCertificateFile = userInfo.FCertificateEntity[0].FAttachmentCertificateFile
					loading()
					getUrl(userInfo.FCertificateEntity[0].FAttachmentCertificateFile).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)
						}
					})
				}
				getCertificate().then((res) => {
					hidden()
					if(res.data.code == 0){
						console.log("证书信息:",res)
						that.carInfo = res.data.result
					}else{
						console.log("获取证书信息失败:",res)
					}
				})
			},
			confirm(res){
				this.params.Model.FCertificateEntity[0].FCertificate.FNumber = res[0].value
				this.form.name = res[0].label
			},
			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.FCertificateEntity[0].FAttachmentCertificateFile = data.result.fileid
					this.form.image = data.result.fileid
				}
			},
			remove(){
				this.fileList = []
				this.params.Model.FCertificateEntity[0].FAttachmentCertificateFile = ''
				this.form.image = ''
			},
			save(){
				this.$refs.uform.validate(valid => {
					if(valid){
						console.log('验证通过')
						console.log(this.params)
						loading()
						edit(this.params).then((res) => {
							hidden()
							if(res.data.code == 0){
								toast('修改成功')
								uni.navigateBack()
							}else{
								toast('修改失败')
								uni.navigateBack()
							}
						})
					}else{
						console.log('验证失败')
					}
				})
			},
		},
		computed:{
			list:function(){
				let tempArray = []
				for(let i in this.carInfo){
					let tempObj = {value:'',label:''}
					tempObj.value = this.carInfo[i].FNUMBER
					tempObj.label = this.carInfo[i].fname
					tempArray.push(tempObj)
				}
				return tempArray
			},
			action:function(){
				let url = apiBaseUrl+'/api/UploadAttachments'
				return url
			}
		}
	}
</script>

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