<template>
	<dialog-base ref="baseDialog" title="摆放位置">
		<view class="list-container">
<!-- 			<view class="" style="margin: 0 20rpx 20rpx 0;width: 88%;">
				<uni-section title="请选择工段" title-font-size="32rpx" style="margin: 0 0 0 -16rpx;">
					<uni-data-select v-model="curDept" :localdata="deptList" @change="handleChange"
					:clear="false"
						style="margin: 0 0 0 16rpx;outline: 2rpx solid #999999;border-radius: 10rpx;"></uni-data-select>
				</uni-section>
			</view> -->
			<view class="list-title">
				<text class="label">请选择您存放位置</text>
			</view>
			<view class="turnArea uni-row" >
				<view v-for="(item,index) in turnAreaList" class="btn">
					<view :class="{ 'middle-btn': true, 'active': item == turnoverDoorChecked }"
						@click="selectTurnoverDoor(item)"><text class="label">{{item.code}}</text></view>
				</view>
			</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 {
		getDayWorkItemList,
		saveDayWorkItem
	} from '@/api/business/dayWorkItem.js'
	import {
		getDeptList
	} from '@/api/dept/dept.js'
	import {
		getTurnoverListByDeptId
	} from '@/api/business/turnover.js'
	import {
		store
	} from '@/store/index.js'
	import {
		timestampToTime
	} from '@/utils/common.js'

	const baseDialog = ref(null) 
	const turnoverDoorChecked = ref({})
	const turnoverArea = ref([])
	const curDept = ref(0)
	const deptList = ref([]) // 工段列表
	const turnAreaList = ref([]) // 周转区列表
	const placement = ref(null)
	const emit = defineEmits(['submit']);

	onLoad(() => {

	})

	function open() {
		resetPage();
		baseDialog.value.open();
		init();
	}
 
	function resetPage(){
		deptList.value = [];
		turnAreaList.value = [];
		curDept.value = 0;
		placement.value = null;
	}	
	
	defineExpose({
		open
	})

	function close() {
		baseDialog.value.close()
		turnAreaList.value = [];
	}

	function init() {
		// getDeptList({
		// 	isWorkSection: 1,
		// 	tenantId: store.tenantId
		// }).then(res => {
		// 	for (var i = 0; i < res.data.length; i++) {
		// 		deptList.value[i] = {
		// 			text: res.data[i].deptName,
		// 			value: res.data[i].deptId,
		// 		}
		// 	}
		// 	curDept.value = res.data[0].deptId;
			handleGetTurnoverListByDId(store.curDeptDetails.deptId);
		// })
	}


	function selectTurnoverDoor(item) {
		turnoverDoorChecked.value = item;
		placement.value = item;
		console.log(placement.value)
	}


	function handleConfirm() {
		if(placement.value){
			emit('submit',placement.value);
			close();
		}else{
			uni.showToast({
				icon: 'none',
				title: '请选择摆放位置'
			})
		}
	}

	// function handleChange() {
	// 	handleGetTurnoverListByDId(curDept.value);
	// }
	
	function handleGetTurnoverListByDId(depId){
		// turnAreaList.value = [];
		getTurnoverListByDeptId({
			deptId: depId,
		}).then(res => {
			for (var i = 0; i < res.data.length; i++) {
				if(res.data[i].status == 0){
					turnAreaList.value[i] = res.data[i];
				}
			}
		})
	}
</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: #1684fc;
				color: #FFFFFF;

			}
		}
	}
</style>