<template>
  <view class="page">
    <u-navbar class="u-navbar" title="人员转场" placeholder @leftClick="leftClick" leftIconColor="#fff" rightIcon="list" @rightClick="goRecord"  bgColor="#00337A"  :titleStyle="{ color: '#FFF', fontSize: '32rpx' }">
    	<view class="u-nav-slot" slot="right">
    		<text style="color: #FFF;">记录</text>
    	</view>
    </u-navbar>
	<view class="tab-box">
		<view class="tab-item" :class="currentTabIndex == 0 ? 'active' : ''" @click="currentTabIndex = 0">
			查询
			<view class="activeLine" v-if="currentTabIndex == 0"></view>
		</view>
		<view class="tab-item" :class="currentTabIndex == 1 ? 'active' : ''">
			转场
			<view class="activeLine" v-if="currentTabIndex == 1"></view>
		</view>
	</view> 
	
	<scroll-view class="content" scroll-y="true">
		<view class="view-box" v-show="currentTabIndex == 0">
			 <view style="display: flex;flex-direction: column;padding: 0 20rpx;font-size: 28rpx;color: red;">
			 	<text>注：</text>
			 	<text>1.人员转场成功后，在原工程、分包、班组自动解绑，合同自动失效。</text>
			 	<text>2.自动入场新的工程、分包、班组，请尽快更新合同信息。</text> 
			 	<text>3.班组长及班组骨干人员不可使用转场功能，请按照出场-入场流程完成转场。</text>
			 </view> 
			 
			<u--form class="form-box" :model="searchData" ref="sForm">
			 	<view class="form-input-box"> 
			 		<u-form-item prop='name' style="width:100%;height: 80rpx;" >
			 			<u--input v-model="searchData.name" type="text" style="background: #FFF;" placeholder="请输入人员姓名" maxlength="20"  border="surround" clearable></u--input>
			 		</u-form-item>
			 	</view>
				<view class="form-input-box">
					<u-form-item prop='idNumber' style="width:100%;height: 80rpx;" >
						<u--input v-model="searchData.idNumber" type="text" style="background: #FFF;" placeholder="请输入人员身份证号" maxlength="20"  border="surround" clearable></u--input>
					</u-form-item>
				</view>
				<view class="form-input-box">
					<u-form-item prop='proId' style="width:100%;height: 80rpx;" >
						<!-- <uni-data-select v-model="searchData.proId" :localdata="proList"></uni-data-select> -->
						<searchSelect :disabled="false"
							v-model="searchData.proId" :localdata="proList"  style="background: #FFF;"
							dataKey="name" dataValue="id" placeholder="请选择工程" 
							clearable="false" :filterable="true" ></searchSelect>
					</u-form-item>
				</view>
				<view class="form-input-box" style="position: relative;height: 100rpx;">
					<view class="searchBtn" style="position: absolute;right: 40rpx;top:20rpx;" @click="searchPerson">查询</view>
				</view>
			</u--form>
			
			<view v-if="personData.name">
				<view style="color: red;margin: 10rpx;">履历记录</view>
				<view class="view-item">
					<view class="label">姓名</view>
					<view>{{personData.name}}</view>
				</view>
				<view class="view-item">
					<view class="label">工种</view>
					<view>{{personData.postName}}</view>
				</view>
				<view class="view-item">
					<view class="label">所属班组</view>
					<view>{{personData.teamName}}</view>
				</view>
				<view class="view-item">
					<view class="label">所属分包</view>
					<view>{{personData.subName}}</view>
				</view>
				<view class="view-item">
					<view class="label">所属工程</view>
					<view>{{personData.proName}}</view>
				</view>
				<view class="view-item">
					<view class="label">骨干人员</view>
					<view v-if="personData.isRegularPerson==0">否</view>
					<view v-if="personData.isRegularPerson==1">是</view>
				</view>
				<view class="view-item">
					<view class="label">入场时间</view>
					<view>{{personData.einTime}}</view>
				</view>
				<view class="view-item">
					<view class="label">出场时间</view>
					<view>{{personData.exitTime}}</view>
				</view>
				
				<view class="sumbit-btn" @click="goCutTo">下一步</view>
			</view>
		</view>
		
		<view class="view-box" v-show="currentTabIndex == 1">
			<view style="display: flex;flex-direction: column;padding: 0 20rpx;font-size: 28rpx;color: red;">
				<text>选择转入的工程、分包、班组(请依次选择分包、工程、班组)</text>
			</view> 
			
			<u--form class="form-box" :model="cutToData" ref="cForm">
			 	<view class="form-input-box"> 
			 		<u-form-item prop='proId' style="width:100%;height: 80rpx;" >
			 			<searchSelect :disabled="false" @change="chosenPro"
			 				v-model="cutToData.proId" :localdata="proList"  style="background: #FFF;"
			 				dataKey="name" dataValue="id" placeholder="请选择转场工程" 
			 				clearable="false" :filterable="true" ></searchSelect>
			 		</u-form-item>
			 	</view>
				<view class="form-input-box">
					<u-form-item prop='subId' style="width:100%;height: 80rpx;" >
						<searchSelect :disabled="false" @change="changeSub"
							v-model="cutToData.subId" :localdata="subList"  style="background: #FFF;"
							dataKey="name" dataValue="id" placeholder="请选择转场分包" 
							clearable="false" :filterable="true" ></searchSelect>
					</u-form-item>
				</view>
				<view class="form-input-box">
					<u-form-item prop='teamId' style="width:100%;height: 80rpx;" >
						<searchSelect :disabled="false" @change="changeTeam"
							v-model="cutToData.teamId" :localdata="teamList"  style="background: #FFF;"
							dataKey="name" dataValue="id" placeholder="请选择转场班组" 
							clearable="false" :filterable="true" ></searchSelect>
					</u-form-item>
				</view>
				<view class="form-input-box">
					<view style="width:20%;height: 100%;margin-top:20rpx;margin-left:20rpx;">
						<text>是否骨干</text>
					</view>
					<u-form-item prop='isRegularPerson' style="width:100%;height: 80rpx;" >
						<u-radio-group v-model="cutToData.isRegularPerson" placement="row" style="border: 1rpx solid #ccc;padding: 16rpx;border-radius: 10rpx;background: #FFF;">
							<u-radio label="否" name="0" style="margin-right: 40rpx;"></u-radio>
							<u-radio label="是" name="1"></u-radio>
						</u-radio-group>
					</u-form-item>
				</view>
			</u--form>
			
			<view class="sumbit-btn" @click="hanleCutTo">确认转场</view>
		</view>
		
	</scroll-view>
	
	
 
  </view>
</template>

<script>
import { pathToBase64, base64ToPath } from 'image-tools';
import searchSelect from '@/components/search-select.vue';
import config from '@/config'
export default {
	components: {
		searchSelect
	},
	data() {
		return { 
			currentTabIndex:0,
			searchData:{
				name:"",proId:"",idNumber:"",type:"2"
			},
			personData:{},
			cutToData:{
				proId:"",
				subId:"",
				teamId:"",
				isRegularPerson:"0",
			},
			proList:[],
			subList:[],
			teamList:[],
 
		}
	},
	onLoad(option) { 
		this.getproList(); 
	},
	onShow() {
		 
	},
	watch: {
		currentTabIndex: function(newVal, oldVal) {
			if (newVal == 0) {
				 
			} else if(newVal == 1) {
				
			}  
		}
	},
	methods: {
		// 获取工程名称
		getPro(){
			let param={
				id:this.proId,
				subId:-1
			}
			uni.request({
			  url: config.realAppUrl + '/offLine/getPro',
			  method: 'post',
			  data: param,
			  header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
				res = res.data;
				if(res.code==200){
					console.log(res)
					this.proName = res.data[0].abbreviation||"";
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
		},
		//获取工程
		getproList(){
			uni.request({
			  url: config.realExamUrl+'/select/getProByTeamId',
			  method: 'get',
			  data: {},
			  header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
				  console.log(res)
				res = res.data;
				if(res.code==200){
					this.proList=res.data;
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
		},
		//查询人员信息
		searchPerson(){
			if(this.searchData.name==""){
				uni.$u.toast('请填写人员姓名');
			}else if(this.searchData.idNumber==""){
				uni.$u.toast('请填写人员身份证号');
			}else if(!this.verifyidNumber(this.searchData.idNumber)){
				uni.$u.toast('人员身份证号不正确');
			}else if(this.searchData.proId==""){
				uni.$u.toast('请选择工程');
			}else{
				uni.request({
					url: config.realBmwUrl+'/cutToPlace/getWorkerNowMsg', 
					method: 'post',
					data: this.searchData,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						Authorization: uni.getStorageSync('realNameToken')	
					},
					success: res => {
					console.log(res) 
						res = res.data;
						if(res.code==200){
							this.personData=res.data[0];
							this.personData.idNumber=this.searchData.idNumber;
						}else{
							this.personData={}
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					},
					fail: err => { 
						console.log(err)
					}
				}) 
				
				
				
				
				
			}	
		},
		//转场
		goCutTo(){
			if(this.personData.contractId==""){
				uni.$u.toast('无合同,出场后将无法计算工资，请去补全合同,才能转场!');
			}else if(this.personData.isPlan>0){
				uni.$u.toast('今天填报了日计划,无法转场!');
			}else if(this.personData.isRegularPerson=='1'){
				uni.$u.toast('骨干人员禁止强制转场!');
			}else if(this.personData.isTeamMan!='-1'){
				uni.$u.toast('班组长禁止强制转场!');
			}else{
				this.currentTabIndex=1
			}
		},
		//身份证校验
		verifyidNumber(idNumber) {
		  // 校验长度
		  if (idNumber.length !== 18) {
		    return false;
		  }
		  // 校验前17位是否为数字
		  if (!/^\d{17}$/.test(idNumber.substr(0, 17))) {
		    return false;
		  }
		  
		  // 校验最后一位
		  var weightFactor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
		  var checkCodeList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
		  var checkSum = 0;
		  for (var i = 0; i < 17; i++) {
		    checkSum += parseInt(idNumber.charAt(i)) * weightFactor[i];
		  }
		  var checkCodeIndex = checkSum % 11;
		  if (idNumber.charAt(17).toUpperCase() !== checkCodeList[checkCodeIndex]) {
		    return false;
		  }
		  
		  return true;
		},
		chosenPro(e){
			console.log(e)
			this.cutToData.proNameN=e.name;
			this.getSubList(e.id)
		},
		//获取分包商
		getSubList(proId){
			let param={
				proId:proId
			}
			uni.request({
			  url: config.realExamUrl+'/select/getSubByProId',
			  method: 'get',
			  data: param,
			  header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
				console.log(res)
				res = res.data;
				if(res.code==200){
					this.subList=res.data
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
		},
		changeSub(e){
			console.log(e)
			this.cutToData.subNameN=e.name;
			this.getTeamList(e.id)
		},
		//获取班组
		getTeamList(subId){
			let param={
				subId:subId
			}
			uni.request({
			  url: config.realExamUrl+'/select/getTeamBySubId',
			  method: 'get',
			  data: param,
			  header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				Authorization: uni.getStorageSync('realNameToken')
			  },
			  success: res => {
				console.log(res)
				res = res.data;
				if(res.code==200){
					this.teamList=res.data;
				}
			  },
			  fail: err => {
				console.log(err)
			  }
			})
			 
		},
		changeTeam(e){ 
			console.log(e.id)
			this.cutToData.teamNameN=e.name;
		},
		//确认转场
		hanleCutTo(){
			if(this.cutToData.proId==""){
				uni.$u.toast('请选择转场工程!');
			}else if(this.cutToData.subId==""){
				uni.$u.toast('请选择转场分包!');
			}else if(this.cutToData.teamId==""){
				uni.$u.toast('请选择转场班组!');
			}else{
				uni.showModal({
					title: `确认是否转场？`,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) { 
							this.confirmCutTo()
						}
					}
				});
			}
		},
		//转场接口
		confirmCutTo(){ 
			let param={
				idNumber:this.personData.idNumber,
				name:this.personData.name,
				phone:this.personData.phone,
				proId:this.cutToData.proId,
				subId:this.cutToData.subId,
				teamIdN:this.cutToData.teamId,
				isRegularPerson:this.cutToData.isRegularPerson,
				postId:this.personData.postId,
				proName:this.personData.proName,
				subName:this.personData.subName,
				teamName:this.personData.teamName,
				proNameN:this.cutToData.proNameN,
				subNameN:this.cutToData.subNameN,
				teamNameN:this.cutToData.teamNameN,
				operator:uni.getStorageSync('realNameUser').userId,
				operateRole:uni.getStorageSync('realNameUser').roleName,
			}
			console.log(param)
			uni.request({
				url: config.realBmwUrl+'/cutToPlace/sureCutToPlace', 
				method: 'post',
				data: param,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
					Authorization: uni.getStorageSync('realNameToken')	
				},
				success: res => {
				console.log(res) 
					res = res.data;
					if(res.code==200){
						uni.showToast({
							title: '转场成功！',
							icon: 'none'
						})
						this.currentTabIndex=0;
						this.personData={};
						this.searchData={
							name:"",proId:"",idNumber:"",type:"2"
						};
						this.cutToData={
							proId:"",
							subId:"",
							teamId:"",
							isRegularPerson:"0",
						};
					}else{ 
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				},
				fail: err => { 
					console.log(err)
				}
			}) 
		},
		//记录按钮-跳转记录
		goRecord(){
			uni.navigateTo({
				url: `/pages/realName/workbench/cutToPlace/record`
			})
		},
		leftClick() {
			console.log('返回')
			uni.navigateBack({
				delta: 1 // 返回
			});
		},
		timeFormat(dateTime = null, formatStr = 'yyyy-mm-dd') {
			let date
			// 若传入时间为假值，则取当前时间
			if (!dateTime) {
				date = new Date()
			}
			// 若为unix秒时间戳，则转为毫秒时间戳（逻辑有点奇怪，但不敢改，以保证历史兼容）
			else if (/^\d{10}$/.test(dateTime?.toString().trim())) {
				date = new Date(dateTime * 1000)
			}
			// 若用户传入字符串格式时间戳，new Date无法解析，需做兼容
			else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) {
				date = new Date(Number(dateTime))
			}
			// 处理平台性差异，在Safari/Webkit中，new Date仅支持/作为分割符的字符串时间
			// 处理 '2022-07-10 01:02:03'，跳过 '2022-07-10T01:02:03'
			else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) {
				date = new Date(dateTime.replace(/-/g, '/'))
			}
			// 其他都认为符合 RFC 2822 规范
			else {
				date = new Date(dateTime)
			}
		
			const timeSource = {
				'y': date.getFullYear().toString(), // 年
				'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月
				'd': date.getDate().toString().padStart(2, '0'), // 日
				'h': date.getHours().toString().padStart(2, '0'), // 时
				'M': date.getMinutes().toString().padStart(2, '0'), // 分
				's': date.getSeconds().toString().padStart(2, '0') // 秒
				// 有其他格式化字符需求可以继续添加，必须转化成字符串
			}
		
			for (const key in timeSource) {
				const [ret] = new RegExp(`${key}+`).exec(formatStr) || []
				if (ret) {
					// 年可能只需展示两位
					const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0
					formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex))
				}
			}
			return formatStr
		},
	},
}
</script>
 
<style lang="scss">
  
.page {
	width: 100vw;
	height: 100vh;
	background-color: #EFEFEF;
	box-sizing: border-box;
	.tab-box {
		width: 100%;
		margin: 0rpx auto;
		padding-top: 20rpx; 
		display: flex;
		justify-content: space-between;
		background-color: #FFF;
		.tab-item {
			height: 70upx;
			width: 45%;
			font-size: 30upx;
			text-align: center;
			line-height: 70upx;
			color: #666;
		}
		.active {
			color: #00337A;
			border-radius: 10upx 10upx 0 0;
			font-weight: bold;	
		}
		.activeLine{
			background: #00337A;
			border-radius: 10upx;
			width: 100%;
			height: 6upx;
		}
	}
	.searchBtn{
		width: 20%;
		height: 50rpx;
		line-height: 50rpx;
		border-radius: 10rpx;
		background: #0078FB;
		color: #FFF;
		padding: 10rpx 16rpx;
		text-align: center;
	}
	.content{
		width: 100%;
		height: 84vh;
		margin-top: 20rpx;
		// padding-bottom: 80rpx;
		background-color: #EFEFEF;
	}
	 
	
	.view-box{
		width: 100%;
		height: auto;
		margin: 0rpx auto;
		border-radius: 10rpx;
		padding-top: 20rpx;
		// background-color: red;
		
		.form-box{
			width: 100%;
			height: auto;
			font-size: 26rpx; 
			margin-top: 20rpx;
			.form-input-box{
				padding:10rpx 0rpx; 
			}
		}
		
		
		
		.view-item{
			width: 94%;
			margin: 0rpx auto;
			padding:10rpx;
			display: flex;
			border-bottom: 1rpx solid #EFEFEF;
			font-size: 26rpx;
			.label{
				width: 120rpx;
				color: #666;
				margin-bottom: 10rpx;
				font-weight: bold;
			}
		} 
		.sumbit-btn{
			width: 94%;
			height: 80rpx;
			margin: 20rpx auto;
			margin-bottom: 80rpx;
			display: flex;align-items: center;justify-content: center;
			background: #0078FB;
			color: #FFF;
			border-radius: 10rpx;
		}
	}
 
 
 

}
</style>
