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

	const form = ref({} as any)
	const dataItem = ref({ shrq: formatDate(new Date()) } as any)
	const list = ref([] as any[])
	const factory = ref([] as any[])
	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 }))
			}
		})
		//获取工厂
		getAction('/factory').then((res : any) => {
			if (res.code === 200) {
				factory.value = res.data.map((l : any) => ({ text: l.name, value: l.name }))
			}
		})
	})
	onShow(() => {
		list.value = []
		init()
		//用户信息
		uni.getStorage({
			key: 'info',
			success(res) {
				let data = JSON.parse(res.data)
				dataItem.value.shr = data.userName
			},
			fail: (err : any) => {
				console.log(err);
			}
		})
	})

	const onClickLeft = () => {
		history.back()
	}
	const onClickRight = () => {
		uni.navigateTo({
			url:'/pages/colorBlank/chemical'
		})
	}

	function init() {
		getAction('/testing', form.value).then((res : any) => {
			if (res.code === 200) {
				res.data.forEach((l:any)=>{
					let item=list.value.find(m=>m.trackingLogId==l.trackingLogId&&m.fabric==l.fabric&&m.status==l.status)
					if(item){
						item.len=(item.len*1+l.len*1).toFixed(1)
						item.ps++
						item.ids.push(l.id)
						item.mlxqs.push(l.mlxq)
					}else{
						l.ids=[l.id]
						l.ps=1
						l.mlxqs=[l.mlxq]
						if(l.zt=='待收货'){
							l.ztc='warning'
						}else if(l.zt=='待质检'){
							l.ztc='primary'
						}else if(l.zt=='待审核'){
							l.ztc='danger'
						}else if(l.zt=='已质检'){
							l.ztc='success'
						}
						list.value.push(l)
					}
				})
			}
		})
	}

	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]
		showPicker.value = false
	}
	//取消
	const showPickerCancel = () => {
		form.value[typeData.value] = ''
		showPicker.value = false
	}
	//弹窗开启事件
	const handleOpen = () => {
		if (typeData.value === 'factory') {
			popuList.value = factory.value
		} else if (typeData.value === 'zt') {
			popuList.value = [{ text: '待收货', 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) => {
		if (dataType.value == 'shrq') {
			dataItem.value[dataType.value] = formatDate(val)
		} else {
			form.value[dataType.value] = formatDate(val)
		}
		show.value = false
	}


	const pickerList = ref([])//面料数据
	const pickerContainerList = ref([])
	const showPickerList = ref(false)
	const searchValue = ref('')//查询的值
	const searchIndex = ref(0)//查询到的数组
	const mlList = ref([])
	//选择面料
	const selectChoose = () => {
		showPickerList.value = true
	}
	//面料开启
	const mlmcOpen = () => {
		pickerList.value = mlList.value
		pickerContainerList.value = pickerList.value
		searchValue.value = ''
	}
	//面料关闭
	const pickerCancel = () => {
		showPickerList.value = false
	}

	//搜索
	const selectedValue = ref()
	const getSeachList = () => {
		searchIndex.value = 0
		let reg = new RegExp(searchValue.value)
		let arr = []
		pickerList.value.forEach(l => {
			if (reg.test(l.text)) {
				arr.push(l)
			}
		})
		pickerContainerList.value = arr
	}
	//向上选择
	const upSearch = () => {
		if (searchIndex.value === 0 && pickerContainerList.value.length) {
			searchIndex.value = pickerContainerList.value.length - 1
			selectedValue.value = [pickerContainerList.value[searchIndex.value].text]
		} else if (pickerContainerList.value.length) {
			searchIndex.value--
			selectedValue.value = [pickerContainerList.value[searchIndex.value].text]
		} else {
			showToast('没有了！')
		}
	}
	//向下选择
	const downSearch = () => {
		if (pickerContainerList.value.length) {
			if (searchIndex.value === pickerContainerList.value.length) {
				searchIndex.value = 0
				selectedValue.value = [pickerContainerList.value[searchIndex.value].text]
			} else {
				searchIndex.value++
				selectedValue.value = [pickerContainerList.value[searchIndex.value].text]
			}
		} else {
			showToast('没有了！')
		}
	}
	//搜索框输入
	const timer = ref()
	const searchTo = () => {
		if (timer.value) {
			clearTimeout(timer.value)
		}
		timer.value = setTimeout(() => {
			getSeachList()
		}, 800)
	}
	const onConfirm = (val : any) => {
		form.value.fabric = val.selectedValues[2] + '/' + val.selectedValues[0] + val.selectedValues[1]
		pickerCancel()
	}


	const showDialog = ref(false)
	const itemList = ref({} as any)

	const submit = () => {
		let ids=itemList.value.ids.join(',')
		putAction('/testing/receive?ids='+ids+'&date='+dataItem.value.shrq).then((res:any)=>{
			if(res.code===200){
				showDialog.value = false
				list.value = []
				init()
			}
		})
	}

	//工序详情
	const toDetile = (item : any) => {
		itemList.value = item
		if(item.zt=='待收货'){
			showDialog.value = true
		}else if(item.zt=='待质检'||item.zt=='待审核'){
			uni.navigateTo({
				url:'/pages/colorBlank/ColorBlankQuality?item='+JSON.stringify(item),
			})
		}else if(item.zt=='已质检'){
			
		}
	}
	
	const computer = (data:any) =>{
		let date1 = new Date();
		let date2 = new Date(data);
		
		let time1 = date1.getTime();
		let time2 = date2.getTime();
		
		let diffInMs = time1 - time2;
		let diffInSeconds = Math.floor(diffInMs / 1000); // 转换为秒
		let diffInMinutes = Math.floor(diffInSeconds / 60); // 转换为分钟
		let diffInHours = Math.floor(diffInMinutes / 60); // 转换为小时
		let diffInDays = Math.floor(diffInHours / 24); // 转换为天
		return diffInDays
	}
</script>

<template>
	<view class="flex">
		<van-nav-bar title="色胚质检列表" left-text="返回" left-arrow @click-left="onClickLeft" right-text="新增"
			@click-right="onClickRight()" />
		<van-cell-group inset style="display: flex;">
			<van-field v-model="form.fabric" name="面料名称" label="面料名称" class="bor" colon label-width="5em" readonly
				@click="selectChoose" />
			<van-field v-model="form.makeUser" name="做单人员" label="做单人员" colon class="bor" label-width="5em" readonly
				@click="choosePic('makeUser')" />
		</van-cell-group>
		<van-cell-group inset style="display: flex;">
			<van-field v-model="form.color" name="颜色色号" label="颜色色号" colon class="bor-a" label-width="5em" />
			<van-field v-model="form.belongTo" name="归属人员" label="归属人员" colon class="bor" label-width="5em" readonly
				@click="choosePic('belongTo')" />
		</van-cell-group>
		<van-cell-group inset style="display: flex;">
			<van-field v-model="form.sd" name="谁定" label="谁&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;定" colon
				class="bor-a" label-width="5em" />
			<van-field v-model="form.factory" name="面料来源" label="面料来源" colon class="bor" label-width="5em" readonly
				@click="choosePic('factory')" />
		</van-cell-group>
		<van-cell-group inset style="display: flex;">
			<van-field v-model="form.zt" name="状态" label="状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态" 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 class="grid-container" v-for="(item,index) in list" :key="index">
				<view class="grid-item">
					{{item.fabric}}<br>{{item.color}}
				</view>
				<view class="grid-item">
					{{item.sd}}/{{item.makeUser}}/{{item.belongTo}}<br>{{item.ps}}匹/{{item.len}}米
				</view>
				<view class="grid-item">
					{{item.rc}}
				</view>
				<view class="grid-item" style="display: flex;flex-direction: column;">
					<van-button :type="item.ztc" @click="toDetile(item)">{{item.zt}}</van-button>
					<p v-if="item.receiveTime&&item.zt!='已质检'">已入库<span :style="{color: item.receiveTime>=2?'red':''}">{{computer(item.receiveTime)}}</span>天</p>
				</view>
			</view>
		</view>
	</view>

	<van-dialog v-model:show="showDialog">
		<view style="margin-top: 10rpx;">
			<van-cell-group inset>
				<van-field v-model="dataItem.shr" name="收货人" label="收货人" colon readonly label-width="5em" class="bor-n"
					label-align="right" />
				<van-field v-model="dataItem.shrq" name="收货日期" label="收货日期" colon label-width="5em" class="bor" readonly
					@click="chooseDate('shrq')" label-align="right" />
			</van-cell-group>
		</view>
		<template #footer>
			<view class="footer-button">
				<van-button plain size="small" style="width: 25vw;" @click="showDialog=false">
					取消
				</van-button>
				<van-button size="small" type="success" style="width: 25vw;" @click="submit">
					提交
				</van-button>
			</view>
		</template>
	</van-dialog>

	<!--选择框-->
	<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)" />

	<!--    面料选择框-->
	<van-popup v-model:show="showPickerList" position="bottom" @open="mlmcOpen">
		<view class="select-model">
			<van-picker :columns="pickerContainerList" @cancel="pickerCancel" v-model="selectedValue"
				@confirm="onConfirm" />
			<view>
				<view class="top-select">
					<view class="confirm-select">
						<p>请选择品种</p>
					</view>
					<view class="search-box">
						<view>
							<van-search placeholder="请输入品种名字" v-model="searchValue" label="面料搜索：" background="#ffffff"
								@input="searchTo()" :clearable="false" />
						</view>
						<view class="flex-btn">
							<button class="search-btn" @click="upSearch()">↑</button>
							<button class="search-btn" @click="downSearch()">↓</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</van-popup>
</template>

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

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

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

			.grid-item {
				border: 1px solid #f2f2f2;
				text-align: center;
				white-space: pre-line;
				padding: 5rpx;
				word-break: break-all;
				display: flex;
				justify-content: center;
				align-items: center;
				/*将对象转为弹性盒模型展示*/
				-webkit-box-orient: vertical;
				/*设置弹性盒模型子元素的排列方式*/
				overflow: hidden;
				/*超出隐藏*/
			}
		}

		.content {
			flex: 1;
			overflow-y: scroll;
			font-size: 24rpx;
		}
	}

	::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;
		font-size: 22rpx;
	}

	::v-deep .van-field__control {
		text-align: center;
		font-size: 16px;
	}

	.footer-button {
		margin: 20rpx;
		display: flex;
		justify-content: space-around;
	}

	/*面料选择搜索框*/
	.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>