<template>
	<div style="text-align: center;">
		<div style="width:600px;float: left; margin: 10px;">

			<el-input v-model="queryLeaveName" placeholder="请输入查询条件" style="width :300px;float: left;"></el-input>
			<el-button type="primary" @click="search">查询</el-button>
			<el-button type="primary" @click="handleAdd">新增</el-button>
		</div>




		<el-table :data="tableData" border style="width: 99%">
			<el-table-column prop="id" label="id" v-if="isShow">
			</el-table-column>
			<el-table-column prop="leaveName" label="请假申请名称" width="180">
			</el-table-column>
			<el-table-column prop="leaveBeginDate" label="请假开始日期" width="180">
			</el-table-column>
			<el-table-column prop="leaveEndDate" label="请假结束日期" width="180">
			</el-table-column>
			<el-table-column prop="leaveDays" label="请假天数" width="80">
			</el-table-column>
			<el-table-column prop="leaveReason" label="请假原因" width="80">
			</el-table-column>

			<el-table-column label="操作">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
					<el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
					<el-button size="mini" type="success" @click="handlePublish(scope.row.id)">发布</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
		 :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalRows">
		</el-pagination>







		<el-dialog title="请假申请" :visible.sync="dialogVisible" width="500px">
			<el-form :model="form" label-width="80px">
				<el-form-item label="流程名称" v-show="isShow">
					<el-input v-model="form.id"></el-input>
				</el-form-item>
				<el-form-item label="申请名称">
					<el-input v-model="form.leaveName"></el-input>
				</el-form-item>
				<el-form-item label="请假日期">
					<el-date-picker v-model="form.leaveBeginDate" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="">
					<el-date-picker v-model="form.leaveEndDate" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="请假天数">
					<el-input v-model="form.leaveDays"></el-input>
				</el-form-item>
				<el-form-item label="请假原因">
					<el-input v-model="form.leaveReason"></el-input>
				</el-form-item>




				<el-form-item>
					<el-button type="primary" @click="handleSave">保存</el-button>
					<el-button type="primary" @click="handleSavePublis">保存并提交</el-button>
					<el-button @click="handleCancelSave">取消</el-button>

				</el-form-item>
			</el-form>

		</el-dialog>


	</div>
</template>

<script>
	import axios from '@/axios';
	export default {
		data() {
			return {
				//表格初始化参数
				tableData: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [10, 50, 100, 200],
				pageSize: 10,
				//查询条件
				queryProcessName: '',
				//录入框
				dialogVisible: false,
				form: {
					id: '',
					leaveName: '',
					leaveBeginDate: '',
					leaveEndDate: '',
					leaveDays: '',
					leaveReason: ''

				},
				isShow: true
			}
		},
		//界面渲染完毕调用   初始化表格参数
		mounted() {
			this.getTableData()
		},
		methods: {
			// 获取table数据
			getTableData() {
				//console.log("获取table数据getTableData")
				var url = 'LeaveTest/queryPage.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					leaveName: this.queryLeaveName,
					userId: sessionStorage.getItem("userId")
				}
				axios.get(url, param).then(response => {
					this.tableData = response.data.rows
					this.totalRows = response.data.total
				});
			},
			// 修改每页条数事件
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.currentPage = val
				this.getTableData()
			},
			//保存按钮事件
			handleSave(row) {

				var url = 'LeaveTest/save.do'
				var param = {
					id: this.form.id,
					leaveName: this.form.leaveName,
					leaveBeginDate: this.form.leaveBeginDate,
					leaveEndDate: this.form.leaveEndDate,
					leaveDays: this.form.leaveDays,
					leaveReason: this.form.leaveReason,
					processKey:'testProcess1',
					userId: sessionStorage.getItem("userId")
				}
				axios.post(url, param).then(response => {
					if (response.data === 'success') {
						this.$message({
							type: 'success',
							message: '保存成功!',
						});
						this.getTableData()
						this.dialogVisible = false
					} else {
						this.$message({
							type: 'error',
							message: '保存失败' + response.data
						});
					}
				});



			},
			//保存取消按钮事件   关闭编辑对话框
			handleCancelSave(row) {
				this.dialogVisible = false
			},

			// 新增按钮事件
			handleAdd() {
				this.dialogVisible = true
				this.form = {
					id: '',
					leaveName: '',
					leaveBeginDate: '',
					leaveEndDate: '',
					leaveDays: '',
					leaveReason: ''
				}
			},
			// 编辑按钮事件
			handleEdit(row) {
				this.dialogVisible = true
				this.form = {
					id: row.id,
					leaveName: row.leaveName,
					leaveBeginDate: row.leaveBeginDate,
					leaveEndDate: row.leaveEndDate,
					leaveDays: row.leaveDays,
					leaveReason: row.leaveReason
				}
			},
			// 删除按钮事件
			handleDelete(id) {
				this.$confirm('此操作将永久删除该, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {

					var url = 'LeaveTest/remove.do'
					var param = {
						id: id,
						userId: sessionStorage.getItem("userId")

					}
					axios.post(url, param).then(response => {
						if (response.data === 'success') {
							this.$message({
								type: 'success',
								message: '删除成功!',
							});
							this.getTableData()
						} else {
							this.$message({
								type: 'error',
								message: '删除失败' + response.data
							});
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			// 查询按钮事件
			search() {
				this.getTableData()
			},
			// 发布按钮事件
			handlePublish(id) {
				this.$confirm('确定发布该流程？, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {

					var url = 'LeaveTest/publish.do'
					var param = {
						id: id,
						userId: sessionStorage.getItem("userId")
					}
					axios.post(url, param).then(response => {
						if (response.data === 'success') {
							this.$message({
								type: 'success',
								message: '发布成功!',
							});
							this.getTableData()
						} else {
							this.$message({
								type: 'error',
								message: '发布失败' + response.data
							});
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消发布'
					});
				});
			},
		}
	}
</script>
<style>
	
</style>
