<style lang='scss' scoped>
	.addRecord{
		width: 100%;
		padding: $paddingTB $paddingLR;
		:not(not) {
			box-sizing: border-box;
		}
		
	}
	
</style>
<template>
	<view class="addRecord">
		<u-form :model="form" label-width="150">
			<!-- <u-form-item label="所属项目" prop="name">
				<u-input v-model="form.projectName" />
			</u-form-item> -->
			<u-form-item label="设备号" prop="intro">
				<u-input v-model="form.deviceId" />
			</u-form-item>
			<u-form-item label="设备名称" prop="intro">
				<u-input v-model="form.deviceName" />
			</u-form-item>
			<u-form-item label="维保时间" prop="intro" :rightIconStyle="{color: '#888', fontSize: '32rpx'}" right-icon="calendar">
				<u-input :disabled="true" v-model="form.createTime" @click="openTimePickerFn" right-icon="" />
				<u-picker mode="time" :params="params" v-model="timePickerShow" @confirm="timeOnConfirmFn"></u-picker>
			</u-form-item>
			<u-form-item label="维保内容" prop="intro">
				<u-input v-model="form.maintenanceContext" type="textarea" :auto-height="true"/>
			</u-form-item>
			<u-form-item label="处理人" prop="handlerId">
				<u-input :disabled="true" v-model="form.handlerName" @click="show" />
				<u-picker v-model="showHandler" mode="selector" :range="handlerList" range-key="nickName" @confirm="setHandler"></u-picker>
			</u-form-item>
			<!-- <u-form-item label="故障内容" prop="intro">
				<u-input v-model="form.faultType" type="textarea" :auto-height="true"/>
			</u-form-item>
			<u-form-item label="处理结果" prop="intro">
				<u-input v-model="form.dealResult" type="textarea" :auto-height="true"/>
			</u-form-item>
			<u-form-item label="意见建议" prop="intro">
				<u-input v-model="form.advice" type="textarea" :auto-height="true"/>
			</u-form-item> -->
			<u-form-item label="现场图片" prop="photo">
				<!-- <u-upload width="160"></u-upload> -->
				<u-upload
					width="160"
					ref="uUpload"
					max-count="1" 
					:action="action" 
					:header="uploadHeader"
					:size-type="['compressed']"
					:max-size="1024*1024"
					@on-success="onUploadSuccessFn"
				></u-upload>
			</u-form-item>
			<!-- <u-form-item label="维保后图片" prop="photo">
				<u-upload width="160"></u-upload>
			</u-form-item> -->
			<u-button type="primary" @click="add">提交</u-button>
		</u-form>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				params:{
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true,
					timestamp: true, // 1.3.7版本提供
				},
				timePickerShow:false,
				form:{
					projectName:"", // 所属项目
					deviceId:"", // 设备号
					deviceName: "",
					createTime:"", // 维保时间
					maintenanceContext:"", // 维保内容
					faultType:"", // 故障内容
					dealResult:"", // 处理结果
					advice:"", // 意见建议
					scenePictures:"", // 维保前图片 现场图片
					// maintenanceAfterImg:"" // 维保后图片
				},
				action: '', // 文件上传地址
				uploadHeader: {},
				editImgDataStr:"",
				uploadCode: -1,
				handlerList: [],
				showHandler: false,
			}
		},
		onLoad(e) {
			this.action = this.$config.baseUrl + "/common/upload";
			const userToken = uni.getStorageSync('userToken');
			this.uploadHeader = {
				"Authorization" :userToken,
				"Content-Type":"multipart/form-data"
			}
			this.form.createTime = this.$u.timeFormat(new Date().getTime(), 'yyyy-mm-dd hh:MM:ss');
			console.log(e,"eeeeeeeeeeeee");
			let deviceInfo = JSON.parse(e.deviceInfo);
			Object.assign(this.form, deviceInfo);
			this.getHandlerList()
		},
		methods: {
			timeOnConfirmFn(e){
				console.log(e,"e")
				this.form.createTime = this.$u.timeFormat(e.timestamp, 'yyyy-mm-dd hh:MM:ss')
			},
			openTimePickerFn(){
				this.timePickerShow = true;
			},
			add() {
				let userType = uni.getStorageSync("userType");
				let url = "/app/maintenance";
				this.$post(url, this.form).then((res) => {
					if (res.code === 200) {
						this.$u.toast('添加成功', 3000);
						setTimeout(() => {
							uni.navigateBack();
						}, 500);
					}
				}).catch((err) => {
					console.log(err)
				})
			},
			onUploadSuccessFn(data, index, lists, name){
				console.log(lists,"成功");
				this.uploadCode = lists[0].response.code;
				this.form.scenePictures = lists[0].response.fileName;
			},
			show() {
				this.showHandler = true;
			},
			setHandler(e) {
				let index = e[0];
				let obj = this.handlerList[index];
				this.form.handlerId = obj.userId;
				this.form.handlerName = obj.nickName;
			},
			getHandlerList() {
				this.$get('/app/maintenance/maintenance-user').then((res) => {
					if (res.code === 200) {
						this.handlerList = [];
						for (let item of res.data) {
							let { userId, nickName } = item;
							this.handlerList.push({
								userId,
								nickName
							})
						}
					}
				}).catch((err) => {
					console.log(err)
				})
			}
		}
	}
</script>
