<template>
	<view class="page">
		<navbar
		title="需提交的材料"
		leftClick 
		@leftClick="$utils.navigateBack"
		/>
		
		<view class="form">
			
			<view class="form-item"
			:key="item.id"
			v-for="(item, index) in formList">
				<view class="label"
				:class="{req : item.isFill == 1}">
					{{ item.title }}
                    <br>
                    <text>{{ item.descrip }}</text>
				</view>
				
				<view class="input"
				v-if="item.type == 0">
					<input type="text" 
					:placeholder="'请输入' + item.title"
					v-model="form[item.keyName]"/>
				</view>
				
				<view class="input"
				v-else>
					<view class="upload-container">
						<uv-upload
						:fileList="fileMapList[item.keyName]" 
						:name="item.keyName"
						:maxCount="1" 
						width="120rpx"
						height="120rpx" 
						multiple 
						@afterRead="afterRead" 
						@delete="deleteImage">
							<view class="upload">
								<image :src="item.image"
								mode="aspectFit"
								style="width: 120rpx;height: 120rpx;" />
							</view>
						</uv-upload>
						<view class="download-btn" 
						v-if="item.template"
						@click="downloadTemplate(item.template, item.title)">
							<text>下载模板</text>
						</view>
					</view>
				</view>
				<view class="icon"
				v-if="item.type == 1">
					<uv-icon
					name="arrow-right"
					size="30rpx"
					></uv-icon>
				</view>
			</view>
			
		</view>
			
		
		<view class="uni-color-btn" 
		v-if="formList.length > 0"
		@click="addMaterial()">
			提交
		</view>
		
		<view class="uni-uncolor-btn"
		@click="$refs.customerServicePopup.open()">
			联系客服
		</view>
		
		<view class="tips"
		style="text-align: center;padding: 20rpx 0;
		width: 750rpx;">
			如有疑问请联系客服
		</view>
		
		<customerServicePopup ref="customerServicePopup"/>
	</view>
</template>

<script>
	import customerServicePopup from '@/components/config/customerServicePopup.vue'
	export default {
		components : {
			customerServicePopup
		},
		data() {
			return {
				form : {
				},
				fileMapList : {},
				formList : [],
				id : 0,
				verification : {},
			}
		},
		onLoad({id}) {
			this.id = id
			this.queryCertById()
		},
		methods: {
			deleteImage(e){
				this.fileMapList[e.name].splice(e.index, 1)
			},
			afterRead(e){
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self.fileMapList[e.name].push({
							url
						})
					})
				})
			},
			addMaterial(){
				
				this.formList.forEach(n => {
					if(n.type == 1){
						this.form[n.keyName] = this.fileMapList[n.keyName]
						.map((item) => item.url).join(",")
					}
				})
				
				if(this.$utils.verificationAll(this.form, this.verification)) {
					return
				}
				this.$api('addMaterial',this.form, res =>{
					if(res.code == 200){
						uni.showToast({
							title:'提交成功！等待审核',
							icon: 'none'
						})
						setTimeout(uni.navigateBack, 1000, -1)
					}
				})
			},
			downloadTemplate(template, title) {
				if (!template) {
					uni.showToast({
						title: '暂无模板文件',
						icon: 'none'
					})
					return
				}
				
				uni.downloadFile({
					url: template,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.openDocument({
								showMenu: true,
                                filePath: res.tempFilePath,
                                success: () => {
                                    console.log('文件打开成功')
                                },
                                fail: (err) => {
                                    console.log('文件打开失败', err)
                                }
                            })
						}
					},
					fail: () => {
						uni.showToast({
							title: '下载失败',
							icon: 'none'
						})
					}
				})
			},
			queryCertById(){
				this.$api('queryCertById', {
					cerId : this.id,
				}, res => {
					if(res.code == 200){
						this.formList = res.result.employMaterialList
						
						res.result.employMaterialList.forEach(n => {
							if(n.isFill != 1){
								return
							}
							if(n.type == 0){
								this.verification[n.keyName] = '请输入' + n.title
								this.form[n.keyName] = ''
							}else{
								this.verification[n.keyName] = '请上传' + n.title
								// this.fileMapList[n.keyName] = []
								this.$set(this.fileMapList, n.keyName, [])
							}
						})
						
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
.page{
	min-height: 100vh;
	.required::before{
		content: '*';
		color: #f00;
	}
	.form-item{
		padding: 30rpx 0;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #00000009;
		font-size: 28rpx;
		.label{
			font-weight: 600;
			margin-right: 50rpx;
			text{
				font-size: 24rpx;
				font-weight: 500;
			}
			&.req::before{
				content: '*';
				color: #f00;
			}
		}
		.input{
			margin-left: auto;
			flex-shrink: 0;
			image{
				
			}
			input{
				text-align: right;
			}
		}
		.icon{
			margin-left: 10rpx;
		}
	}
	.tips{
		font-size: 26rpx;
		color: #777;
		padding-bottom: 20rpx;
	}
	.form {
		padding: 0 30rpx;
		background-color: #fff;
		margin: 20rpx;
		border-radius: 20rpx;
		.upload-container {
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 10rpx;
		}
		.upload{
			display: flex;
			justify-content: center;
			align-items: center;
			// width: 690rpx;
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			overflow: hidden;
			background-color: #f3f3f3;
			border-radius: 10rpx;
			// .btn-add{
			// 	margin: auto;
			// 	padding: 10rpx 20rpx;
			// 	background-color: $uni-color;
			// 	color: #fff;
			// 	border-radius: 10rpx;
			// }
		}
		.download-btn {
			padding: 8rpx 16rpx;
			background-color: #007aff;
			color: #fff;
			border-radius: 6rpx;
			font-size: 24rpx;
			text {
				color: #fff;
			}
		}
	}
}
</style>
