<template>
	<view class="pad-x120">
		<!-- 头部 -->
		<status-nav :ifReturn="false" 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="submit-btn color-white font30" @tap="submitOvertime">提交</view>
			</view>
		</view>
		<!-- 尾部 -->
		<tabbar :userType="userType" current="1"></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, //是否加载完成
			}
		},
		onLoad() {
			// 获取工地列表
			this.getWorksiteList();
			// 获取加班时间
			this.getTimeList();
		},
		onShareAppMessage(res) {
			let shareObj = {
				title:'工地打卡',
				path: '/pages/pagehome/pagehome',
				imageUrl:'/static/share-logo.jpg',
			}
			// 返回shareObj
			return shareObj;
		},
		methods: {
			// 获取工地列表
			getWorksiteList(){
				this.$requst.get('/api/v1/common/worksite-list').then(res=>{
					if(res.code==0){
						this.worksiteList = res.data.list;
						if(uni.getStorageSync('worksite_id')){
							this.worksiteIndex = res.data.list.findIndex(item=> item.id === parseFloat(uni.getStorageSync('worksite_id')));
						}
						this.isLoding = true;
					}
				})
			},
			
			// 获取加班时间
			getTimeList(){
				let timeArr = [];
				for(let i=1;i<=48;i++){
					timeArr.push(i-0.5,i)
				}
				this.timeList = timeArr;
			},
			
			// 提交加班
			submitOvertime(){
				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
					}
					this.$requst.post('/api/v1/worker/overtime',params).then(res=>{
						if(res.code==0){
							this.$toolAll.tools.showToast('提交成功');
							setTimeout(()=>{
								uni.reLaunch({
									url:'/pages/worker/sign/sign'
								})
							},1000)
						}else{
							this.$toolAll.tools.showToast(res.msg);
						}
						setTimeout(()=>{
							this.flag = true;
						},2000)
					})
				}
			},
			
			// 选择工地
			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>