<template>
	<view class="container">
		<u--form labelPosition="top" :model="form" :rules="rules" ref="uForm" labelWidth="120">
			<u-form-item label="姓名" prop="form.account" borderBottom>
				<u--input v-model="form.account" border="none" placeholder="请输入真实姓名"></u--input>
			</u-form-item>
			<u-form-item label="手机号" prop="form.phone" borderBottom>
				<u--input v-model="form.phone" border="none" placeholder="请输入手机号"></u--input>
			</u-form-item>
			<u-form-item label="密码" prop="form.password" borderBottom>
				<u--input v-model="form.password" border="none" placeholder="不修改密码不填此项"></u--input>
			</u-form-item>
			<u-form-item label="头像" prop="form.head" borderBottom>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1"></u-upload>
			</u-form-item>
			<u-form-item label="身份证" prop="form.card_image" borderBottom>
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
					:maxCount="2"></u-upload>
			</u-form-item>
			<u-form-item label="驾驶证" prop="form.license_image" borderBottom>
				<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
					:maxCount="1"></u-upload>
			</u-form-item>
			<u-form-item label="牵引车车牌号" prop="form.tractor_no" borderBottom>
				<u--input v-model="form.tractor_no" border="none" maxlength="11" placeholder="请输入牵引车车牌号"></u--input>
			</u-form-item>
			<u-form-item label="牵引车道运证" prop="form.tractor_image" borderBottom>
				<u-upload :fileList="fileList4" @afterRead="afterRead" @delete="deletePic" name="4" multiple
					:maxCount="1"></u-upload> </u-form-item>
			<u-form-item label="牵引车行驶证" prop="form.traactor_license_image" borderBottom>
				<u-upload :fileList="fileList5" @afterRead="afterRead" @delete="deletePic" name="5" multiple
					:maxCount="1"></u-upload> </u-form-item>
		</u--form>

		<u-button type="primary" class="btn" v-if="show" text="提交信息" shape="circle" @click="add"></u-button>

	</view>
</template>

<script>
	import config from '../../common/config';
	import {
		get,
		post
	} from '@/common/request.js'
	export default {
		data() {
			return {
				form: {
					tractor_no: "", //牵引车牌号
					head: "", //头像
					card_image: "", //身份证
					tractor_image: "", //牵引车道运证
					traactor_license_image: "", //牵引车行驶证
					license_image: "", //驾驶证
					account: "", //账号
					password: "", //密码
					nick_name: "", //昵称
					phone: "" //电话
				},
				rules: {},
				array: ['货车', '卡车', '厢式货车'],
				index: null,
				fileList1: [],
				fileList2: [],
				fileList3: [],
				fileList4: [],
				fileList5: [],
				show: true
			}
		},
		onLoad(options) {
			if (options.type == 2) {
				this.show = false
				uni.setNavigationBarTitle({
					title: '个人信息'
				});
			}
			const driver_info = config.driverInfo
			this.form = {
				...driver_info
			}
			this.form.password = ''
			this.fileList1 = this.imageArr(this.form.head)
			this.fileList2 = this.form.card_image ? this.form.card_image.split(',') : []
			this.fileList3 = this.form.license_image ? this.form.license_image.split(',') : []
			this.fileList4 = this.form.tractor_image ? this.form.tractor_image.split(',') : []
			this.fileList5 = this.form.traactor_license_image ? this.form.traactor_license_image.split(',') : []
		},
		methods: {
			async add() {

				this.form.head = this.imageStr(this.fileList1)
				this.form.card_image = this.imageStr(this.fileList2)
				this.form.license_image = this.imageStr(this.fileList3)
				this.form.tractor_image = this.imageStr(this.fileList4)
				this.form.traactor_license_image = this.imageStr(this.fileList5)

				await post('/myapi/api/yq_driver/updateDriverInfo', {
					...this.form
				}).then((res) => {
					if (res.code == 1) {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						})
						config.driverInfo = res.data
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/my/index'
							})
						}, 800)
					}
				})

			},
			imageStr(arr) {
				return arr
					.filter(item => item.url)
					.map(item => item.url)
					.join(',')
			},
			imageArr(str) {
				if (str) {
					var arr = []
					str.split(',').map(item => {
						arr.push({
							url: item
						})
					})
					return arr
				} else {
					return []
				}
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
			},
			// 删除图片
			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);
					console.log('result', result)
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "/myapi/api/yq_driver/upload",
						filePath: url,
						name: "file",
						header: {
							token: uni.getStorageSync('token'),
						},
						success: (res) => {
							const data = JSON.parse(res.data);
							resolve(data.data.fullurl);
						},
					});
				});
			},
		},
	}
</script>

<style scoped lang="less">
	.container {
		padding: 20px;

		.tag_title {
			width: 80px;
			height: 30px;
			display: flex;
			align-items: center;
		}

		.btn {
			margin-top: 20px;
		}
	}
</style>