<template>
	<dialog-base ref="baseDialog" title="周转申请">
		<view class="list-container">
			<view><text style="margin: 0 0 0 5rpx;">请选择周转类型</text></view>
			<view class="btn uni-row">
				<view v-for="(item,index) in turnoverType"
					:class="{ 'middle-btn': true, 'active': item.dictValue == curDayworkItem.turnoverType }"
					@click="selectTurnoverType(item)"><text class="label"
						style="font-size: 30rpx;">{{item.dictLabel}}</text></view>
			</view>
			<view class="" style="margin: 0 20rpx 20rpx 0;width: 88%;">
				<uni-section title="请选择下序工段" title-font-size="32rpx" style="margin: 0 0 0 -16rpx;"
					v-if="curDayworkItem.turnoverType == '2'">
					<uni-data-select v-model="curDayworkItem.deptId" :localdata="outsideDepts"
						:clear="false"
						style="margin: 0 0 0 16rpx;outline: 2rpx solid #999999;border-radius: 10rpx;"></uni-data-select>
				</uni-section>
			</view>
		</view>
		<view class="add-btn-container uni-row">
			<button type="default" class="btn" @click="handleConfirm">确认</button>
		</view>
	</dialog-base>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getDictInfoByType
	} from '@/api/dict/dict.js'
	import {
		turnover,
		updateTurnoverInfo
	} from '@/api/business/dayWorkItem.js'
	import {
		getDeptList
	} from '@/api/dept/dept.js'
	import {
		store
	} from '@/store/index.js'

	const baseDialog = ref(null)
	const turnoverType = ref([])
	const turnoverArea = ref([])
	const curDayworkItem = ref({
		turnoverType: 1
	})
	const dayworkInfo = ref(null)
	const deptList = ref([]) // 工段列表
	const outsideDepts = ref([]) // 车间外工段
	const emit = defineEmits(['reflushTurnoverList']) // 自定义调用父组件方法

	onLoad(() => {

	})

	function open(data) {
		resetPage();
		dayworkInfo.value = data;
		console.log(store)
		console.log(dayworkInfo.value)
		init();
		baseDialog.value.open();
	}

	defineExpose({
		open
	})

	function close() {
		baseDialog.value.close()
	}

	function resetPage() {
		turnoverType.value = []
		turnoverArea.value = []
		curDayworkItem.value = {
			turnoverType: 1
		}
		deptList.value = []
		outsideDepts.value = []
	}

	function init() {
		getDictInfoByType('daywork_turnover_type').then(res => {
			turnoverType.value = res.data.filter(item => {return item.dictValue !=1 && item.dictValue !=3 });
		})
		getDeptList({
			tenantId:store.tenantId,
			productionPlanDetailId: dayworkInfo.value.daywork.productionPlanDetailId,
			lotId:dayworkInfo.value.daywork.lotId,
			isWasteRecycling:dayworkInfo.value.daywork.isWasteRecycling,
			isAmend: dayworkInfo.value.daywork.isAmend
		}).then(res => {
				deptList.value = res.data.filter(item => {return item.deptName.trim() != dayworkInfo.value.preDeptName.trim()});
				for (let i = 0; i < deptList.value.length; i++) {
					outsideDepts.value.push({
						text: deptList.value[i].deptName,
						value: deptList.value[i].deptId,
						data: deptList.value[i]
					})
			}
			console.log(outsideDepts.value)
		})
	}

	function selectTurnoverType(item) {
		curDayworkItem.value.turnoverType = item.dictValue;
	}

	function handleValidate(data) {
		console.log(data)
		if (data.turnoverArea == "" || data.deptId == null) {
			return false;
		} else {
			return true;
		}
	}

	function handleConfirm() {
			// curDayworkItem.value.dayworkId = store.dayworkInfo.id;
			// 设置周转下一个车间名
			for (let i = 0; i < deptList.value.length; i++) {
				if (deptList.value[i].deptId == curDayworkItem.value.deptId) {
					curDayworkItem.value.deptName = deptList.value[i].deptName;
				}
			}
			if (!handleValidate(curDayworkItem.value)) {
				uni.showToast({
					icon: "none",
					title: '请选择完整信息'
				});
			} else {
				curDayworkItem.value.dayworkId = dayworkInfo.value.daywork.id
				curDayworkItem.value.processStepNumber = dayworkInfo.value.processStepNumber
				console.log(curDayworkItem.value)
				close();
				updateTurnoverInfo(curDayworkItem.value).then(res => {
					if (res.code === 200) {
						uni.showToast({
							icon: 'success',
							title: '操作成功'
						});
						emit('reflushTurnoverList');
					} else {
						uni.showToast({
							icon: 'none',
							title: '操作失败'
						});
					}
				})
			}
			
			}
</script>

<style lang="scss">
	.dialog-body {
		.list-container {
			width: 100%;

			.list-title {
				margin-top: 24rpx;

				.label {
					font-size: 32rpx;
				}
			}

			.turnArea {
				flex-wrap: wrap;
				height: auto;
				max-height: 200rpx;
				overflow: auto;
			}

			.btn {
				margin-top: 24rpx;

				.middle-btn {
					margin-right: 32rpx;
					align-items: center;
					justify-content: center;
					padding-left: 0;
					height: 80rpx;
					width: 220rpx;
					border-radius: 8rpx;
					background-color: #FFFFFF;
					border: 1px solid #999999;
					background-color: #FFFFFF;

				}

				.label {
					font-size: 24rpx;
					color: #000000;
				}

				.active {
					border-color: #1684fc;
					background-color: rgb(236, 245, 255);

					.label {
						color: #1684fc;
					}
				}
			}
		}

		.add-btn-container {
			margin-top: 32rpx;

			.btn {
				flex: 1;
				background-color: rgb(255, 121, 1);
				color: #FFFFFF;

			}
		}
	}
</style>