<template>
	<div>
		<div style="width:600px;float: left; margin: 10px;">
			<el-input v-model="queryTableName" placeholder="表名"/>
			<el-button type="success" @click="handleSearch">查询</el-button>
			<el-button type="primary" @click="handleAdd">新增</el-button>
		</div>
		<el-table v-loading="loading" :data="tableData" stripe border style="width: 99%;">
			<el-table-column v-for="item in tableProperty" :key="item.id" :prop="item.tableProp" :label="item.tableLabel" :width="item.tableWidth" v-if="item.attr1==='0'">
			</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>
				</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-position="right" label-width="80px">
				<el-form-item label="id">
					<el-input v-model="form.id" placeholder=""></el-input>
				</el-form-item>
				<el-form-item label="业务名称">
					<el-input v-model="form.bussinessName" placeholder=""></el-input>
				</el-form-item>
				<el-form-item label="挂载流程">
					<el-select v-model="form.processName" placeholder="请选择">
						<el-option v-for="item in optionsProcess" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="数据表">
					<el-select v-model="form.dbTableName" placeholder="请选择">
						<el-option v-for="item in optionsTable" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="包名">
					<el-input v-model="form.packageName" placeholder="com.th.demo"></el-input>
				</el-form-item>
				<el-form-item label="模块名">
					<el-input v-model="form.moduleName" placeholder=""></el-input>
				</el-form-item>
				<el-button type="success" @click="handleGenerate">一键生成</el-button>
			</el-form>
		</el-dialog>


	</div>
</template>
<script>
	import axios from '@/axios';
	export default {
		data() {
			return {
				tableData: [],
				tableProperty: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [10, 50, 100, 200],
				pageSize: 10,
				optionsProcess: [],
				optionsTable: [],
				form: {
					id: '',
					dbTableName: '',
					packageName: '',
					moduleName: '',
					bussinessName: '',
					processName: '',
				},
				loading: true,
				dialogVisible: false,
				queryTableName:'',
			}
		},
		methods: {
			getTableName() {
				//console.log("获取table数据getTableData")
				var url = 'CodeMachine/queryTableName.do'
				var param = {

				}
				axios.get(url, param).then(response => {
					this.optionsTable = response.data

				});
			},
			getProcessName() {
				//console.log("获取table数据getTableData")
				var url = 'CodeMachine/queryProcessName.do'
				var param = {

				}
				axios.get(url, param).then(response => {
					this.optionsProcess = response.data

				});
			},
			handleGenerate() {
				//console.log("获取table数据getTableData")
				var url = 'CodeMachine/generate.do'
				var param = {
					id: this.form.id,
					bussinessName: this.form.bussinessName,
					dbTableName: this.form.dbTableName,
					packageName: this.form.packageName,
					moduleName: this.form.moduleName,
					processName: this.form.processName,
					userId: sessionStorage.getItem('userId'),
				}
				axios.post(url, param).then(response => {
					//this.optionsProcess = response.data
					this.dialogVisible=false
					this.getTableData()
				});
			},
			handleSearch(){
				this.getTableData()
			},
			getTableData() {
				//console.log("获取table数据getTableData")
				var url = 'CodeMachine/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					queryTableName:this.queryTableName
				}
				axios.get(url, param).then(response => {
					this.tableData = response.data.list
					this.totalRows = response.data.total
					if (response) {
						this.loading = false
						
					} else {
						this.loading = true
					}
				});
			},
			getTableProperty() {
				//console.log("获取table数据getTableData")
				var url = 'CodeMachine/queryTableProperty.do'
				var param = {
					 bussinessName:'test'
				}
				axios.get(url, param).then(response => {
					this.tableProperty = response.data 
				});
			},
			// 修改每页条数事件
			handleSizeChange(val) {
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				this.currentPage = val
				this.getTableData()
			},
			handleAdd() {
				this.dialogVisible = true
				this.form = {
					id: '',
					dbTableName: '',
					packageName: '',
					moduleName: '',
					bussinessName: '',
					porcessName: '',
				}
			},
			// 编辑按钮事件
			handleEdit(row) {
				this.dialogVisible = true
				this.form = {
					id: row.id,
					dbTableName: row.dbTableName,
					packageName: row.packageName,
					moduleName: row.moduleName,
					bussinessName: row.bussinessName,
					porcessName: row.porcessName,
				}
			},
			// 删除按钮事件
			handleDelete(id) {
				this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {

					var url = 'CodeMachine/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: '已取消删除'
					});
				});
			},
		},
		mounted() {
			this.getTableProperty()
			this.getTableData()
			this.getTableName()
			this.getProcessName()
		}
	}
</script>
