<template>
	<div>
		<div style="margin-left: 10px;">
			<el-button style="margin-left: 10px;" size="small" type="success" @click="handleDownload">下载模板</el-button>


		</div>
		<div style="margin-left: 10px;margin-top: 20px;">

			<el-upload class="upload-demo" ref="upload" action="123" :on-change="handleChange" :on-remove="handleRemove"
			 :auto-upload="false" :before-remove="beforeRemove">
				<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
				<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">导入</el-button>
				<el-button style="margin-left: 10px;" size="small" type="" @click="handleExport">导出</el-button>
			</el-upload>

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

	</div>
</template>

<script>
	import axios from '@/axios';
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			...mapState({
				serverName: state => state.app.serverName
			})
		},
		data() {
			return {
				tableData: [],
				tableProperty: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [10, 50, 100, 200],
				pageSize: 10,
				importUrl: '',

				fileListTemp: [],
				formData: new FormData(),
			};
		},
		methods: {

			submitUpload() {
				for (let i = 0; i < this.fileListTemp.length; i++) {
					this.formData.append('param', '1' )
					this.formData.append('file', this.fileListTemp[i].raw)
				}
				axios.post('DevImexport/importExcel.do', this.formData).then(response => {

					if (response.data === 'success') {
						this.$message({
							type: 'success',
							message: '导入成功!',
						});
						this.formData = new FormData()
						this.fileListTemp = []
						this.$refs.upload.submit()
						this.getTableData()
						 

					} else {
						this.$message({
							type: 'error',
							message: '导入失败' + response.data
						});
						this.fileListTemp = []
						this.formData = new FormData()
						this.$refs.upload.submit()
						this.getTableData()
					 
					}

				})

			},
			handleDownload() {
				location.href = this.serverName + "/DevImexport/download.do";
			},

			handleExport() {
				location.href = this.serverName + "/DevImexport/exportExcel.do";
			},
 
			handleRemove(file, fileList) {
				this.fileListTemp = fileList
			},
			handleChange(file, fileList) {
				this.fileListTemp = fileList
			},
			beforeRemove(file, fileList) {
				return this.$confirm(`确定移除 ${ file.name }？`);
			},
			//查询table数据
			getTableData() {
				//console.log("获取table数据getTableData")
				var url = 'DevImexport/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize
				}
				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
					}
				});
			},
			//查询table列
			getTableProperty() {
				//console.log("获取table数据getTableData")
				var url = 'CodeMachine/queryTableProperty.do'
				var param = {
					bussinessName: 'DEV_IMEXPORT'
				}
				axios.get(url, param).then(response => {
					this.tableProperty = response.data
				});
			},

			// 修改每页行数
			handleSizeChange(val) {
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				this.currentPage = val
				this.getTableData()
			},
		},
		mounted() {
			this.getTableData()
			this.getTableProperty()
			this.importUrl = this.serverName + "/DevImexport/importExcel.do"
		}
	}
</script>

<style>

</style>
