<template>
	<!-- 列表展示标准模版 -->
	<view class="container">
				<view>
					<u-subsection :list="tabsList" v-model="current" @change="tabsChange"></u-subsection>
					
<!-- 		<u-tabs-swiper :list="tabsList" :is-scroll="true" :current="current" @change="tabsChange"></u-tabs-swiper> -->
				</view>
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index" @click="openDetail(item)">
				<view class="title">
					<view class="title-txt">
						{{item.reworkbillNo}}
					</view>
					<view class="time">
						{{`${$time.formatDate(item.createTime)}`}}
					</view>
				</view>
				<view class="dec">
					<view>处理状态：</view>
					<view>
						<u-tag text="待处理"  type="warning" mode="light" shape="circle" v-if="item.status==1" :style="{ width: '65px' }" />
						<u-tag text="处理中" type="primary" mode="light" shape="circle" v-if="item.status==2" :style="{ width: '65px' }" />
						<u-tag text="已完成" type="success" mode="light" shape="circle" v-if="item.status==3" :style="{ width: '65px' }" />
						<u-tag text="已终止" type="error" mode="light" shape="circle" v-if="item.status==-1" :style="{ width: '65px' }" />
					</view>
				</view>
				<view class="dec">
					<view>产品编码：</view><view>{{item.productionCode}}</view>
				</view>
				<view class="dec">
					<view>工作类型：</view><view>{{item.status == '1'?'返工' :'返修'}}</view>
				</view>
				<view class="dec">
					<view>是否有替换件：</view><view>{{item.replaceFlag == 'TRUE'?'是' :'否'}}</view>
				</view>
				<view class="dec">
					<view>返修人员：</view><view>{{item.reworkPersoncode}}</view>
				</view>
				<view class="dec" v-if="item.status!=1">
					<view>返修时间：</view><view>{{`${$time.formatDate(item.reworkTime)}`}}</view>
				</view>
				<view class="dec" v-if="item.status==3">
					<view>返修结果：</view><view>{{item.reworkResult}}</view>
				</view>
				<view>
						<u-button v-if="item.status==1"  size="medium" type="primary" @click="handleItem(item)" >处理</u-button>
						<!-- <u-button v-if="item.status==2"  class="btn"  size="medium" type="warning" @click="handleRefuse(item)" >退回</u-button> -->
						<u-button v-if="item.status==2"    size="medium" type="error" @click="handleSuspend(item)" >中止</u-button>
						<u-button v-if="item.status==2"  size="medium" type="success" @click="handleSuccess(item)" >完成</u-button>
						<!-- <u-button v-if="item.status==3"  class="btn"  size="medium"  type="warning" @click="handleRefuse(item)" >退回</u-button> -->
				</view>
			</view>
			<view style="height: 94rpx;padding-top: 30rpx;">
				<u-loadmore :status="status" v-if="status != 'loadmore'" />
			</view>
		</view>
		<view style="height: constant(safe-area-inset-bottom); height: env(safe-area-inset-bottom);"></view>
		<requestButton @goScan='openScanDetailPopup'></requestButton>
	</view>
	<u-popup v-model="showType" mode="bottom" border-radius="14" length="30%">
		
			<view style="margin-top: 30px; margin-left: 30px;">
				<u-input v-model="reworkPersoncode" placeholder="请输入返工返修人员" />
			</view>
			<view class="btns">
				<u-button class="sure" @click="handleReceive">确定</u-button>
			</view>
	</u-popup>
</template>

<script setup lang="ts">
	import requestButton from '@/mycomponents/button/requestButton.vue'
	/*初始化*/
	import {
		onLoad,
		onShow,
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	/*引入API*/
	import * as reworkSingleApi from "@/api/mes/reworkSingle/reworkSingleApi.ts"
	const { proxy } = getCurrentInstance()
	const paramData = ref()
	const tabsList = ref([{name:'待返修'},{name:'返修中'},{name:'已完成'},{name:'已终止'}])
	// const tabsList = ref([{name:'待处理'},{name:'已完成'}])
	const reworkPersoncode = ref()
	const showType = ref(false)
	const current = ref(0)
	/*分页参数*/
	const params = ref({
		pageNo: 1,
		pageSize: 10,
		status: 1
	})
	/*是否显示"没有更多了"*/
	const status = ref('loadmore')
	/*列表数据集*/
	const list = ref([])
	/*列表调用API方法*/
	async function getList() {
		if (status.value == 'nomore') return;
		status.value = 'loading';
		proxy.$modal.loading('加载中')
		await reworkSingleApi.getPage(params.value).then((res) => {
			proxy.$modal.closeLoading()
			if (res.data.list.length > 0) {
				list.value = list.value.concat(res.data.list);
				params.value.pageNo++;
				status.value = 'loadmore'
			} else {
				status.value = 'nomore'
			}
		}).catch(() => {
			proxy.$modal.closeLoading()
		})
	}
	// 打开处理人员和处理时间弹窗
	function handleItem(index){
		reworkPersoncode.value = null
		paramData.value = index	
		showType.value = true
	}
	// “去处理”按钮提交事件
	function handleReceive(){
		if(reworkPersoncode.value == '' && reworkPersoncode.value.trim() == ''){
			proxy.$modal.showToast('请输入返修人员')
			reworkPersoncode.value = null
			return;
		}
		else{
			paramData.value.reworkPersoncode=reworkPersoncode.value
			changeStatus(2)
			showType.value = false
		}
	}
	// 终止按钮提交事件
	function handleSuspend(index){
		proxy.$modal.confirm('确定提交吗').then(() => {
			paramData.value = index
			changeStatus(-1)
		})
	}
	// 完成按钮提交事件
	function handleSuccess(index){
		proxy.$modal.confirm('确定提交吗').then(() => {
			paramData.value = index
			changeStatus(3)
		})
	}
	// 撤回按钮提交事件
	function handleRefuse(index){
		proxy.$modal.confirm('确定提交吗').then(() => {
			paramData.value = index
			changeStatus(1)
		})
	}
	
	// 状态变更通用方法
	function changeStatus(val){
		proxy.$modal.loading('加载中')
		paramData.value.status = val
		reworkSingleApi.update(paramData.value).then((res) => {
			proxy.$modal.closeLoading()
			if (res.data) {
				list.value=[]
				proxy.$modal.showToast('成功')
				setTimeout(() => {
					proxy.$modal.closeLoading()
					params.value.pageNo = 1
					params.value.pageSize = 5
					getList();
				}, 500)
			} else {
				proxy.$modal.showToast('失败')
				proxy.$modal.closeLoading()
			}
		}).catch(() => {
			proxy.$modal.closeLoading()
		})
	}
	
	function tabsChange(curr){
		if(curr == 3 ){
			params.value.status = -1 
		}else{
			
			params.value.status = (curr + 1)
		}
		current.value = curr
		params.value.pageNo = 1
		params.value.pageSize = 5
		list.value = []
		status.value = 'loadmore'
		getList();
	}
	/*打开详情页*/
	function openDetail(item) {
		if(item.ststus==3){
			// const objString = JSON.stringify(item);
			// proxy.$tab.navigateTo(`/pages/mes/reworkSingle/detail?obj=${encodeURIComponent(objString)}`)
			// proxy.$modal.loading('正在打开详情页')
			// setTimeout(function() {
			//     proxy.$modal.closeLoading();
			// }, 500);
		}
	}
	function openScanDetailPopup() {
		proxy.$tab.navigateTo(`/pages/mes/reworkSingle/create`)
		proxy.$modal.loading('正在打开')
		setTimeout(function() {
		    proxy.$modal.closeLoading();
		}, 500);
	}
	/*通用方法*/
	onShow(() => {
		params.value.pageNo = 1
		list.value = []
		status.value = 'loadmore'
		getList()
	})
	onReachBottom(() => {
		getList()
	})
	
</script>

<style lang="scss" scoped>
	.btn{
		margin-left:5px;
		width:20px
	}
	.container{
		background: #f5f5f5;
		min-height: 100vh;
	}
	.list {
		background: #f5f5f5;
		margin-top: 20rpx;
		.item {
			padding: 30rpx 30rpx 0px 30rpx;
			margin-top: 20rpx;
			background: white;
			position: relative;
			.title {
				display: flex;
				align-items: center;
				padding-bottom: 20rpx;
				.title-txt {
					color: #409eff;
					font-weight: bold;
					font-size: 36rpx;
					width: 0px;
					flex: 1;
					word-wrap: break-word;
				}
				.time {
					color: #919191;
				}
			}
			.dec {
				padding-bottom: 20rpx;
				display: flex;
				align-items: center;
				view {
					&:nth-child(1){
						width: 160rpx;;
					}
					&:nth-child(2){
					color: #999999;
						flex: 1;
						width: 0px;
						word-wrap: break-word;
					}
				}
			}
			.last {
				padding-bottom: 30rpx;
			}
			.bottom {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-top: 1px solid #E4E4E4;
				padding: 20rpx 0px;
				height: 90rpx;
			}
		}
	}
	
	.btns {
		display: flex;
	
	
		button {
			flex: 1;
		}
	
		.sure {
			background: #409eff;
			color: white;
			border-radius: 0px;
			margin-top: 60px;
	
			&::after {
				border: 1px solid #409eff;
				border-radius: 0px;
			}
		}
	
		.reset {
			background: #ff7a45;
			border-radius: 0px;
	
			&::after {
				border-radius: 0px;
			}
		}
	}
	
	
</style>