<template>
	<view class="assign-container">
		<u-popup :show="popShow" @close="close">
			<view class="select-container">
				<view class="title">
					<text class="btn cancel" @tap="close">取消</text>
					非完成报工
					<text class="btn confirm" @tap="submit">确定</text>
				</view>
				<view class="select-wrapper">
					<view class="col deptInp">
						<text class="label">部门：</text>
						<text class="input_text"
							@click="openPicker">{{ formData.executorDeptName ? formData.executorDeptName : '请选择' }}</text>
					</view>

					<view class="col userInp">
						<text class="label">接收人：</text>
						<zxz-uni-data-select :localdata="userList" v-model="formData.executorId"
							@change="handleUserChange"></zxz-uni-data-select>
					</view>
					<view class="col">
						<view class="label">处理说明：</view>
						<textarea class="textarea" v-model="formData.repairReportExplain"></textarea>
					</view>
					<view class="col">
						<view class="label">附件：</view>
						<fileMain v-model="formData.attachments"></fileMain>
					</view>
				</view>
			</view>
		</u-popup>
		<ba-tree-picker ref="treePicker" :multiple="false" @select-change="confirm" title="选择部门" :localdata="listData"
			valueKey="id" textKey="name" childrenKey="children" />
	</view>
</template>

<script>
	import {
		listOrganizations,
		getUserPage,
		workOrderRotate
	} from '@/api/myTicket/index.js'
	import {
		post,
		get,
		postJ
	} from '@/utils/api.js'
	import baTreePicker from '@/components/ba-tree-picker/ba-tree-picker.vue'
	import fileMain from "@/pages/doc/index.vue"
	import {
		loseReport
	} from '@/api/repair'

	export default {
		components: {
			baTreePicker,
			fileMain
		},
		props: {},
		data() {
			return {
				popShow: false,
				deptPickerShow: false,
				listData: [],
				userList: [],
				formData: {
					executorDeptName: '',
					executorName: '',
					executorId: '',
					repairReportExplain: '',
					attachments: []
				},

				selectList: [],
				newArr: [],
				sid: ''
			}
		},
		created() {
			this.getDept()
		},
		methods: {
			open(id) {
				this.sid = id;
				console.log(id)
				this.popShow = true
			},
			openPicker() {
				this.$refs.treePicker._show()
			},

			deWeight(arr4) {
				var obj = {}
				arr4 = arr4.reduce(function(a, b) {
					obj[b.userId] ? '' : (obj[b.userId] = true && a.push(b))
					return a
				}, [])
				return arr4
			},
			handleUserChange(obj) {
				this.formData.executorName = obj.text

				// const obj = this.userList.find(i => i.value === id)
				// this.formData.executorName = (obj && obj.text) || ''
			},
			close() {
				this.formData = {
					executorDeptName: '',
					executorName: '',
					executorId: '',
					repairReportExplain: '',
					attachments: []
				}
				this.popShow = false
			},

			submit() {
				if (!this.formData.executorId) {
					uni.showToast({
						icon: 'error',
						title: '请选择接收人'
					})
					return
				}

				let params = {
					workOrderId: this.sid,
					acceptUserId: this.formData.executorId,
					reason: this.formData.repairReportExplain,
					attachments: this.formData.attachments,
				}
				loseReport(params).then(() => {
					uni.showToast({
						title: '非报工成功',
						icon: 'success',
						duration: 2000
					})
					this.popShow = false;
				})
			},
			confirm(data, name) {
				console.log(data)
				console.log(name)
				this.formData.executorDeptName = name
				this.formData.executorDeptCode = data[0]

				this.formData.executorName = ''
				this.formData.executorId = ''
				this.getUser(data[0])
			},
			getDept() {
				listOrganizations(1).then(data => {
					console.log('listOrganizations-----------------------')
					console.log(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
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.select-container {
		min-height: 60vh;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			background-color: $j-primary-border-green;
			font-weight: bold;
			padding: 0 20rpx;
			position: relative;
			font-size: 32rpx;
			color: #fff;

			.btn {
				padding: 10rpx 20rpx;
				display: inline-block;
				font-size: 32rpx;
				border: 1px solid #fff;
				text-align: center;
				line-height: 30rpx;
				margin: 0;
			}
		}

		.select-wrapper {
			.col {
				display: flex;
				// min-height: 0rpx;
				margin-top: 22rpx;
				align-items: center;
				padding-right: 14rpx;

				.label {
					display: inline-block;
					width: 200rpx;
					text-align: right;
				}

				.input_text {
					flex: 1;
					height: 66rpx;
					line-height: 66rpx;
					padding: 0rpx 16rpx;
					box-sizing: border-box;
					font-size: 28rpx;
					border: 1px solid #eceeec;
					border-radius: 8rpx;
				}

				input {
					flex: 1;
					border: 1rpx solid #e5e5e5;
					height: 66rpx;
					border-radius: 8rpx;
					font-size: 28rpx;
					padding: 0rpx 16rpx;
					color: #6a6a6a;
				}

				textarea {
					border: 1rpx solid #e5e5e5;
					flex: 1;
					height: 100rpx;
				}

				.tab_box {
					display: flex;
					flex-direction: row;
					flex-wrap: wrap;
					width: 520rpx;

					.tab {
						display: flex;
						flex-direction: row;

						border: 2rpx solid #d9ecff;
						background-color: #f4f4f5;
						border-color: #e9e9eb;
						color: #909399;
						margin-left: 8rpx;
						margin-bottom: 8rpx;
						padding: 2rpx 8rpx;
						font-size: 28rpx;
					}

					.icon {
						margin-left: 6rpx;
					}
				}
			}
		}
	}
</style>