<template>
	<view class="form-page">
		<scroll-view scroll-y class="form-scroll">
		<uni-forms v-model="form" label-width="180upx">
			<view class="form-card" v-if="form.ticket_">
				<ticketd :ticket_="form.ticket_"></ticketd>
			</view>
			<view class="form-card">
				<view class="form-card-title">会议室预约</view>
			<uni-forms-item label="会议名称" required>
				<uni-easyinput v-model="form.title" placeholder="请输入会议名称" :disabled="mode=='show'"/>
			</uni-forms-item>
			<uni-forms-item label="会议室" required>
				<uni-data-select
				  v-model="form.mroom"
				  :localdata="mroomList"
				  :disabled="mode=='show'"
				  @change="mdateChange" 
				></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="预约日期" required>
				<uni-datetime-picker type="date" :clear-icon="false" v-model="form.mdate" @change="mdateChange" 
				v-if="mode!='show'"/>
				<span v-else>{{form.mdate}}</span>
			</uni-forms-item>
			<uni-forms-item label="参加人数" required>
				<uni-number-box v-model="form.participant_count" :disabled="mode=='show'"></uni-number-box>
			</uni-forms-item>
			<uni-forms-item label="参会领导">
				<uni-easyinput v-model="form.key_participants" :disabled="mode=='show'"/>
			</uni-forms-item>
			<uni-forms-item label="备注">
				<uni-easyinput v-model="form.note" :disabled="mode=='show'"/>
			</uni-forms-item>
			<uni-forms-item label="时间段">
				<span v-for="item in form.time_ranges" :key="item">{{item}}</span>
			</uni-forms-item>
			</view>
		</uni-forms>
		<view class="timeView" v-show="mode != 'show'">
			<view style="height: 70upx;line-height: 70upx;">
				<text>凌晨：</text>
				<view class="iconsWrap" @click="showAmChange"> 
					<uni-icons v-if="showAm" type="up" size="20" color="#ffffff"></uni-icons>
					<uni-icons v-else type="down" size="20" color="#ffffff"></uni-icons>
				</view>
			</view>
			<view :class="{'amBlockHide':!showAm,'amBlock': showAm}">
				<view v-for="(item,index) in timesListAm" :key="item.value" class="timeBlock ">
					<view :class="{'isSelected':item.isSelect,'isSloted':item.sloted,'timeItem': true}" @click="timeItemClickAm(index)">{{item.label}}</view>
				</view>
			</view>
			<view v-for="(item,index) in timesListPm" :key="item.value" class="timeBlock">
				<view :class="{'isSelected':item.isSelect,'isSloted':item.sloted,'timeItem': true}" @click="timeItemClickPm(index)">{{item.label}}</view>
			</view>
		</view>
		</scroll-view>
		<view class="footer_fixed">
			<ticketd_b :workflow_key="'wf_mroombooking'" :title="form.title + '-会议室预定'" :t_id="form.id" :ticket_="form.ticket_"
				@success="submitSuccess" :submit_b_func="submit_b_func" ref="ticketd_b_start"></ticketd_b>
		</view>
	</view>
</template>

<script>
import ticketd_b from "../wf/ticketd_b.vue"
import ticketd from "../wf/ticketd.vue"
import {actStateEnum} from "@/utils/enum.js"
	export default {
		components: { ticketd_b, ticketd },
		data(){
			return{
				t_id: null,
				actStateEnum,
				index:0,
				totalCount:0,
				pickDate:'',
				mroomId:'',
				mode:'show',
				showAm:false,
				bookingTitle:'',
				form:{
					id:'',	
					note:'',
					title:'',
					mroom:'',
					mdate:'',
					slots:[],
					create_by_phone:'',
					key_participants:'',
					participant_count:'',
					ticket_:null,
					belong_dept:uni.getStorageSync('userInfo').belong_dept
				},
				selectList:[],
				mroomList: [],
				transitions:[],
				//凌晨0-6点
				timesListAm:[
					{value:0,label:'00:00-00:30',isSelect:false,sloted:false},
					{value:1,label:'00:30-01:00',isSelect:false,sloted:false},
					{value:2,label:'01:00-01:30',isSelect:false,sloted:false},
					{value:3,label:'01:30-02:00',isSelect:false,sloted:false},
					{value:4,label:'02:00-02:30',isSelect:false,sloted:false},
					{value:5,label:'02:30-03:00',isSelect:false,sloted:false},
					{value:6,label:'03:00-03:30',isSelect:false,sloted:false},
					{value:7,label:'03:30-04:00',isSelect:false,sloted:false},
					{value:8,label:'04:00-04:30',isSelect:false,sloted:false},
					{value:9,label:'04:30-05:00',isSelect:false,sloted:false},
					{value:10,label:'05:00-05:30',isSelect:false,sloted:false},
					{value:11,label:'05:30-06:00',isSelect:false,sloted:false}
				],
				timesListPm:[
					{value:12,label:'06:00-06:30',isSelect:false,sloted:false},
					{value:13,label:'06:30-07:00',isSelect:false,sloted:false},
					{value:14,label:'07:00-07:30',isSelect:false,sloted:false},
					{value:15,label:'07:30-08:00',isSelect:false,sloted:false},
					{value:16,label:'08:00-08:30',isSelect:false,sloted:false},
					{value:17,label:'08:30-09:00',isSelect:false,sloted:false},
					{value:18,label:'09:00-09:30',isSelect:false,sloted:false},
					{value:19,label:'09:30-10:00',isSelect:false,sloted:false},
					{value:20,label:'10:00-10:30',isSelect:false,sloted:false},
					{value:21,label:'10:30-11:00',isSelect:false,sloted:false},
					{value:22,label:'11:00-11:30',isSelect:false,sloted:false},
					{value:23,label:'11:30-12:00',isSelect:false,sloted:false},
					{value:24,label:'12:00-12:30',isSelect:false,sloted:false},
					{value:25,label:'12:30-13:00',isSelect:false,sloted:false},
					{value:26,label:'13:00-13:30',isSelect:false,sloted:false},
					{value:27,label:'13:30-14:00',isSelect:false,sloted:false},
					{value:28,label:'14:00-14:30',isSelect:false,sloted:false},
					{value:29,label:'14:30-15:00',isSelect:false,sloted:false},
					{value:30,label:'15:00-15:30',isSelect:false,sloted:false},
					{value:31,label:'15:30-16:00',isSelect:false,sloted:false},
					{value:32,label:'16:00-16:30',isSelect:false,sloted:false},
					{value:33,label:'16:30-17:00',isSelect:false,sloted:false},
					{value:34,label:'17:00-17:30',isSelect:false,sloted:false},
					{value:35,label:'17:30-18:00',isSelect:false,sloted:false},
					{value:36,label:'18:00-18:30',isSelect:false,sloted:false},
					{value:37,label:'18:30-19:00',isSelect:false,sloted:false},
					{value:38,label:'19:00-19:30',isSelect:false,sloted:false},
					{value:39,label:'19:30-20:00',isSelect:false,sloted:false},
					{value:40,label:'20:00-20:30',isSelect:false,sloted:false},
					{value:41,label:'20:30-21:00',isSelect:false,sloted:false},
					{value:42,label:'21:00-21:30',isSelect:false,sloted:false},
					{value:43,label:'21:30-22:00',isSelect:false,sloted:false},
					{value:44,label:'22:00-22:30',isSelect:false,sloted:false},
					{value:45,label:'22:30-23:00',isSelect:false,sloted:false},
					{value:46,label:'23:00-23:30',isSelect:false,sloted:false},
					{value:47,label:'23:30-24:00',isSelect:false,sloted:false}
				]
			}
		},
		async onLoad(options) {
			let that = this;
			that.form.id='';
			that.selectList = [];
			that.mroomId = options.mroomId;
			that.mode = options.mode?options.mode:'show';
			that.t_id = options.t_id?options.t_id:null;
			if(that.mode != "add"){
				if(that.t_id) {
					that.form = await that.$api.bookingItem(that.t_id);
					if(that.form.ticket_.state_.type == 1 && that.form.create_by == uni.getStorageSync("userInfo").id ) {
						that.mode = "edit";
					}else{
						that.mode = "show";
					}
					that.mdateChange()
				}else{
					that.form = uni.getStorageSync("currentBooking");
				}
			}
			await that.getmRooms();
		},
		methods:{
			async getmRooms(){
				let that = this;
				let res = await that.$api.getMroom({ page: 0});
				res.forEach(item=>{
					item.value = item.id;
					item.text = item.name
				})
				that.mroomList = res;
			},
			showAmChange(){
				this.showAm = !this.showAm;
			},
			timeItemClickAm(index){
				if(this.timesListPm[index].sloted){
					
				}else{
					this.timesListAm[index].isSelect = !this.timesListAm[index].isSelect;
					this.clickItems();
				}
			},
			timeItemClickPm(index){
				if(this.timesListPm[index].sloted){
					
				}else{
					this.timesListPm[index].isSelect = !this.timesListPm[index].isSelect;
					this.clickItems();
				}
			},
			clickItems(){
				let that = this;
				that.selectList = [];
				let lists = [],slots=[];
				that.timesListAm.forEach(item=>{
					if(item.isSelect){
						lists.push(item)
						slots.push(item.value);
					}
				})
				that.timesListPm.forEach(item2=>{
					if(item2.isSelect){
						lists.push(item2)
						slots.push(item2.value);
					}
				})
				setTimeout(()=>{
					that.totalCount = slots.length*0.5;
					that.selectList = lists;
					that.form.slots = slots;
				},500)
			},
			//选择会议室和日期后查询有无预定
			bindPickerChange(e) {
				this.form.mroom = e.detail.value;
				this.index = e.detail.value;
				if(this.form.mdate!==''&&this.form.mdate!==null){
					this.getmRoomSlot();
				}
			},
			mdateChange(){
				if(this.form.mroom&&this.form.mdate){
					this.getmRoomSlot();
				}
			},
			getmRoomSlot(){
				let that = this;
				let form = {};
				form.page = 0;
				form.mroom = that.form.mroom;
				form.mdate = that.form.mdate;
				form.is_inuse = true;
				that.timesListAm.forEach(slot=>{
					slot.sloted = false;
				})
				that.timesListPm.forEach(slot=>{
					slot.sloted = false;
				})
				that.$api.bookingSlot(form).then(res=>{
					res.forEach(item=>{
						if(item.slot<12){
							if(item.booking == that.form.id) {
								that.timesListAm[item.slot].isSelect = true;
							}else{
								that.timesListAm[item.slot].sloted = true;
							}
						}else{
							if(item.booking == that.form.id) {
								that.timesListPm[item.slot-12].isSelect = true;
							}else{
								that.timesListPm[item.slot-12].sloted = true;
							}
						}
					})
				})
			},
			//选择会议室和日期后查询有无预定
			async submit_b_func(id){
				let that = this;
				if (that.mode != 'show') {
					if(that.form.id) {
						await that.$api.bookingUpdate(that.form.id, that.form);
					}else{
						let res = await that.$api.bookingCreate(that.form);
						that.form.id = res.id;
					}
				}
			},
			submitSuccess(){
				uni.navigateTo({
					url: "/pages/index/index"
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
.form-page { min-height: 100vh; background: #F0F2F5; }
.form-scroll { padding: 20rpx 24rpx; padding-bottom: 180rpx; }
.form-card { background: #fff; border-radius: 20rpx; padding: 24rpx; margin-bottom: 20rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.04); }
.form-card-title { font-size: 30rpx; font-weight: 600; color: #1F2937; margin-bottom: 16rpx; padding-left: 16rpx; border-left: 6rpx solid #2BA471; }
.form-textarea { width: 100%; min-height: 160rpx; border: 2rpx solid #E5E7EB; border-radius: 12rpx; padding: 16rpx; font-size: 28rpx; color: #374151; box-sizing: border-box; background: #F9FAFB; }
.form-btn { flex: 1; height: 80rpx; line-height: 80rpx; border-radius: 14rpx !important; font-size: 28rpx; font-weight: 500; border: none !important; }
.form-btn-primary { background: linear-gradient(135deg, #2BA471, #1F8C5E) !important; color: #fff !important; box-shadow: 0 4rpx 12rpx rgba(43,164,113,0.2); }
.form-btn-danger { background: linear-gradient(135deg, #EF4444, #DC2626) !important; color: #fff !important; box-shadow: 0 4rpx 12rpx rgba(239,68,68,0.3); }
	.iconsWrap{
		width: 40upx;
		height: 40upx;
		display: inline-block;
		line-height: 40upx;
		border-radius: 25upx;
		text-align: center;
		background-color: #74ddfc;
		border: 1px solid #74ddfc;
	}
	.timeView{
		width: 100%;
		padding: 10upx;
		box-sizing: border-box;
	}
	.amBlock{
		display: block;
	}
	.amBlockHide{
		display: none;
	}
	.timeBlock{
		width: 25%;
		padding: 6upx;
		height: 70upx;
		margin: 10upx 0;
		font-size: 28upx;
		line-height: 70upx;
		display: inline-block;
		box-sizing: border-box;
	}
	.timeItem{
		color: #666666;
		text-align: center;
		border-radius: 10upx;
		background-color: #b9f0cb;
		border: 1upx solid #eeeeee;
	}
	.timeItem.isSelected{
		background-color: #00a870;
		color: #fff;
	}
	.timeItem.isSloted{
		background-color: #fac275!important;
		  color: #fff;
	}
</style>