<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="status === 'audit' || status === 'taskFormDetail'"
				 label-width="160px" @submit.native.prevent>
			<el-divider content-position="left"><i class="el-icon-document"></i>
				会议室预约
			</el-divider>
			<el-row  :gutter="15">
				<el-col :span="12">
					<el-form-item label="标题" prop="title"
								  :rules="[ {required: true, message:'标题不能为空', trigger:'blur'}]">
						<el-input v-model="inputForm.title" class="m-2" placeholder="请输入会议标题" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="会议室编号" prop="meetNumber"
								  :rules="[
							 {required: true, message:'会议室编号不能为空', trigger:'change'}
							]">
						<el-select style="width:100%;" v-model="inputForm.meetNumber" @change="checkTime" class="m-2" placeholder="请选择" clearable>
							<el-option
								v-for="item in $dictUtils.getDictList('meet_number')"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="会议开始时间" prop="startDate"
								  :rules="[
								  {required: true, message:'会议开始日期不能为空', trigger:'blur'}]">
						<el-date-picker
							style="width:50%;"
							v-model="inputForm.startDate"
							type="date"
							value-format="YYYY-MM-DD"
							placeholder="请选择日期"
							:disabled-date="disableDate"
							:clearable="true"
							@change="checkTime"
						/>
						<el-select v-model="inputForm.startTime" placeholder="请选择时间" style="width:50%;" clearable>
							<el-option
								v-for="item in dateList"
								:disabled="item.disabled"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="会议时长" prop="duration"
								  :rules="[
							 {required: true, message:'会议时长不能为空', trigger:'change'}
							]">
						<el-select style="width:100%;" v-model="inputForm.duration" class="m-2" placeholder="请选择" clearable>
							<el-option
								v-for="item in $dictUtils.getDictList('meet_time_long')"
								:key="item.value"
								:label="item.label"
								:value="item.value">
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="会议发起人" prop="createByName"
								  :rules="[]">
						<el-input :disabled="true" v-model="inputForm.createByName"></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="会议说明" prop="remarks"
								  :rules="[ ]">
						<el-input :disabled="status === 'audit' || status === 'taskFormDetail' || method === 'view'" type="textarea" placeholder="请输入说明"  maxlength="500"  style="width:100%" v-model="inputForm.remarks" show-word-limit></el-input>
					</el-form-item>
				</el-col>

			</el-row>
		</el-form>
	</div>
</template>

<script>
	import XEUtils from 'xe-utils'
	import MeetingRoom from '@/api/dailyOfficeWork/MeetingRoomService'
	import { ElDatePicker } from 'element-plus';
	export default {
		props: {
			businessId: {
				type: String,
				default: ''
			},
			formReadOnly: {
				type: Boolean,
				default: false
			},
			status: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				title: '',
				method: '',
				loading: false,
				inputForm: {
					id: '',
					title: '',
					startTime: '',
					startDate: '',
					duration: '',
					createById: this.$store.state.user.id,
					createByName: this.$store.state.user.name,
					createDate: '',
					remarks: '',
					meetNumber:''
				},
				baseKey: '',
				keyWatch: '',
				dateList:[]
			}
		},
		MeetingRoom:null,
		created () {
			console.log('jld ')
			this.meetingRoomServie=new MeetingRoom()
		},
		mounted () {
		},
		activated () {
		},
		components: {
			ElDatePicker
		},
		computed: {
			bus: {
				get () {
					return this.businessId
				},
				set (val) {
					this.businessId = val
				}
			}
		},
		watch: {
			'keyWatch': {
				handler (newVal) {
					if (this.bus) {
						if (this.bus !== 'false') {
							this.init('', this.bus)
						}
					} else {
						this.$nextTick(() => {
							this.$refs.inputForm.resetFields()
						})
					}
				}
			},
			'loading': {
				handler (newVal) {
					this.$emit('changeLoading', newVal)
				}
			}
		},
		methods: {
			getKeyWatch(keyWatch) {
				this.keyWatch = keyWatch
			},
			init(method, id) {
				this.method = method
				this.inputForm={
					id:'',
					title:'',
					startTime:'',
					startDate:'',
					duration:'',
					createById:'',
					createByName:'',
					createDate:'',
					remarks:'',
					meetNumber:''
				}
				this.inputForm.id = id
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					this.loading = true
					if (this.commonJS.isNotEmpty(this.inputForm.id)) {
						console.log('this.inputForm.id', this.inputForm.id)
						this.meetingRoomServie.queryById(this.inputForm.id).then((data)=>{
							console.log('s',data)
							this.inputForm = this.recover(this.inputForm, data)
							this.inputForm.createById=data.createById
							this.inputForm.createByName=data.createName
							this.inputForm.duration=String(data.duration)
							if (this.commonJS.isEmpty(data.duration)){
								this.inputForm.duration=''
							}
							if(this.commonJS.isNotEmpty(this.inputForm.startDate)){
								this.checkTime()
							}
							if (this.commonJS.isEmpty(this.inputForm.createDate)) {
								this.inputForm.createDate = this.moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
							}
							if(this.commonJS.isNotEmpty(data.startTime)){
								this.inputForm.startTime = data.startTime
							}
						})
						this.loading = false
					}
				})
			},
			saveForm(callback) {
				this.doSubmit('save', callback)
			},
			startForm(callback) {
				this.loading = true
				if (this.commonJS.isNotEmpty(this.inputForm.id)) {
					this.meetingRoomServie.queryById(this.inputForm.id).then((data) => {
						// if (data.status !== '0' && data.status !== '1' && data.status !== '3') { // 审核状态不是“未发起”或“暂存”或“撤回”，就弹出提示
						// 	this.loading = false
						// 	this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						// 	throw new Error()
						// } else {
							this.doSubmit('start', callback)
						// }
					})
				} else {
					this.doSubmit('start', callback)
				}
			},
			async agreeForm(callback) {
				console.log('进入方法')
				this.loading = true
				await this.meetingRoomServie.queryById(this.inputForm.id).then((data) => {
					// if (data.status !== '2' ) { // status的值不等于“审核中”，就弹出提示
					// 	this.loading = false
					// 	this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					// 	throw new Error()
					// } else {
							this.inputForm.status= '5'
						let param = {status: this.inputForm.status, id: this.inputForm.id}
						this.inputForm=JSON.stringify(this.inputForm)
						this.meetingRoomServie.updateStatusById(param).then(() => {
							callback(data.businessTable, data.businessId, this.inputForm)
							this.loading = false
						}).catch(() => {
							this.loading = false
						})
					// }
				})
			},
			reapplyForm(callback) {
				this.loading = true
				this.meetingRoomServie.queryById(this.inputForm.id).then((data) => {
					// if (data.status !== '4') { // 审核状态不是“驳回”，就弹出提示
					// 	this.loading = false
					// 	this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					// 	throw new Error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
					// } else {
						this.doSubmit('reapply', callback)
					// }
				})
			},
			// 表单提交
			async doSubmit(status, callback) {
				this.loading = true
				console.log('5',this.inputForm)
				if (status === 'save') {
					// 暂存
					this.loading = true
					this.inputForm.status = '1'
					if (this.commonJS.isEmpty(this.inputForm.startTime)){
						this.inputForm.startTime = null
					}
					if (this.commonJS.isEmpty(this.inputForm.startDate)){
						this.inputForm.startDate = null
					}
					this.meetingRoomServie.saveForm(this.inputForm).then((data) => {
						callback(data.businessTable, data.businessId, this.inputForm)
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
					return
				} else if (status === 'start') {
					if (this.commonJS.isEmpty(this.inputForm.startDate)|| this.commonJS.isEmpty(this.inputForm.startTime)) {
						this.loading = false
						this.$message.error('请选择会议开始时间')
						this.loading = false
					}
					if (this.commonJS.isEmpty(this.inputForm.duration)) {
						this.loading = false
						this.$message.error('请选择会议时长')
						this.loading = false
					}
					if (this.commonJS.isEmpty(this.inputForm.title)) {
						this.loading = false
						this.$message.error('请输入会议标题')
						this.loading = false
					}
					if (this.commonJS.isEmpty(this.inputForm.meetNumber)) {
						this.loading = false
						this.$message.error('请选择会议室编号')
						this.loading = false
					}
					if (this.commonJS.isNotEmpty(this.inputForm.startTime)){
						console.log('this.dateList',this.dateList)
						for (let i = 0; i <this.dateList.length ; i++) {
							if (this.dateList[i].disabled === true && this.dateList[i].value === this.inputForm.startTime){
								this.loading = false
								this.$message.error('会议室当前无空余,请选择其他时间')
								throw new Error()
							}
						}
					}
					// 送审  待审核
					this.inputForm.status = '2'
				} else if (status === 'agree') {
					this.inputForm.status= '5'
				} else if (status === 'reapply') {
					if (this.commonJS.isEmpty(this.inputForm.meetNumber)) {
						this.loading = false
						this.$message.error('请选择会议室编号')
						this.loading = false
					}
					if (this.commonJS.isNotEmpty(this.inputForm.startDate)){
						const now = this.moment(new Date()).format('YYYY-MM-DD'); // 获取当前时间
						console.log('now',now)
						if (new Date(now) > new Date(this.inputForm.startDate)){
							this.loading = false
							this.$message.error('请选择今天或之后的日期')
							throw new Error()
						}
					}
					if (this.commonJS.isNotEmpty(this.inputForm.startTime)){
						console.log('this.dateList',this.dateList)
						for (let i = 0; i <this.dateList.length ; i++) {
							if (this.dateList[i].disabled === true && this.dateList[i].value === this.inputForm.startTime){
								this.loading = false
								this.$message.error('会议室当前无空余,请选择其他时间')
								throw new Error()
							}
						}
					}
					this.inputForm.status = '2'
				}
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						console.log('form',this.inputForm)
						this.meetingRoomServie.saveForm(this.inputForm).then((data) => {
							let processTitle = `${this.$store.state.user.name} 在 ${this.moment(new Date()).format('YYYY-MM-DD HH:mm')} 发起了 [会议室预约流程]`
							this.inputForm.title = processTitle
							callback(data.businessTable, data.businessId, this.inputForm)
							this.$refs.inputForm.resetFields()
							this.loading = false
						}).catch(() => {
							this.$refs.inputForm.resetFields()
							this.loading = false
						})
					} else {
						this.loading = false
					}
				})
			},
			close() {
				this.inputForm = {
					id:'',
					projectList:[],
					name:'',
					no:'',
					projectLeader:'',
					projectCreateDate:'',
					createById:'',
					createByName:'',
					createDate:'',
					remarks:'',
					files: [], // 附件信息
					types:[], //用印类型
					number:'', //用印份数
					projectId:'',
				}
				this.visible = false
			},
			async updateStatusById(type, callback) {
				this.loading = true
					if (type === 'reject' || type === 'reback') {
						this.meetingRoomServie.queryById(this.inputForm.id).then((data) => {
							// if (data.status !== '2') { // status的值不等于“审核中”，就弹出提示
							// 	this.loading = false
							// 	this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							// 	throw new Error()
							// } else {
								// if (type === 'agree') {
								//   // 同意
								//   this.inputForm.status = '5'
								// }
								if (type === 'reject') {
									// 驳回
									this.inputForm.status = '4'
								}
								if (type === 'reback') {
									// 撤回
									this.inputForm.status = '3'
								}
								if (type === 'reject' || type === 'reback') {
									let param = {status: this.inputForm.status, id: this.inputForm.id}
									this.meetingRoomServie.updateStatusById(param).then(() => {
										this.loading = false
										callback()
									})
								}
							// }
						})
					} else if (type === 'hold') {
						this.meetingRoomServie.queryById(this.inputForm.id).then((data) => {
							// if (data.status !== '4') { // status的值不等于“驳回”就弹出提示
							// 	this.loading = false
							// 	this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
							// 	throw new Error()
							// } else {
								// 终止
								let param = {status: '1', id: this.inputForm.id}
								this.meetingRoomServie.updateStatusById(param).then(() => {
									this.loading = false
									callback()
								})
							// }
						})
					}

			},
			//禁用日期
			disableDate(time) {
				const now = new Date(); // 获取当前时间
				const fivePM = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 17, 0, 0); // 设置下午五点的时间
				// 判断当前时间是否在下午五点之前
				if (now.getTime() < fivePM.getTime()){
					//Date.now()是javascript中的内置函数，它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。
					return time.getTime() < Date.now() - 8.64e7;
				}else {
					return time.getTime() < Date.now()
				}


			},
			//禁用小时
			disabledHour(){
				const arrs = []
				arrs.push(0,1,2,3,4,5,6,7,8,12,13,18,19,20,21,22,23)
				return arrs;
			},
			//禁用分钟
			disabledMinute(time){
				const arrs = []
				if (time ===17){
					for (let i = 0; i <59 ; i++) {
						arrs.push(i+1)
					}
				}
				if (time === 11){
					for (let i=0;i<59;i++){
						arrs.push(i+31)
					}
				}
				return arrs;
			},
			async checkTime(){
				this.dateList=this.$dictUtils.getDictList('meeting_date')
				console.log('this.dateList',this.dateList)
				this.inputForm.startTime=''
				if (this.commonJS.isNotEmpty(this.inputForm.startDate) && this.commonJS.isNotEmpty(this.inputForm.meetNumber)){
					this.meetingRoomServie.checkTime(this.inputForm.startDate,this.inputForm.meetNumber).then((data)=>{
						console.log('data',data)
						if (this.commonJS.isNotEmpty(data)){
							var num = [];
							for (var key in data) {
								console.log(data[key])
								let date1=data[key].replace(/:/g, '')

								for (let i=0;i<this.dateList.length;i++){
									let date2=this.dateList[i].value.replace(/:/g, "")
									console.log('date2',date2)
									if (date1===date2){
										num.push(i)
									}
								}

							}
							console.log('num', num)
							for (let i=0;i<num.length;i++){
								console.log('num[i]', num[i])
								this.dateList[num[i]].disabled = true
							}

						}
					})

				}


			}

		}

	}
</script>
<style scoped>
	/deep/ .el-input-number .el-input__inner {
		text-align: left;
	}
	/deep/ .vxe-footer--row .vxe-footer--column:nth-child(1) .vxe-cell--item {
		font-weight:700;
	}
</style>
