<template>
		<view class="details">
			<!-- 设备信息 -->
			<view class="main" v-show="show">
				<u--form labelPosition="left" labelAlign="center" :model="detail" ref="form1" >
					<u-form-item label="卡片号码" prop="detail.assetNumber" labelWidth="auto" borderBottom ref="item1" >
						<u--input v-model="detail.assetNumber" border="none" placeholder="请输入卡片号码"></u--input>
					</u-form-item>
					<u-form-item label="资产名称" prop="detail.name" labelWidth="auto" borderBottom ref="item1" >
						<u--input v-model="detail.name" border="none" placeholder="请输入资产名称"></u--input>
					<!-- <u--text :text="detail.name"></u--text> -->
					</u-form-item>
					<u-form-item label="资产型号" prop="detail.specifications" labelWidth="auto" borderBottom ref="item1" >
						<u--input v-model="detail.specifications" border="none" placeholder="请输入资产型号"></u--input>
					<!-- <u--text :text="detail.specifications"></u--text> -->
					</u-form-item>
					<u-form-item label="资产品牌" prop="detail.brand" labelWidth="auto" borderBottom ref="item1" >
						<u--input v-model="detail.brand" border="none" placeholder="请输入资产品牌"></u--input>
					<!-- <u--text :text="detail.brand"></u--text> -->
					</u-form-item>
					<u-form-item
							label="使用部门"
							prop="detail.deptName"
							borderBottom
							@click="showDept = true"
							labelWidth="auto"
							ref="item1"
					>
						<u--input
								v-model="detail.deptName"
								disabled
								disabledColor="#ffffff"
								placeholder="请选择使用部门"
								border="none"
						></u--input>
						<u-icon
								slot="right"
								name="arrow-right"
						></u-icon>
					</u-form-item>
					<u-form-item
							label="资产状态"
							prop="detail.statusName"
							labelWidth="auto"
							borderBottom
							@click="showStatus = true; hideKeyboard()"
							ref="item1"
					>
						<u--input
								v-model="detail.statusName"
								disabled
								disabledColor="#ffffff"
								placeholder="请选择状态"
								border="none"
						></u--input>
						<u-icon
								slot="right"
								name="arrow-right"
						></u-icon>
					</u-form-item>
					<u-form-item label="上传照片" prop="detail.photo" labelWidth="auto" borderBottom ref="item1" >
					<u-upload
						:fileList="fileList1"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="10"
						:previewFullImage="true"
					></u-upload>
					</u-form-item>
					<u-form-item label="说明内容" prop="detail.remark" labelWidth="auto" borderBottom ref="item1" >
						<u--textarea v-model="detail.remark" placeholder="请输入说明内容" ></u--textarea>
					</u-form-item>
				</u--form>
				<u-action-sheet
						:show="showStatus"
						:actions="actions"
						title="请选择状态"
						@close="showStatus = false"
						@select="statusSelect"
				>
				</u-action-sheet>
				<u-action-sheet
						:show="showDept"
						:actions="deptActions"
						title="请选择部门"
						@close="showDept = false"
						@select="deptSelect"
				>
				</u-action-sheet>
			</view>
			
			<view class="btn" v-if="detail.detailsStatus < 2 ">
				<u-button type="primary" class="submit" text="确 认" @click="submit"></u-button>
				<u-button type="primary" class="submit" text="驳 回" @click="pdReturn"></u-button>
			</view>
			
			<view class="btn" v-if="userinfo.roleId == 1 && detail.detailsStatus == 2 ">
				<u-button type="primary" class="submit" text="驳 回" @click="pdReturn"></u-button>
			</view>
			
			
		</view>
</template>

<script>
	export default {
		data() {
			return {
				show: true,
				showStatus: false,
				detail: {
				},
				inspectionList: [],
				done: 0,
				undo: 0,
				showStatus: false,
				actions: [
					{
						name: '正常',
						value: 0
					},
					{
						name: '故障',
						value: 1
					}
				],
				fileList1: [],
				showDept: false,
				deptActions: [
				],
				userinfo: null
			}
		},
		
		onLoad: function(options) {
			this.userinfo = uni.getStorageSync('userinfo');
			this.getDept();
			let data = JSON.parse(options.zc);
			console.log("zc id = "+ data.id)
			this.detail = data;
			this.detail.statusName = data.status == 0 ? "正常" : "故障";
			let urls = this.detail.picUrl.split(",")
			for (var i = 0; i < urls.length; i++) {
				this.fileList1.push({
					url: this.API_URL + urls[i]
				})
			}
		},
		
		filters: {
		},
		
		methods: {
			// 1.1扫码
			scan() {
				var that = this;
				uni.scanCode({
					success: function(res) {
						console.log(res.result);
						that.getdeviceinfo(res.result)
					}
				})
			},
			async getdeviceinfo(id) {
				const { data:res } = await this.$http('/getdeviceinfo', {
					id: id
				});
				if(res.code) return
				console.log(res.data)
				this.show=true;
			},
			async getDept() {
				const { data:res } = await this.$http('/deptList', {});
				if(res.code) return
				this.deptActions = [];
				for (var i = 0; i < res.data.length; i++) {
					this.deptActions.push({
						name: res.data[i].deptName,
						value: res.data[i].id
					})
				}
			},
			statusSelect(e) {
				this.detail.status = e.value
				this.detail.statusName = e.name
				this.$refs.form1.validateField('detail.status')
			},
			deptSelect(e) {
				this.detail.deptName = e.name
				this.detail.deptId = e.value
				this.$refs.form1.validateField('detail.deptName')
			},// 删除图片
			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)
					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) => {
					const token = uni.getStorageSync("token");
					let a = uni.uploadFile({
						url: this.BASE_URL+'/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						success: (res) => {
							setTimeout(() => {
								let data = JSON.parse(res.data)
								console.log(data)
								resolve(data.fileName)
							}, 1000)
						}
					});
				})
			},
			submit(){
				let url = [];
				for (var i = 0; i < this.fileList1.length; i++) {
					url.push(this.fileList1[i].url.slice(this.fileList1[i].url.indexOf("/profile")))
				}
				this.detail.picUrl = url.join(",");
				// this.detail.useUserId = this.useUserId;
				// this.detail.nickName = this.nickName;
				console.log(this.detail)
				if(!this.detail.assetNumber || this.fileList1.length==0){
					return uni.showToast({
						title: "禁止空白提交",
						icon: 'error'
					})
				}
				this.pdAsset()
			},
			async pdAsset(){
				const { data:res } = await this.$http('/pdAssetDetailReSubmit', this.detail);
				if(res.code) return
				this.back()
			},
			back(){
				uni.navigateBack({ delta: 1 });
			},
			async pdReturn(){
				const { data:res } = await this.$http('/pdAssetDetailCheckNo/'+this.detail.assetDetailsId, {});
				if(res.code) return
				this.back()
			},
			async manageReturn(){
				const { data:res } = await this.$http('/pdTaskCheckNo/'+this.detail.assetDetailsId, {});
				if(res.code) return
				this.back()
			},
		}
	}
</script>

<style lang="scss">
	.details {
		background: #f6f6f6;
		padding: 2.667vw;
		height: 100vh;
		
		.btn{
			.u-button{
				margin-bottom: 1vh;
			}
			.u-form{
				margin-bottom: 1vh;
			}
		}
		
		.main {
			background: #ffffff;
			margin-bottom: 2.667vw;
			padding: 2vw;
			border-radius: 8px;
			
			.u-form-item__body__right{
				padding-left: 20vw;
			}
			.submit{
				position: absolute;
				bottom: 10vw;
				width: 100%;
			}
		}
	}
</style>