<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog
			:title="title"
			:close-on-click-modal="false"
			draggable
			width="1300px"
			@close="close"
			@keyup.enter.native=""
			v-model="visible">
			<el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="status === 'audit' || status === 'taskFormDetail' || method === 'view'"
					 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="createName"
									  :rules="[
								]">
							<el-input v-model="inputForm.createName" placeholder="请填写请假人" readonly="true" disabled="true"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="请假部门" prop="officeName"
									  :rules="[
								  ]">
							<el-input v-model="inputForm.officeName" placeholder="请填写请假部门" readonly="true" disabled="true"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item prop="type" label="请假类型"
									  :rules="[ {required: true, message:'请假类型不能为空', trigger:'blur'}]">
							<el-select v-model="inputForm.type" placeholder="请选择" style="width:100%;">
								<el-option
									v-for="item in $dictUtils.getDictList('holiday_type')"
									: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="startDay"
									  :rules="[
								  {required: true, message:'开始时间不能为空', trigger:'blur'}]">
							<el-date-picker
								style="width: 50%"
								v-model="inputForm.startDay"
								type="date"
								format="YYYY-MM-DD"
								value-format="YYYY-MM-DD"
								placeholder="选择日期"
								@change="checkTime"
							/>
							<el-select v-model="inputForm.beginTime" style="width: 50%" placeholder="请选择开始时间" @change="checkTime">
								<el-option value="上午" label="上午"></el-option>
								<el-option value="下午" label="下午"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="结束时间" prop="endDay"
									  :rules="[
								  {required: true, message:'结束时间不能为空', trigger:'blur'}]">
							<el-date-picker
								style="width:50%;"
								v-model="inputForm.endDay"
								type="date"
								format="YYYY-MM-DD"
								value-format="YYYY-MM-DD"
								placeholder="请选择日期"
								:size="default"
								@change="checkTime"
							/>
							<el-select v-model="inputForm.endTime" style="width: 50%" placeholder="请选择开始时间" @change="checkTime">
								<el-option value="上午" label="上午"></el-option>
								<el-option value="下午" label="下午"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="请假天数" prop="days"
									  :rules="[
								  {required: true, message:'结束时间不能为空', trigger:'blur'}]">
							<el-input v-model="inputForm.days" placeholder="请填写请假天数" readonly="true" disabled="true"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="请假原因" prop="reason"
									  :rules="[]">
							<el-input :disabled="status === 'audit' || status === 'taskFormDetail' || method === 'view'" type="textarea" placeholder="请输入请假原因"  maxlength="500"  style="width:100%" v-model="inputForm.reason" show-word-limit></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<UpLoadComponent ref="uploadComponent"></UpLoadComponent>
			<template #footer>
			<span class="dialog-footer">
			  <el-button  @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
			</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
	import XEUtils from 'xe-utils'
	import HolidayService from '@/api/dailyOfficeWork/HolidayService'
	import { ElDatePicker } from 'element-plus';
	import UserSelectSignatory from '@/views/cw/workClientInfo/clientUserSelect'
	import UpLoadComponent from '@/views/common/UpLoadComponent'
	import CommonApi from '@/api/cw/common/CommonApi'
	import SelectTree from '@/components/treeSelect/treeSelect.vue'
	export default {
		props: {
			businessId: {
				type: String,
				default: ''
			},
			formReadOnly: {
				type: Boolean,
				default: false
			},
			status: {
				type: String,
				default: ''
			}
		},
		data () {
			return {
				title: '',
				method: '',
				loading: false,
				visible: false,
				inputForm: {
					createName:this.$store.state.user.name,
					officeName:this.$store.state.user.office.name,
					id: '',
					startDay:"",
					beginTime:'',
					reason: '',
					endDay:"",
					endTime:'',
					type:'',
					files: [], // 附件信息
					procInsId: '',
					days:'',
				},
				baseKey: '',
				keyWatch: '',
				dateList:[],
				programRow:'',
				days:'',

			}
		},
		HolidayService:null,
		commonApi: null,
		created () {
			this.holidayService=new HolidayService()
			this.commonApi = new CommonApi()
		},
		mounted () {
		},
		activated () {
		},
		components: {
			ElDatePicker,
			UserSelectSignatory,
			UpLoadComponent,
			SelectTree,
		},
		computed: {
			bus: {
				get () {
					this.$refs.uploadComponent.setDividerName('附件')
					return this.businessId
				},
				set (val) {
					this.businessId = val
				}
			}
		},
		watch: {
			'keyWatch': {
				handler (newVal) {
					console.log('几里路')
					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)
					this.$refs.uploadComponent.changeLoading(newVal)
				}
			}
		},
		methods: {
			getKeyWatch(keyWatch) {
				this.keyWatch = keyWatch
			},
			init(method, id) {
				this.method = method
				this.inputForm={
					createName:'',
					officeName:'',
					id: '',
					startDay:"",
					beginTime:'',
					reason: '',
					endDay:"",
					endTime:'',
					type:'',
					files: [], // 附件信息
					procInsId: '',
					days:'',
				}
				this.inputForm.id = id
				this.loading = false
				this.visible = true
				if (method === 'edit'){
					this.title = '修改请假信息'
				}else if (method === 'view'){
					this.title = '查看请假信息'
				}
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					this.loading = true
					this.$refs.uploadComponent.clearUpload()
					if (this.commonJS.isNotEmpty(this.inputForm.id)) {
						console.log('this.inputForm.id', this.inputForm.id)
						this.holidayService.queryById(this.inputForm.id).then((data)=>{
							this.inputForm = this.recover(this.inputForm, data)
							this.days=data.days
							this.inputForm.files=data.files
							console.log('s',this.inputForm)
							if (this.commonJS.isEmpty(this.inputForm.createName)){
								this.inputForm.createName = this.$store.state.user.name
							}
							if (this.commonJS.isEmpty(this.inputForm.officeName)){
								this.inputForm.officeName = this.$store.state.user.office.name
							}
							if (this.status === 'audit' || this.status === 'taskFormDetail') {
								method = 'view'
							}
							console.log('me',method)
							this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'jyHoliday',null,null,null,null,null)
							this.loading = false
						})

					}
				})
			},
			saveForm(callback) {
				this.doSubmit('save', callback)
			},
			startForm(callback) {
				this.loading = true
				if (this.commonJS.isNotEmpty(this.inputForm.id)) {
					this.holidayService.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.holidayService.queryById(this.inputForm.id).then((data) => {
					if (data.status !== '2' ) { // status的值不等于“审核中”，就弹出提示
						this.loading = false
						this.$message.error('任务数据已发生改变或不存在，请在待办任务中确认此任务是否存在')
						throw new Error()
					} else {
						this.inputForm.status= '2'
						if (this.inputForm.days>1){
							this.commonApi.getTaskNameByProcInsId(this.inputForm.procInsId).then((da)=>{
								if (this.commonJS.isNotEmpty(da)) {
									if (da === '总经理审核') {
										this.inputForm.status = '5'
									}
								}
								let param = {status: this.inputForm.status, id: this.inputForm.id}
								this.inputForm=JSON.stringify(this.inputForm)
								this.holidayService.updateStatusById(param).then(() => {
									callback(data.businessTable, data.businessId, this.inputForm,data.days)
									this.loading = false
								}).catch(() => {
									this.loading = false
								})
							})
						}else {
							this.inputForm.status= '5'
							let param = {status: this.inputForm.status, id: this.inputForm.id}
							this.inputForm=JSON.stringify(this.inputForm)
							this.holidayService.updateStatusById(param).then(() => {
								callback(data.businessTable, data.businessId, this.inputForm,data.days)
								this.loading = false
							}).catch(() => {
								this.loading = false
							})
						}

					}
				})
			},
			reapplyForm(callback) {
				this.loading = true
				this.holidayService.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.$refs.uploadComponent.checkProgress()) {
						this.loading = false
						return
					}
					this.inputForm.files = this.$refs.uploadComponent.getDataList()
					this.holidayService.saveForm(this.inputForm).then((data) => {
						callback(data.businessTable, data.businessId, this.inputForm,data.days)
						this.loading = false
					}).catch(() => {
						this.loading = false
					})
					return
				} else if (status === 'start') {

					if (this.commonJS.isEmpty(this.inputForm.startDay) || this.commonJS.isEmpty(this.inputForm.beginTime)){
						this.$message.error('请选择开始时间')
						this.loading = false
					}
					if (this.commonJS.isEmpty(this.inputForm.endDay) || this.commonJS.isEmpty(this.inputForm.endTime)){
						this.$message.error('请选择结束时间')
						this.loading = false
					}
					if (this.commonJS.isEmpty(this.inputForm.type)){
						this.$message.error('请选择请假类型')
						this.loading = false
					}
					if (this.$refs.uploadComponent.checkProgress()) {
						this.loading = false
						return
					}
					this.inputForm.files = this.$refs.uploadComponent.getDataList()
					// 送审  待审核
					this.inputForm.status = '2'
				} else if (status === 'agree') {
					this.inputForm.status= '5'
				} else if (status === 'reapply') {
					this.inputForm.status = '2'
				}
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						console.log('form',this.inputForm)
						this.inputForm=JSON.parse(JSON.stringify(this.inputForm))
						this.holidayService.saveForm(this.inputForm).then((data) => {
							this.visible = false
							this.$emit('refreshList')
						}).catch(() => {
							this.$refs.inputForm.resetFields()
							this.loading = false
						})
					} else {
						this.loading = false
					}
				})
			},
			close() {
				this.inputForm = {
					id: '',
					startDay:"",
					beginTime:'',
					reason: '',
					endDay:"",
					endTime:'',
					type:'',
					files: [], // 附件信息
					procInsId: '',
					days:'',
				}
				this.visible = false
			},
			async updateStatusById(type, callback) {
				this.loading = true
				if (type === 'reject' || type === 'reback') {
					this.holidayService.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.holidayService.updateStatusById(param).then(() => {
									this.loading = false
									callback()
								})
							}
						}
					})
				} else if (type === 'hold') {
					this.holidayService.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.holidayService.updateStatusById(param).then(() => {
								this.loading = false
								callback()
							})
						}
					})
				}

			},
			checkType(){
				if (this.inputForm.type === '1'){
					this.holidayService.checkType(this.inputForm).then((data)=>{
						console.log('data',data)
						this.$message.error(data)
						throw new Error()

					})
				}
			},
			checkTime(){
				let differenceInDays =''
				let date1=new Date(this.inputForm.startDay)
				let date2=new Date(this.inputForm.endDay)
				if ((date2<date1) || (date2 === date1 && this.inputForm.beginTime === '下午' && this.inputForm.endTime === '上午')){
					this.$message.error('开始时间不能小于结束时间')
					throw new Error()
				}
				differenceInDays= Math.floor((date2 - date1) / (1000 * 60 * 60 * 24));
				if ((this.inputForm.beginTime === '上午' && this.inputForm.endTime === '下午') ||(this.inputForm.beginTime === '下午' && this.inputForm.endTime === '上午') ){
					differenceInDays=differenceInDays+1
				}else if ((this.inputForm.beginTime === '上午' && this.inputForm.endTime === '上午') ||(this.inputForm.beginTime === '下午' && this.inputForm.endTime === '下午')){
					differenceInDays=differenceInDays+0.5
				}


				console.log('differenceInDays',differenceInDays)
				this.inputForm.days = differenceInDays
			}

		}

	}
</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>
