<script setup lang="ts">
	import { onMounted, ref, watch } from 'vue';
	import { getAction } from '../../common/http';
	import { formatDate } from '../../utils/date';
	import { onShow } from '@dcloudio/uni-app';
	import { showToast } from 'vant';

	const form = ref({} as any)
	const list = ref([] as any[])
	const mlList = ref([] as any[])
	const statusList = ref([
		{ status: "部分配货", type: "warning" },
		{ status: "待配货", type: "primary" },
		{ status: "已配全", type: "success" },
	])
	const nameList = ref([] as any[])

	onMounted(() => {
		getAction('/fabric/info/all').then((res : any) => {
			if (res.code === 200) {
				res.data.forEach((l : any) => {
					let itemMl = mlList.value.find(m => m.text === l.commodity)
					if (itemMl) {
						let itemMm = itemMl.children.find(m => m.value === l.momme)
						if (itemMm) {
							itemMm.children.push({ text: l.width + 'cm', value: l.width })
						} else {
							itemMl.children.push({
								text: l.momme + 'mm',
								value: l.momme,
								children: [{ text: l.width + 'cm', value: l.width }]
							})
						}
					} else {
						mlList.value.push({
							text: l.commodity,
							value: l.commodity,
							children: [{ text: l.momme + 'mm', value: l.momme, children: [{ text: l.width + 'cm', value: l.width }] }]
						})
					}
				})
			}
		})
		//所有用户名
		getAction('/v1/user/getNames').then((res : any) => {
			if (res.code === 200) {
				nameList.value = res.data.map((l : any) => ({ value: l, text: l }))
			}
		})
	})
	onShow(() => {
		list.value = []
		init()
	})

	const onClickLeft = () => {
		history.back()
	}

	function init() {
		getAction('/tribute/getUnTribute', form.value).then((res : any) => {
			if (res.code === 200) {
				list.value = res.data
			}
		})
	}

	watch(form.value, () => {
		list.value = []
		init()
	})

	const showPicker = ref(false)
	const typeData = ref()
	const popuList = ref([] as any[])
	//选择框事件
	const choosePic = (type : any) => {
		showPicker.value = true
		typeData.value = type
	}
	//选择框确认
	const pickerConfirm = (val : any) => {
		form.value[typeData.value] = val.selectedValues[0]
		showPickerCancel()
	}
	//取消
	const showPickerCancel = () => {
		showPicker.value = false
	}
	//弹窗开启事件
	const handleOpen = () => {
		if (typeData.value === 'zt') {
			popuList.value = [{ text: '待配货', value: '待配货' }, { text: '部分配货', value: '部分配货' }, { text: '已配全', value: '已配全' }]
		} else {
			popuList.value = nameList.value
		}
	}
	//日期选择
	const show = ref(false)
	const dataType = ref('')
	const chooseDate = (val : any) => {
		show.value = true
		dataType.value = val
	}
	const onConfirmDate = (val : any) => {
		form.value[dataType.value] = formatDate(val)
		show.value = false
	}

	//详情
	const toDetile = (item : any) => {
		if (item.type === '定做单') {
			let url = './detailCus' + '?' + 'id=' + item.orderId + '&subId=' + item.subId+'&zt=' + item.zt
			uni.navigateTo({
				url: url,
			})
		} else if (item.type === '现货单') {
			let url = './detailSpot' + '?' + 'id=' + item.orderId+'&zt=' + item.zt
			uni.navigateTo({
				url: url,
			})
		}

	}
	const showColor = (zt : any) => {
		let item = statusList.value.find((l : any) => l.status === zt)
		return item.type
	}
</script>

<template>
	<view class="flex">
		<van-nav-bar title="待配货列表" left-text="返回" left-arrow @click-left="onClickLeft" />
		<van-cell-group inset style="display: flex;">
			<van-field v-model="form.startDate" name="开始时间" label="开始时间" colon class="bor" label-width="5em" readonly
				@click="chooseDate('startDate')" :rules="[{ required: true, message: '请填写' }]" />
			<van-field v-model="form.endDate" name="截止时间" label="截止时间" colon class="bor" label-width="5em" readonly
				@click="chooseDate('endDate')" />
		</van-cell-group>
		<van-cell-group inset style="display: flex;">
			<van-field v-model="form.makeUser" name="做单人员" label="做单人员" colon class="bor" label-width="5em" readonly
				@click="choosePic('makeUser')" />
			<van-field v-model="form.zt" name="订单状态" label="订单状态" colon class="bor" label-width="5em" readonly
				@click="choosePic('zt')" />
		</van-cell-group>
		<view class="grid-container">
			<view class="grid-item">日期/颜色</view>
			<view class="grid-item">面料名称/工艺<br>谁定</view>
			<view class="grid-item">匹数</view>
			<view class="grid-item">状态</view>
		</view>
		<view class="content">
			<view v-for="(item,index) in list" :key="index">
				<template v-if="item.type==='定做单'">
					<view class="grid-container">
						<view class="grid-item">{{item.makeTime}}<br>{{item.fabrics[0].color}}</view>
						<view class="grid-item">
							<view class="grid-container1">
								<view :class="indexSecond===(item.fabrics.length-1)?'grid-item2':'grid-item1'"
									v-for="(itemSecond,indexSecond) in item.fabrics" :key="indexSecond">
									{{itemSecond.name}}<br>{{itemSecond.craft}}&nbsp;&nbsp;&nbsp;{{itemSecond.sd}}
								</view>
							</view>
						</view>
						<view class="grid-item">
							<view class="grid-container1">
								<view :class="indexSecond===(item.fabrics.length-1)?'grid-item2':'grid-item1'"
									v-for="(itemSecond,indexSecond) in item.fabrics" :key="indexSecond">
									{{itemSecond.qty}}
								</view>
							</view>
						</view>
						<view class="grid-item" @click="toDetile(item)">
							<van-button :type="showColor(item.zt)">{{item.zt}}</van-button>
						</view>
					</view>
				</template>
				<view class="grid-container" v-if="item.type==='现货单'">
					<view class="grid-item">{{item.makeTime}}<br>{{item.fabrics[0].color}}</view>
					<view class="grid-item">
						<view class="grid-container1">
							<view :class="indexSecond===(item.fabrics.length-1)?'grid-item2':'grid-item1'"
								v-for="(itemSecond,indexSecond) in item.fabrics" :key="indexSecond">
								{{itemSecond.name}}<br>{{itemSecond.craft}}&nbsp;&nbsp;&nbsp;{{itemSecond.sd}}
							</view>
						</view>
					</view>
					<view class="grid-item">
						<view class="grid-container1">
							<view :class="indexSecond===(item.fabrics.length-1)?'grid-item2':'grid-item1'"
								v-for="(itemSecond,indexSecond) in item.fabrics" :key="indexSecond">
								{{itemSecond.qty}}
							</view>
						</view>
					</view>
					<view class="grid-item" @click="toDetile(item)">
						<van-button :type="showColor(item.zt)">{{item.zt}}</van-button>
					</view>
				</view>
			</view>
		</view>
	</view>

	<!--选择框-->
	<van-popup v-model:show="showPicker" round position="bottom" @open="handleOpen">
		<van-picker show-toolbar :columns="popuList" @confirm="pickerConfirm" @cancel="showPickerCancel"
			ref="pickerRef" />
	</van-popup>

	<van-calendar v-model:show="show" @confirm="onConfirmDate" :min-date="new Date(2010, 0, 1)"
		:max-date="new Date(2050, 0, 31)" />
</template>

<style lang="scss" scoped>
	.flex {
		display: flex;
		flex-direction: column;
		height: 100vh;
		width: 100vw;
		overflow-y: hidden;

		.van-nav-bar {
			width: 100%;
		}

		.grid-container {
			display: grid;
			grid-template-columns: 1.5fr 2fr 1fr 1.5fr;
			padding: 0 15rpx;

			.grid-item {
				border: 1px solid #f2f2f2;
				display: flex;
				flex-direction: column;
				text-align: center;
				align-items: center;
				justify-content: center;

				.grid-container1 {
					display: grid;
					grid-template-columns: 1fr;
					width: 100%;
					height: 100%;

					.grid-item2 {
						display: flex;
						align-items: center;
						text-align: center;
						justify-content: center;
						padding: 5rpx;
					}

					.grid-item1 {
						border-bottom: 1px solid #f2f2f2;
						display: flex;
						align-items: center;
						text-align: center;
						justify-content: center;
						padding: 5rpx;
					}
				}
			}
		}

		.content {
			flex: 1;
			overflow-y: scroll;
		}
	}

	::v-deep .van-cell {
		padding: 5px !important;
	}

	::v-deep .van-field {
		font-size: 16px;
	}

	.bor {
		::v-deep .van-field__control {
			border: 1px solid #d7d7d7;
			text-align: center;
		}

		::v-deep .van-field__label {
			margin: 0;
		}
	}

	.bor-a {
		::v-deep .van-field__control {
			border-bottom: 1px solid #d7d7d7;
			text-align: center;
		}

		::v-deep .van-field__label {
			margin: 0;
		}
	}

	::v-deep .van-button--normal {
		padding: 5px 8px;
		height: 30px;
	}

	::v-deep .van-field__control {
		text-align: center;
	}

	/*面料选择搜索框*/
	.select-model {
		position: relative;
	}

	.confirm-select {
		width: 100%;
	}

	.confirm-select>p {
		text-align: center;
	}

	.confirm-select>button {
		border: none;
		background-color: #ffffff;
		color: #388aed;
		margin: 10px 30px 0 0;
	}

	.top-select {
		position: absolute;
		top: 30px;
		left: 0;
		right: 0;
		margin: auto;
		z-index: 999;
	}

	::v-deep .select-model .van-picker__toolbar {
		height: 66px;
		align-items: flex-start;
		/*justify-content: flex-end;*/
	}

	::v-deep .select-model .van-picker__cancel,
	.select-model .van-picker__confirm {
		height: 30px;
		padding: 10px 16px 0;
	}
	::v-deep .select-model .van-picker__confirm,
	.select-model .van-picker__confirm {
		height: 30px;
		padding: 10px 16px 0;
	}

	.search-btn {
		padding: 1px 8px;
		border: none;
		background-color: #388aed;
		color: #ffffff;
		margin-left: 5px;
		height: 30px;
		line-height: 25px;
	}

	.search-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding-top: 15px;
		padding-bottom: 5px;
		position: relative;
		background-color: #fff;
	}

	.van-search {
		background-color: #f7f7f8;
	}

	.ml-search-jump {
		padding: 3px 10px;
		background-color: #388aed;
		color: #ffffff;
		border: none;
		margin-left: 10px;
	}

	.flex-btn {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	/*面料选择搜索框*/
	.select-model {
		position: relative;
	}

	.confirm-select {
		width: 100%;
	}

	.confirm-select>p {
		text-align: center;
	}

	.confirm-select>button {
		border: none;
		background-color: #ffffff;
		color: #388aed;
		margin: 10px 30px 0 0;
	}

	.top-select {
		position: absolute;
		top: 30px;
		left: 0;
		right: 0;
		margin: auto;
		z-index: 999;
	}

	::v-deep .select-model .van-picker__toolbar {
		height: 66px;
		align-items: flex-start;
		/*justify-content: flex-end;*/
	}

	::v-deep .select-model .van-picker__cancel,
	.select-model .van-picker__confirm {
		height: 30px;
		padding: 10px 16px 0;
	}
	::v-deep .select-model .van-picker__confirm,
	.select-model .van-picker__confirm {
		height: 30px;
		padding: 10px 16px 0;
	}

	.search-btn {
		padding: 1px 8px;
		border: none;
		background-color: #388aed;
		color: #ffffff;
		margin-left: 5px;
		height: 30px;
		line-height: 25px;
	}

	.search-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding-top: 15px;
		padding-bottom: 5px;
		position: relative;
		background-color: #fff;
	}

	.van-search {
		background-color: #f7f7f8;
	}

	.ml-search-jump {
		padding: 3px 10px;
		background-color: #388aed;
		color: #ffffff;
		border: none;
		margin-left: 10px;
	}

	.flex-btn {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}
</style>