<template>
    <div style="text-align: left;">
		<el-row>
			<el-select size="small" v-model="queryWare" clearable placeholder="请选择" @change="change" @clear="clear">
				<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
				</el-option>
			</el-select>
			<el-button type="info" size="small" @click="search">查询</el-button>
			<el-button type="primary" size="small" @click="handleAdd">新增</el-button>
		</el-row>
    
		<el-table :data="tableData" border>
			<el-table-column prop="id" label="编号" width="60" v-if="isShow">
			</el-table-column>
			<el-table-column prop="ware.name" label="所属库房" width="120">
			</el-table-column>
			<el-table-column prop="name" label="分区名称" width="120">
			</el-table-column>
			<el-table-column prop="code" label="分区编码" width="120">
			</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="area" label-width="100px" ref="area">
				<el-form-item label="id" v-if="isShow">
					<el-input v-model="atea.id"></el-input>
				</el-form-item>
				<el-form-item label="所属库房">
					<el-select v-model="area.ware.id" clearable placeholder="请选择" style="width: 100%;">
						<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
						</el-option>
					</el-select>

				</el-form-item>
				<el-form-item label="分区名称">
					<el-input v-model="area.name"></el-input>
				</el-form-item>
				<el-form-item label="分区编码">
					<el-input v-model="area.code"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="success" @click="handleSave">保存</el-button>
					<el-button @click="handleCancelSave">取消</el-button>
				</el-form-item>
			</el-form>
		</el-dialog>


	</div>
</template>

<script>
    import axios from '@/axios'
	import {
		mapState
	} from 'vuex'
	export default {
        data() {
            return {
                queryWare: '',
                options: [],
                tableData: [],

                currentPage: 1,
				totalRows: 0,
				pageSizes: [50, 100, 200,500],
				pageSize: 50,

                loading: true,
                isShow: false,
                dialogVisible: false,

                area: {
                    id: '',
					name: '',
					code: '',
					ware: {
						id: '',
						name: ''
					},
                }

            }
        },
        //界面渲染完毕调用   初始化表格参数
		mounted() {
			this.getAllWare()
		},
        methods: {
            //
            getAllWare() {
				var url = 'MaintWare/queryByUserId.do'
				var param = {}
				axios.get(url, param).then(response => {
					this.options = response.data.data
				});
			},
            handleAdd() {

                this.dialogVisible=true;
				//this.$$refs.area.clearValidate();
            },
            handleCancelSave(row) {
				this.dialogVisible = false
			},
            getTableData() {
				var url = 'MaintArea/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					queryWare: this.queryWare
				}
				axios.get(url, param).then(response => {
					if (response.data.code == 0) {
						this.tableData = response.data.data.list
						this.totalRows = response.data.data.total
						//this.drawWareLayout()
					} else {
						this.$message({
							type: 'error',
							message: '查询失败' + response.data.msg,
						});
					}
				});
			},
            search() {
				this.currentPage = 1
				this.getTableData()
			},
            handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.currentPage = val
				this.getTableData()
			},
            handleSave() {
				var tempStack = this.area;
				var json = JSON.stringify(tempStack)
				var url = 'MaintArea/save.do'
				var param = {
					json: json
				}
				axios.post(url, param).then(response => {
					if (response.data.code == 0) {
						this.$message({
							type: 'success',
							message: '保存成功!',
						});
						this.getTableData()
						this.dialogVisible = false
					} else {
						this.$message({
							type: 'error',
							message: '保存失败' + response.data.msg,
						});
					}
					// loading.close();
				});



			},

			// 删除按钮事件
			handleDelete(id) {
				this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {

					var url = 'MaintArea/remove.do'
					var param = {
						id: id
					}
					axios.post(url, param).then(response => {
						if (response.data.code == 0) {
							this.$message({
								type: 'success',
								message: '删除成功!',
							});
							this.getTableData()
						} else {
							this.$message({
								type: 'error',
								message: '删除失败;' + response.data.msg
							});
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			// 编辑按钮事件
			handleEdit(row) {
				var tempRow = row;
				//tempRow.ware.name = tempRow.ware.id;
				this.dialogVisible = true
				this.area = tempRow
			},
        }
	}
</script>
