<template>
	<view class="main">
		<uni-nav-bar fixed="true" statusBar="true" left-icon="back" right-icon="scan" title="申请备品备件"
			@clickLeft="goHome"></uni-nav-bar>
		<!-- <KdTabs v-model="active" :list="['基本信息','设备信息','备品备件信息']" /> -->
		<u-subsection :list="tabList" :current="active" @change="handleTabChange" active-color="#157a2c"></u-subsection>
		<view v-show="active==0">
			<Cell cellType="more" title="编号">
				<view class="cell-tip-right" slot="more">
					{{ addForm.code  }}
				</view>
			</Cell>
			<Cell cellType="more" title="工单编码">
				<view class="cell-tip-right" slot="more">

					{{ addForm.repairCode  }}
				</view>
			</Cell>
			<Cell cellType="more" title="工单名称">
				<view class="cell-tip-right" slot="more">

					{{ addForm.repairName  }}
				</view>
			</Cell>
			<!-- 		<Cell cellType="more" title="设备编号">
				<view class="cell-tip-right" slot="more">
		
					{{ addForm.deviceCode }}
				</view>
			</Cell>
			<Cell cellType="more" title="设备名称">
				<view class="cell-tip-right" slot="more" v-if="sparePartsApplyList.length==0">
					{{ addForm.deviceName }}
				</view>
				<view class="cell-tip-right" slot="more" v-else>
					<uni-data-select style="width: 500rpx;" v-model="addForm.deviceId"
						:localdata="sparePartsApplyList" @change="sparePartsApplyChange"
						:clear="true"></uni-data-select>
				</view>
			</Cell>
			<Cell cellType="more" title="固资编码">
				<view class="cell-tip-right" slot="more">
					{{ addForm.fixCode }}
				</view>
			</Cell> -->
			<!-- addForm.receivingDeptName -->
			<Cell cellType="more" title="领用部门">
				<view class="cell-tip-right" slot="more">
					{{ addForm.receivingDeptName }}
				</view>
			</Cell>
			<!-- {{ addForm.recipientName }} -->
			<Cell cellType="more" title="领用人">
				<view class="cell-tip-right" slot="more">
					{{ addForm.recipientName }}
				</view>
			</Cell>
			<Cell cellType="more" title="使用部门">
				<view class="cell-tip-right" slot="more">
					<text class="input_text"
						@click="openPicker">{{ addForm.useDeptName ? addForm.useDeptName : '请选择' }}</text>
				</view>
			</Cell>
			<Cell cellType="more" title="使用人">
				<view class="cell-tip-right" slot="more">
					<zxz-uni-data-select v-if="type=='add'" style="width: 300rpx;" :localdata="userList"
						v-model="addForm.userId" @change="handleUserChange"></zxz-uni-data-select>
					<text v-else>{{addForm.userName}}</text>
				</view>
			</Cell>

			<Cell cellType="more" title="使用时间">
				<view class="cell-tip-right" slot="more">

					<uni-datetime-picker v-if="type=='add'" type="date" class="picker" v-model="addForm.usageTime">

					</uni-datetime-picker>
					<text v-else>{{addForm.usageTime}}</text>
				</view>

			</Cell>
			<Cell cellType="more" title="用途">
				<u--textarea :disabled="type=='detail'" v-model="addForm.purpose" class="border" maxlength="500"
					placeholder="请输入内容" count></u--textarea>

			</Cell>
		</view>
		<view v-show="active==1" class="kd-equipment">

			<!-- <u-button v-if="type=='add'" type="success" size="small" class="u-reset-button" @click="addDevice()"
				text="添加物品"></u-button> -->
			<view class="kd-list-container">
				<u-list>
					<u-list-item v-for="(item, index) in deviceList" :key="index">
						<view class="kd-card">
							<view class="kd-card-wrapper">
								<uni-icons @click="del(item.id,1)" v-if="type=='add'" custom-prefix="iconfont"
									type="icon-shanchu" size="20" color="#fa3534"></uni-icons>
								<view class="kd-cell">

									<text class="kd-label">设备编码</text>
									{{ item.code }}
								</view>
								<view class="kd-cell">
									<text class="kd-label">设备名称</text>
									{{ item.name }}
								</view>
								<view class="kd-cell">
									<text class="kd-label">设备型号</text>
									{{ item.category&&item.category.modelType||item.model }}
								</view>
								<!-- 	<view class="kd-cell">
									<text class="kd-label">设备位置</text>
									{{ item.deviceLocationName.pathName }}
								</view> -->

							</view>

						</view>
					</u-list-item>
				</u-list>
			</view>
		</view>
		<view v-show="active==2">

			<u-button v-if="type=='add'" type="success" size="small" class="u-reset-button" @click="addPicking()"
				text="添加物品"></u-button>
			<view class="list_box">
				<u-list>
					<view v-for="(item, index) in tableList" :key="index" @change="e => selectVal(e, item, index)">

						<view class="listBox-con">
							<view class="listBox-top rx-bc">

								<view @click="del(item.id)"> <uni-icons v-if="type=='add'" custom-prefix="iconfont"
										type="icon-shanchu" size="20" color="#fa3534"></uni-icons>
									{{ item.categoryName }}
								</view>
							</view>

							<view class="listBox-bottom rx">
								<view class="items">
									<text>编码</text>
									{{ item.categoryCode }}
								</view>
								<view class="items">
									<text>牌号</text>
									{{ item.brandNum }}
								</view>
								<view class="items">
									<text>型号</text>
									{{ item.categoryModel }}
								</view>

								<view class="items">
									<text>规格</text>
									{{ item.specification }}
								</view>
								<view class="items">
									<text>级别</text>
									{{ item.level }}
								</view>
								<view class="items" style="display: flex;">
									<text>出库数量</text>
									<input v-if="type=='add'" style="width: 80px;" v-model="item.totalCount"
										type="number" @blur="handleInput(item,index)" placeholder="请输入出库数量" />
									<text v-else>{{item.totalCount}}</text>
								</view>
								<view class="items">
									<text>计量数量</text>
									{{ item.measureQuantity }}
								</view>
								<view class="items">
									<text>计量单位</text>
									{{ item.measureUnit }}
								</view>
							</view>
						</view>

					</view>

					<view v-if="tableList.length == 0" style="margin-top: 20vh">
						<u-empty iconSize="150" textSize="32" text="暂无数据"></u-empty>
					</view>
				</u-list>
			</view>
		</view>
		<view class="footer" v-if="type=='add'">
			<text @click="submitAdd">提交</text>
		</view>
		<ba-tree-picker ref="treePicker" :multiple="false" @select-change="confirm" title="选择部门" :localdata="listData"
			valueKey="id" textKey="name" childrenKey="children" />

	</view>
</template>

<script>
	import KdTabs from '@/components/KdTabs.vue'
	import {
		listOrganizations,
		getUserPage
	} from '@/api/myTicket/index.js'
	import baTreePicker from '@/components/ba-tree-picker/ba-tree-picker.vue'

	import {
		applySpareParts,
		detailsId
	} from '@/api/repair'
	import Cell from '@/components/Cell.vue'
	import {
		postJ,
		get
	} from '@/utils/api.js'

	import dayjs from 'dayjs'
	export default {
		components: {
			Cell,
			KdTabs,
			baTreePicker
		},
		data() {
			return {
				active: 0,
				tabList: ['基本信息', '设备信息', '备品备件信息'],
				addForm: {
					name: '',
					repairId: '', //维修工单id
					repairCode: '',
					code: '',
					repairName: '',
					deviceName: '',
					fixCode: '',
					deviceCode: '',
					type: '4', //类型
					status: '1', //状态
					purpose: '',
					userId: '', //使用人id
					userName: '',
					useDeptId: '', //使用部门
					useDeptName: '',
					warehouseId: '',
					usageTime: '', //使用时间 //详情信息
					detailList: [],
					deviceId: '',
					executorDeptName: '',
					receivingDeptName:'',
					recipientName:""
				},
				tableList: [],
				deviceList: [],
				// sparePartsApplyList: [],
				type: 'add',
				showTime: '请选择时间',
				userList: [],
				listData: []
			}
		},
		onShow() {
			uni.$off('sparePartsApply')
			uni.$on('sparePartsApply', (data) => {
				this.tableList = data
				// console.log(data,'data')
			})
		},
		async onLoad(data) {
			const obj = JSON.parse(data.data)
			this.userInfo = uni.getStorageSync('userInfo')
			console.log(this.userInfo,'this.userInfo')
			this.type = obj.pageType
			this.getDept()
			if (this.type == 'add') {
				this.addForm.repairCode = obj.code;
				this.addForm.repairName = obj.planName;
				this.addForm.repairId = obj.id;
				this.deviceList = obj.deviceList;
				this.addForm.receivingDeptName = this.userInfo.groupName;
				this.addForm.recipientName = this.userInfo.name;
				// if (!obj.sparePartsApplyList) {
				// 	this.addForm.deviceName = obj.deviceName;
				// 	this.addForm.fixCode = obj.fixCode;
				// 	this.addForm.deviceCode = obj.deviceCode;
				// } else {
				// 	this.sparePartsApplyList = obj.sparePartsApplyList;
				// }

			} else {
				const form = await detailsId(obj.id)
				this.tableList = form.detailList
				this.deviceList = form.deviceList
				this.addForm = form

				if (this.addForm.useDeptId) {
					this.getUser(this.addForm.useDeptId)
				}
			}
		},
		methods: {
			handleTabChange(value) {
				this.active = value
			},
			openPicker() {
				this.$refs.treePicker._show()
			},
			del(id, type) {
				if (this.type != 'add') {
					return
				}
				if (type == 1) {
					this.deviceList = this.deviceList.filter((item) => item.id != id);
					return
				}
				this.tableList = this.tableList.filter((item) => item.id != id);
			},
			sparePartsApplyChange(value) {
				const data = this.sparePartsApplyList.find(item => item.value == value)
				this.addForm.deviceName = data.text;
				this.addForm.fixCode = data.fixCode;
				this.addForm.deviceCode = data.deviceCode;

			},
			// 出库数量限制
			handleInput(row, index) {
				if (row.totalCount > row.measureQuantity) {
					this.$set(this.tableList[index], 'totalCount', row.measureQuantity)
				}
			},
			addPicking() {
				uni.navigateTo({
					url: `/pages/maintenanceWorkorder/sparepart/sparepartList?codeS=` + JSON.stringify(this
						.tableList.map(item => item.categoryCode))
				})

			},
			goHome() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 提交新增
			submitAdd() {
				if (this.deviceList.length == 0) {
					uni.showToast({
						title: '请选择设备!',
						icon: 'none'
					})
					return
				}
				if (this.tableList.length == 0) {
					uni.showToast({
						title: '请选择备品备件!',
						icon: 'none'
					})
					return
				}
				let boolen = this.tableList.every((item) => item.totalCount > 0);
				if (!boolen) {
					uni.showToast({
						title: '请输入出库数量!',
						icon: 'none'
					})
					return
				}

				uni.showLoading({
					title: '加载中'
				})
				
				this.addForm.detailList = this.tableList
				this.addForm.deviceList = this.deviceList
				applySpareParts(this.addForm)
					.then(res => {
						uni.showToast({
							title: '申请成功',
							icon: 'success',
							duration: 2000
						})
						setTimeout(() => {
							this.goHome()
						}, 2000)
					})
					.catch(e => {
						this.loading = false
					})
			},
			confirm(data, name) {

				this.addForm.useDeptName = name
				this.addForm.useDeptId = data[0]
				this.addForm.userName = ''
				this.addForm.userId = ''
				this.getUser(data[0])
			},
			getDept() {
				listOrganizations(1).then(data => {
					this.listData = data
				})
			},
			getUser(deptCode) {
				getUserPage({
					pageNum: 1,
					size: -1,
					groupId: deptCode
				}).then(data => {
					this.userList = data.list.map(item => {
						item.text = item.name
						item.value = item.id
						return item
					})
				})

			},
			handleUserChange(obj) {
				this.addForm.userName = obj.text
			},
		}
	}
</script>

<style scoped lang="scss">
	/deep/.u-subsection__item__text {
		font-size: 28rpx !important;
	}

	/deep/.u-cell__body {
		// {
		// 	font-size: 26rpx !important;
		// }
		// padding: 12rpx 12rpx;

		.u-cell__title-text,
		.input-value,
		.uni-input-wrapper {
			font-size: 26rpx !important;
		}
	}

	//底部按钮
	.footer {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		bottom: 0;
		width: 100%;
		height: 100rpx;
		text-align: center;
		position: fixed;

		// text {
		// 	width: 100%;
		// 	background-color: $j-primary-green;
		// 	font-size: 34rpx;
		// 	color: #FFFFFF;
		// 	line-height: 100rpx;
		// }

		text {
			width: 100%;
			background-color: $j-primary-green;
			font-size: 34rpx;
			color: #ffffff;
			line-height: 100rpx;
		}
	}




	.main {
		padding-bottom: 130rpx;
	}

	.border {
		border: 1px solid #eee;
	}



	.list_box {
		flex: 1;
		overflow: hidden;
		padding: 6rpx 0;

		.u-list {
			height: 100% !important;
		}
	}

	.list_box {
		margin-top: 8rpx;
		padding: 8rpx 24rpx;
		background: #fff;

		/deep/ .uni-checkbox-input-checked {
			background-color: $theme-color !important;
			border-color: $theme-color !important;
		}

		.listBox-con {
			// width: 650rpx;
			font-weight: 400;
		}

		.listBox-top {
			margin-top: 6rpx;
			color: #090a0a;
			font-size: 28rpx;
			font-style: normal;
			font-weight: 800;
		}

		.listBox-bottom {
			color: #090a0a;
			font-size: 24rpx;
			font-style: normal;
			flex-wrap: wrap;

			.items {
				width: calc(50% - 1px);
				border-left: 1rpx solid #e3e5e5;
				border-right: 1rpx solid #e3e5e5;
				border-bottom: 1rpx solid #e3e5e5;
				box-sizing: border-box;
				word-break: break-all;

				text {
					display: inline-block;
					background: #f7f9fa;
					padding: 8rpx 10rpx;
					color: #157a2c;
				}

				&:nth-child(1),
				&:nth-child(2) {
					border-top: 1rpx solid #e3e5e5;
					margin-top: 8rpx;
				}
			}
		}
	}

	.kd-equipment {
		flex: 1;
		display: flex;
		flex-direction: column;
		overflow: hidden;

		.kd-type-box {
			text-align: center;
			padding: 26rpx 0;

			view {
				position: relative;
				display: inline-block;
				width: 120rpx;
				padding: 4rpx 0;
				color: #747474;
				margin: 0 20rpx;
				background-color: rgba(215, 215, 215, 0.5);

				&.type—active {
					background-color: #1e7f35;
					color: #fff;
				}

				.count {
					position: absolute;
					top: -9px;
					right: -9px;
					width: 18px;
					height: 18px;
					border-radius: 50%;
					font-size: 12px;
					background-color: red;
					color: #fff;
				}
			}
		}

		.kd-list-container {
			flex: 1;
			display: flex;
			flex-direction: column;
			overflow: hidden;
			padding: 12rpx 18rpx;
			background-color: $page-bg;

			.u-list {
				flex: 1;
				height: 100% !important;
			}
		}
	}

	.kd-card {
		background-color: #fff;
		margin-bottom: 20rpx;
		padding: 8rpx 0;
		font-size: 28rpx;
		word-break: break-all;

		.kd-card-wrapper {
			padding: 0 30rpx;
			border-bottom: 1px solid #dadada;
		}

		.kd-cell {
			line-height: 60rpx;
		}

		.kd-cell:last-of-type {
			border-bottom: none;
		}

		.status-box {
			margin-right: 16rpx;
		}

		.card-footer {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			padding: 8rpx 0 20rpx;

			button {
				width: 180rpx;
				height: 56rpx;
				line-height: 56rpx;
				font-size: 28rpx;
				margin: 0 8rpx;
			}

			.primary-btn {
				background-color: $j-primary-border-green;
			}
		}
	}

	.kd-cell {
		min-height: 90rpx;
		border-bottom: 1px dashed #dadada;
		display: flex;
		align-items: center;

		.kd-label {
			display: inline-block;
			width: 7em;
			font-weight: bold;
		}

		.kd-content {
			flex: 1;
			word-break: break-all;
		}
	}

	.kd-baseInfo {
		padding: 0 32rpx;
		font-size: 28rpx;
	}
</style>