<template>
	<view class="app-container">
		<uni-nav-bar dark :fixed="true" shadow background-color="#007AFF" status-bar left-icon="left" left-text="返回"
					title="移位任务" @clickLeft="handleBack" />
		<view class="scan-header">
			<uni-forms-item  name="stockCode" label="单号" labelAlign="right" 
					style="margin-bottom: 0px; padding: 0px 15px 0px 15px;" >
				<uni-easyinput type="text" :inputBorder="true" v-model="stockCode"
					:clearable="false" ></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="dateRange" label="日期" labelAlign="right" 
					style="margin-bottom: 0px; padding: 0px 15px 0px 15px;" >
				<uni-datetime-picker v-model="dateRange" type="daterange"/>
			</uni-forms-item>
			<view class="button-group">
				<button type="default" size="mini" @click="setDateRange(-1, -1)">昨日</button>
				<button type="default" size="mini" @click="setDateRange(0, 0)">今日</button>
				<button type="default" size="mini" @click="setDateRange(1, 1)">明日</button>
				<button type="warn" size="mini" @click="openStockMove">进入</button>
				<button type="primary" size="mini" @click="getList">查询</button>
			</view>
		</view>
		<scroll-view class="mxpop_scroll" scroll-y="true">
			<!-- <uni-section class="mb-10" :title="sectionTitle"  type="line"> -->
				<view class="uni-list" style="margin-bottom: 60px;">
					<slot v-for="(item, index) in listData">
						<view class="uni-list-cell"
							hover-class="uni-list-cell-hover"
							:class="{ 'selected': rkid === item.id }" 
						    :key="index" @click="goProDetail(item, index, $event)"
						>
							<view style="display: flex;">
								<view class="detailList">{{ item.taskCode }}&emsp;|&emsp;{{ item.taskDate }}&emsp;|&emsp;{{ item.billType.billTypeName }}
							</view>
							</view>
							<view style="display: flex;">
								<view class="detailList">调出仓：{{ item.warehouseNameOut }}&emsp;调入仓：{{ item.warehouseNameIn }}</view>
							</view>
							<view style="display: flex;" v-if="item.customerNameIn">
									<view class="detailList">移入客户：{{item.customerNameIn}}</view>
							</view>
							<view style="display: flex;" v-if="item.extendCode01 || item.extendCode02">
								<view class="detailList">看板号：{{item.extendCode01}}&emsp;交货时间：{{ item.extendCode02 }}</view>
							</view>
						</view>
						 <view class="s-line" /> 
					</slot>
				</view>
			<!-- </uni-section> -->
		</scroll-view>
	</view>
</template>
 	
<script>
	
	import { listHeadTaskMove,queryBillTypeCache } from '@/api/wms/stockMove.js'
	import storage from '@/utils/storage'
	
	export default {
		async onLoad(option) {
			try {
				//查询移位单单据类型
				const res = await queryBillTypeCache({billCode: "stockMove"});
				let tmpData = res.rows;
				this.billTypeOption = tmpData.filter(item => item.isEnableApp == 0 && item.status == 0)
				                .map(item => ({
				                  billTypeCode: item.billTypeCode,
				                  billTypeName: item.billTypeName,
								  value: item.billTypeCode,
								  text: item.billTypeName,
								  taskValue: item.taskBillTypeCode ? "'" + item.taskBillTypeCode.replace(/,/g, "','") + "'" : '',
								  isControlTask: item.isControlTask,
				                }));
												console.log(this.billTypeOption)
			} catch (error) {
				
			}
		},
		onUnload(){},
		data() {
			return {
				// 筛选
				stockCode: "",
				dateRange: ['', ''],
				
				listData: "", // 数据list
				rkid: "", // 当前选中数据
				rkindex: "", // 选中数据下标
				isNavigating: false, // 用来检查是否正在导航
				billTypeOption: [],  // 业务类型字典选择框数据
			}
		},
		created(){
		  this.setDateRange(-30, 0)
		},
		methods: {
			getList() {
				let that = this;
				let queryParams = {
					taskCode: that.stockCode,
					params: { "beginTime": this.dateRange[0],"endTime": this.dateRange[1] },
				}
				listHeadTaskMove(queryParams).then(res => {
					if(res.code === 200){
						const newData = res.rows.map((item) => {
							// 在每个对象中插入新变量  
							item['selected'] = false;
							// 查询任务对应单据类型
							item['billType'] = item.billTypeCode ? that.billTypeOption.find(item => item.taskValue.includes("'"+ item.billTypeCode +"'")) : {};
							// 返回修改后的对象  
							return item;  
						});  
						this.listData = newData;
					}
				});
				//清空选中
				this.rkid = "";
				this.rkindex = "";
			},
			openStockMove(){
				if(this.rkid && this.rkindex>=0){
					let billTypeCode = this.listData[this.rkindex].billType.billTypeCode
					billTypeCode = billTypeCode==undefined || billTypeCode=='' ? '' : billTypeCode
					
					let path='/pages/menu/stockMove/index?ywlx=' + billTypeCode + '&taskCode=' + this.listData[this.rkindex].taskCode
					this.navClick(path);
				}
			},
			navClick(path) {
				if (this.isNavigating) {
					// 如果正在导航，则不执行任何操作
					return;
				}
			
				this.isNavigating = true; // 设置正在导航状态
			
				uni.navigateTo({
					url: path,
					success: () => {
						// 导航成功，可以在这里做一些操作，比如发送全局事件等
						this.isNavigating = false; // 重置导航状态
					},
					fail: () => {
						// 导航失败，同样重置导航状态
						this.isNavigating = false;
					}
				});
			},
			goProDetail(item, index, event){ //选中行
				try{
					if( this.rkid == item.id ){
						this.rkid = "";
						this.rkindex = "";
					}else{
						this.rkid = item.id;
						this.rkindex = index;
					}
				}catch{
					this.rkid = "";
					this.rkindex = "";
				}
			},
			setDateRange(start, end){ // 赋值日期期间
				let date1 = this.getYesterdayDate(start);
				let date2 = this.getYesterdayDate(end);
				this.dateRange = [date1, date2];
				this.getList();
			},
			getYesterdayDate(num) {// 获取当前日期
				let today = new Date();
				// 计算日期
				let yesterday = new Date(today);
				yesterday.setDate(yesterday.getDate() + parseInt(num));
				// 将日期格式化为YYYY-MM-DD格式的字符串
				let formattedDate = `${yesterday.getFullYear()}-${('0' + (yesterday.getMonth() + 1)).slice(-2)}-${('0' + yesterday.getDate()).slice(-2)}`;
				return formattedDate;
			},
			handleBack(){
				uni.showModal({
					title: '提示',
					content: '确认返回?',
					success: function (res) {
						if (res.confirm) {
							//用户点击确定;
							uni.navigateBack();
						} else if (res.cancel) {
							//用户点击取消;
						}
					}
				});
			},
		}
	}
</script>
 
<style>
	page{
		height: 100%;
	}
	
	.app-container{
		height: 100vh;
		display: flex; 
		flex-direction: column;
		overflow: hidden;
		background: white;
	}
	.scan-header{
		position: fixed;
		/* #ifndef APP-PLUS */
			top: 45px;
		/* #endif */
		/* #ifdef APP-PLUS */
			top: 80px;
		/* #endif */
		width: 100%;
		background-color: white;
		z-index: 999;
	}
	
	.scroll-Y {
		/* height: 100%; */
		/* #ifndef APP-PLUS */
			margin-top: 44px;
			height: calc(100vh - 135px); 
		/* #endif */
		/* #ifdef APP-PLUS */
			margin-top: 54px;
			height: calc(100vh - 175px); 
		/* #endif */
		
	}
	
	::v-deep .uni-forms-item{
		margin-bottom: 5px !important;
	}
	
	.detailList {
		margin-left: 4px;
		color: #333333;
		font-size: 13px;
		height: auto;
		line-height: 26px;
		width: 100vw;
	}
	
	.s-line{
		top: 0;
		right: 0;
		left: 0;
		height: 1px;
		margin-top: 5px;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		background-color: #e5e5e5;
		z-index: 1;
	}
	
	.selected {
	  background-color: #f8edb8;
	}
	
	.uni-list-cell {
		flex-direction: column;
		// margin-top: 10px;
		// background-color: white;
		padding: 8px 12px;
	}
	
	.mxpop_scroll{
		height: calc(100vh - 125px);
		margin-top: 130px;
	}
	
	/* 按钮行 */
	.button-group {
		margin-top: 5px;
		display: flex;
		justify-content: space-around;
	}
	
</style>
