<template>
	<view>
		<view class="form-area">
			<uni-forms
				:modelValue="returnForm[0]"
				label-position="top"
			>
				<uni-forms-item
					label="维修数量"
					v-show="ifNo == 0"
				>
					<uni-easyinput type="number" placeholder="请输入" v-model="returnForm[0].repairNum"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="填写配件"
				>
					<uni-easyinput placeholder="请输入配件" v-model="returnForm[0].partName"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="是否收费"
				>
					<uni-data-checkbox v-model="returnForm[0].partType" :localdata="feeRange"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item
					label="配件数量"
				>
					<uni-easyinput placeholder="请输入" type="number" v-model="returnForm[0].partNum"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="配件单价"
				>
					<uni-easyinput placeholder="请输入" type="number" v-model="returnForm[0].partPrice"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item
					label="维修厂家"
				>
					<uni-data-select
					      v-model="returnForm[0].supplierId"
					      :localdata="factoryRange"
					    ></uni-data-select>
				</uni-forms-item>
				<uni-forms-item
					label="维修内容"
				>
					<uni-easyinput placeholder="请输入" v-model="returnForm[0].repairContent"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<!-- <view class="btn-area">
				<view
					@click="addForm"
					style="background-color: #3788FF; color: #fff;"
				>
					添加配件
				</view>
				<view
					@click="subForm"
					style="border: 1px solid #EBEBEB;"
				>
					维修完成
				</view>
			</view> -->
			<button @click="subForm">维修完成</button>
		</view>
		<u-loading-page :loading="showLoading" color="#000" loading-text="表单提交中,请稍后..."></u-loading-page>
	</view>
</template>

<script>
import { basePath } from '../../public'
	export default {
		data() {
			return {
				formIndex: 0,
				ifNo: '',
				showLoading: false,
				returnForm: [
					{
						repairNum: 1,
						partName: '',
						partType: '',
						partNum: '',
						partPrice: '',
						supplierId: '',
						repairContent: ''
					}
				]
				,
				feeRange: [
					{ text: '否', value: '0' },
					{ text: '是', value: '1' }
				],
				factoryRange: [
					
				],
				totalSub: {},
				id: '',
				taskId: '',
				maId: '',
				typeId: ''
			}
		},
		methods: {
			/* addForm () {
				this.formIndex++
				if (this.formIndex >= 6) {
					uni.showToast({
						icon: 'none',
						title: '无法再添加！'
					})
				} else {
					this.allForm.push({
						partName: '',
						partType: '',
						partNum: '',
						partPrice: '',
						supplierId: '',
						repairContent: ''
					})
				}
			}, */
			subForm () {
				let that = this
				this.totalSub.partStrList = JSON.stringify(this.returnForm) 
				this.totalSub.id = this.id
				this.totalSub.taskId = this.taskId
				this.totalSub.maId = this.maId
				this.totalSub.typeId = this.typeId
				this.totalSub.companyId = uni.getStorageSync('userInfo').sysUser.companyId
				this.totalSub.userId = uni.getStorageSync('userInfo').userid
				this.totalSub.repairType = 2
				console.log(this.totalSub);
				this.showLoading = true
				uni.request({
					url: basePath + '/repair/submitRepairApply',
					method: 'POST',
					header: {
						'content-type': 'application/json',
						'Authorization': uni.getStorageSync('token')
					},
					data: this.totalSub,
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								icon: 'none',
								title: res.data.msg,
								success: () => {
									that.showLoading = false
									uni.switchTab({
										url: '/pages/workSpace/workSpace'
									})
								}
							})
						} else {
							that.showLoading = false
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					}
				})
			},
			handleTree (data, id, parentId, children) {
			  let config = {
			    id: id || 'id',
			    parentId: parentId || 'parentId',
			    childrenList: children || 'children'
			  };
			
			  var childrenListMap = {};
			  var nodeIds = {};
			  var tree = [];
			
			  for (let d of data) {
			    let parentId = d[config.parentId];
			    if (childrenListMap[parentId] == null) {
			      childrenListMap[parentId] = [];
			    }
			    nodeIds[d[config.id]] = d;
			    childrenListMap[parentId].push(d);
			  }
			
			  for (let d of data) {
			    let parentId = d[config.parentId];
			    if (nodeIds[parentId] == null) {
			      tree.push(d);
			    }
			  }
			
			  for (let t of tree) {
			    adaptToChildrenList(t);
			  }
			
			  function adaptToChildrenList(o) {
			    if (childrenListMap[o[config.id]] !== null) {
			      o[config.childrenList] = childrenListMap[o[config.id]];
			    }
			    if (o[config.childrenList]) {
			      for (let c of o[config.childrenList]) {
			        adaptToChildrenList(c);
			      }
			    }
			  }
			  return tree;
			},
			veri (data) {
				let that = this
				if (!data || data.length <= 0) {
					return null;
				}
				return data.map(x => {
					/* let model
					if (x.id.includes('u')) {
						model = {
							name: x.name,
							id: x.userId,
						};
					} else {
						model = {
							name: x.name,
							id: x.id
						};
					} */
					const model = {
						name: x.paName,
						id: x.paId
					}
				    
				    const children = that.veri(x.children);
						
				    if (children) { 
					    model.children = children;
				    }
					
					return model;
				});
			}
		},
		onShow() {
			let that = this
			// 获取维修厂家
			uni.request({
				url: basePath + '/supplierInfo/getSupplierSelect',
				method: 'GET',
				header: {
					'content-type': 'application/json',
					'Authorization': uni.getStorageSync('token')
				},
				data: {},
				success: (res) => {
					if (res.data.code == 200) {
						that.factoryRange = res.data.data.map((item) => {
							return {
								text: item['supplier'],
								value: item['supplierId']
							}
						})
					}
				}
			})
		},
		onLoad(params) {
			this.id = params.id
			this.taskId = params.taskId
			this.maId = params.maId
			this.typeId = params.typeId
			this.ifNo = params.ifNo
		}
	}
</script>

<style lang="scss">
	.form-area{
		width: 90%;
		margin: 2vh auto;
		.btn-area{
			width: 100%;
			display: flex;
			justify-content: space-around;
			align-items: center;
			margin: 3vh auto;
			view{
				box-sizing: border-box;
				padding: 15rpx 40rpx;
				border-radius: 15rpx;
				font-size: 14px;
			}
		}
	}
</style>
