<template>
	<dialog-base ref="baseDialog" title="周转申请">
		<view class="list-container">
			<view class="list-title"><text class="label">请选择周转类型</text></view>
			<view class="btn uni-row">
				<view v-for="(item,index) in turnoverType"
					:class="{ 'middle-btn': true, 'active': item == turnoverTypeChecked }"
					@click="selectTurnoverType(item)"><text class="label">{{item.dictLabel}}</text></view>
			</view>
			<view v-if="curDayworkItem.turnoverType == '2'" class="list-title">
				<text class="label">请选择周转位置</text>
			</view>
			<view class="uni-row">
				<view v-if="curDayworkItem.turnoverType == '2'" v-for="(item,index) in turnoverArea" class="btn">
					<view :class="{ 'middle-btn': true, 'active': item == turnoverDoorChecked }"
						@click="selectTurnoverDoor(item)"><text class="label">{{item.dictLabel}}</text></view>
				</view>
			</view>
			<view v-if="curDayworkItem.turnoverType == '2'" class="" style="margin: 0 20rpx 20rpx 0;width: 96%;">
				<uni-section title="请选择周转工段" title-font-size="32rpx" style="margin: 0 0 0 -16rpx;">
					<uni-data-select v-model="curDayworkItem.deptId" :localdata="deptList" @change="handleChange"
						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,
		onMounted
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getDictInfoByType
	} from '@/api/dict/dict.js'
	import {
		getDayWorkItemList,
		saveDayWorkItem
	} from '@/api/business/dayWorkItem.js'
	import {
		getDeptList
	} from '@/api/dept/dept.js'
	import {
		store
	} from '@/store/index.js'
	import {
		timestampToTime
	} from '@/utils/common.js'

	const baseDialog = ref(null)
	const turnoverTypeChecked = ref({})
	const turnoverDoorChecked = ref({})
	const turnoverType = ref([])
	const turnoverArea = ref([])
	const curDayworkItem = ref({})
	const dayworkInfo = ref(null)
	const deptList = ref([]) // 工段列表
	// const emit = defineEmits('confirm') // 自定义调用父组件方法

	onLoad(() => {

	})

	function open(data) {
		dayworkInfo.value = data;
		baseDialog.value.open()
		init();
	}

	defineExpose({
		open
	})

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

	function init() {
		getDictInfoByType('daywork_turnover_type').then(res => {
			turnoverType.value = res.data;
			getDictInfoByType('daywork_turnover_area').then(res => {
				turnoverArea.value = res.data;
			})
		})
		getDayWorkItemList({
			dayworkId: dayworkInfo.value.id,
			userId: store.userInfo.userId
		}).then(res => {
			console.log(res.rows[0])
			curDayworkItem.value = res.rows[0];
		})
		getDeptList({
			isWorkSection: 1
		}).then(res => {
			console.log(res.data)
			for (var i = 0; i < res.data.length; i++) {
				deptList.value[i] = {
					text: res.data[i].deptName,
					value: res.data[i].deptId,
					data: res.data[i]
				}
			}
		})
	}

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

	function selectTurnoverDoor(item) {
		turnoverDoorChecked.value = item;
		curDayworkItem.value.turnoverArea = item.dictValue;
	}

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

	function handleConfirm() {
		curDayworkItem.value.id = null;
		curDayworkItem.value.status = '4';
		curDayworkItem.value.startTime = timestampToTime(new Date());
		curDayworkItem.value.technologicalProcessId = dayworkInfo.value.technologicalProcessId;
		if (!store.tenantId) {
			curDayworkItem.value.tenantId = store.userInfo.tenantId;
		} else {
			curDayworkItem.value.tenantId = store.tenantId;
		}
		console.log(deptList.value)
		// 设置周转下一个车间名
		for (let i = 0; i < deptList.value.length; i++) {
			if (deptList.value[i].value == curDayworkItem.value.deptId) {
				curDayworkItem.value.deptName = deptList.value[i].text;
			}
		}
		console.log(curDayworkItem.value);
		if (!handleValidate(curDayworkItem.value)) {
			uni.showToast({
				icon: "error",
				title: '请选择完整信息',
				duration: 2000
			});
		} else {
			saveDayWorkItem(curDayworkItem.value).then(res => {
				if (res.code === 200) {
					uni.showToast({
						icon: 'success',
						title: '操作成功',
						duration: 2000
					});
					close();
				} else {
					uni.showToast({
						icon: 'error',
						title: '操作失败',
						duration: 2000
					});
					close();
				}
			})
		}
		// emit('confirm');
		uni.$emit('dayworkItemUpdate');
	}

	function handleChange() {
		console.log(curDayworkItem.value)
	}
</script>

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

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

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

			.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>