<template>
	<view class="">
		<uni-popup ref="popup" type="share" backgroundColor="#fff">
			<view class="select-container">
				<view class="title">
					<text class="btn cancel" @tap="close">取消</text>
					验收
					<text class="btn confirm" @tap="submit">确定</text>
				</view>
				<uni-forms :modelValue="formData" class="form" label-width='80px'>
					<uni-forms-item label="验收人" name="accepterUserId">
						<uni-data-select v-model="formData.accepterUserId" @change="handleChange" :clear="false"
							:localdata="contactInfoVOS"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="验收状态" name="accepterStatus">
						<uni-data-select v-model="formData.accepterStatus" :clear="false"
							:localdata="stateLiist"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="验收意见" name="accepterRemark">
						<textarea class="textarea" type="text" v-model="formData.accepterRemark" placeholder="验收意见" />
					</uni-forms-item>
					<PhotoBtn ref="photoRef" />
				</uni-forms>
			</view>
			<u-toast ref="uToast"></u-toast>
		</uni-popup>
	</view>
</template>

<script>
	import PhotoBtn from './PhotoBtn.vue';
	import {
		getSalesWorkOrderById,
		checkAndAccept
	} from '@/api/salesServiceManagement/workOrder/index.js'
	export default {
		components: {
			PhotoBtn
		},
		data() {
			return {
				acceptShow: false,
				formData: {
					accepterUserId: '',
					accepterUserName: '',
					accepterRemark: '',
					accepterStatus: '',
					id: ''
				},
				contactInfoVOS: [],
				stateLiist: [{
						value: 1,
						text: '通过'
					},
					{
						value: 0,
						text: '不通过'
					}
				],
			}
		},
		methods: {
			open(id) {
				this.acceptShow = true;
				this.$refs.popup.open();
				this.formData = {
					accepterUserId: '',
					accepterUserName: '',
					accepterRemark: '',
					accepterStatus: '',
					id,
				}
				this.getDadaList(id);
				console.log(this.formData, 'fffffm')
			},
			async getDadaList(id) {
				const {
					afterSalesDemandVO
				} = await getSalesWorkOrderById(id);
				this.contactInfoVOS = afterSalesDemandVO.contactInfoVOS || [];
				this.contactInfoVOS.map((el) => {
					el.value = el.id;
					el.text = el.contactName;
				})
				console.log(this.contactInfoVOS, 'contactInfoVOS')
			},
			handleChange(e) {
				this.accepterUserName = this.contactInfoVOS.find(el => el.id == e).contactName
			},
			submit() {
				if (!this.formData.accepterUserId) {
					this.$refs.uToast.show({
						type: "warning",
						message: "请选择验收人",
					})
					return;
				}
				if (!this.formData.accepterStatus) {
					this.$refs.uToast.show({
						type: "warning",
						message: "请选择验收状态",
					})
					return;
				}
				let accepterImageUrl = this.$refs.photoRef.getImageData();
				let data = {
					...this.formData,
					accepterImageUrl
				}
				uni.showLoading({
					title: '加载中'
				})
				checkAndAccept(data).then((res) => {
					this.$emit('getList');
					this.close();
					uni.hideLoading();
				}).then((err) => {
					uni.hideLoading();
				})
			},
			close() {
				this.$refs.popup.close();
			}
		}
	}
</script>

<style scoped lang="scss">
	.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 {
				// width: 80rpx;
				// height: 32rpx;
				display: inline-block;
				font-size: 30rpx;
				border: 1px solid #fff;
				text-align: center;
				line-height: 30rpx;
				margin: 0;
			}
		}

		.form {
			padding: 20rpx 14rpx 0;

			/deep/ .uni-forms-item__label {
				font-size: 28rpx;
			}

			.textarea {
				border: 1px solid #ddd;
			}
		}
	}
</style>