<template>
	<view class="pad-x120">
		<!-- 头部 -->
		<status-nav navBarTitle="编辑加班"></status-nav>
		<view class="content" :style="{'padding-top':statusHeight+50+'px'}" v-if="isLoding">
			<view class="overtime-from font26">
				<view class="item">
					<view class="title">工地</view>
					<picker class="input" mode="selector" :range="worksiteList" @change="bindWorksiteChange" :value="worksiteIndex" :range-key="'name'">
						<view class="name">
							<text>{{worksiteList[worksiteIndex].name}}</text>
							<image src="/static/icon/icon-arrow-02.png" mode="aspectFit"></image>
						</view>
					</picker>
				</view>
				<view class="item">
					<view class="title">日期</view>
					<picker class="input" mode="date" :range="date" @change="bindDateChange" :start="startDate" :end="endDate">
						<view class="name">
							<text>{{date.split('-')[0]+'年'+date.split('-')[1]+'月'+date.split('-')[2]+'日'}}</text>
							<image src="/static/icon/icon-arrow-02.png" mode="aspectFit"></image>
						</view>
					</picker>
				</view>
				<view class="item">
					<view class="title">加班时长</view>
					<view class="time-input">
						<picker class="input" mode="selector" :range="timeList" @change="bindTimeChange" :value="timeIndex">
							<view class="name">
								<text>{{timeList[timeIndex]}}</text>
								<image src="/static/icon/icon-arrow-02.png" mode="aspectFit"></image>
							</view>
						</picker>
						<view class="unit font24">小时</view>
					</view>
				</view>
				<view class="item">
					<view class="title">备注</view>
					<textarea class="input textarea" v-model="remarks"></textarea>
				</view>
				<!-- 审核按钮 -->
				<view class="enter-detail-btns color-white font30" style="margin: 0;">
					<view class="btn" @tap="editEv">确认</view>
					<view class="btn" @tap="delEv">删除</view>
				</view>
			</view>
		</view>
		<!-- 尾部 -->
		<tabbar :userType="userType" current="2"></tabbar>
	</view>
</template>
<script>
	import tabbar from '@/components/tabbar/tabbar';
	function getDate(type) {
	    const date = new Date();
	    let year = date.getFullYear();
	    let month = date.getMonth() + 1;
	    let day = date.getDate();
	    if (type === 'start') {
	        year = year - 10;
	    } else if (type === 'end') {
	        year = year + 10;
	    }
	    month = month > 9 ? month : '0' + month;
	    day = day > 9 ? day : '0' + day;
	    return `${year}-${month}-${day}`;
	}
	export default {
		components:{
			tabbar
		},
		data() {
			return {
				statusHeight:uni.getSystemInfoSync().statusBarHeight, //状态栏高度
				userType:'worker', //账户类型 工人：worker 负责人：director
				worksiteList:[], //工地列表
				worksiteIndex:0, //当前选择
				timeList:[], //加班时长列表
				timeIndex:0, //当前选择
				remarks:'', //备注
				date: getDate({
					format: true
				}),
				startDate: getDate('start'),
				endDate: getDate('end'),
				flag:true, //是否允许提交
				isLoding:false, //是否加载完成
				id:0, //加班id
				overtimeDetail:{}, //加班详情
			}
		},
		onLoad(op) {
			if(op.id){
				this.id = op.id;
			}
			// 获取工地列表
			this.getWorksiteList();
			// 获取加班时间
			this.getTimeList();
		},
		methods: {
			// 获取工地列表
			getOvertimeDetail(){
				this.$requst.get('/api/v1/common/overtime-info',{id:this.id}).then(res=>{
					if(res.code==0){
						console.log(res,'加班详情');
						this.remarks = res.data.remarks;
						this.worksiteIndex = this.worksiteList.findIndex(item=> item.id === parseFloat(res.data.worksite_id));
						this.timeIndex =  this.timeList.findIndex(item=> item === parseFloat(res.data.time));
						this.date = res.data.day_text;
					}
				})
			},
			
			// 获取工地列表
			getWorksiteList(){
				this.$requst.get('/api/v1/common/worksite-list').then(res=>{
					if(res.code==0){
						this.worksiteList = res.data.list;
						this.isLoding = true;
					}
				})
			},
			
			// 获取加班时间
			getTimeList(){
				let timeArr = [];
				for(let i=1;i<=48;i++){
					timeArr.push(i-0.5,i)
				}
				this.timeList = timeArr;
				// 获取加班详情
				this.getOvertimeDetail();
			},
			
			// 编辑
			editEv(){
				if(this.flag){
					this.flag = false;
					let params = {
						day:this.date,
						time:this.timeList[this.timeIndex],
						worksite_id:this.worksiteList[this.worksiteIndex].id,
						remarks:this.remarks,
						id:this.id
					}
					this.$requst.post('/api/v1/worker/overtime-edit',params).then(res=>{
						if(res.code==0){
							this.$toolAll.tools.showToast('编辑成功');
							setTimeout(()=>{
								uni.navigateBack({
									delta: 1,
								})
							},1000)
						}else{
							this.$toolAll.tools.showToast(res.msg);
						}
						setTimeout(()=>{
							this.flag = true;
						},2000)
					})
				}
			},
			
			// 删除
			delEv(){
				this.$requst.post('/api/v1/worker/overtime-del',{id:this.id}).then(res=>{
					if(res.code==0){
						this.$toolAll.tools.showToast('删除成功');
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1,
							})
						},1000)
					}else{
						this.$toolAll.tools.showToast(res.msg);
					}
				})
			},
			
			// 选择工地
			bindWorksiteChange(e) {
				this.worksiteIndex = e.detail.value;
			},
			
			// 选择日期
			bindDateChange(e) {
				this.date = e.detail.value;
			},
			
			// 选择加班时长
			bindTimeChange(e) {
				this.timeIndex = e.detail.value;
			}
		}
	}
</script>
<style scoped>
	
</style>