<template>
	<div style="text-align: center;">
		<div style="width:600px;float: left; margin-left: 10px;margin-bottom: 10px;">
			<el-input v-model="queryUserDesc" size="small" placeholder="请输入查询条件" style="width :300px;float: left;"></el-input>
			<el-button type="info" size="small" @click="search">查询</el-button>
			<el-button type="primary" size="small" @click="handleAdd">新增</el-button>
		</div>

		<div style="padding-right: 30px;">
			<el-table v-loading="loading" :data="tableData" border>
				<el-table-column prop="id" label="编号" width="60" v-if="isShow">
				</el-table-column>
				<el-table-column prop="userName" label="用户名" width="120">
				</el-table-column>
				<el-table-column prop="password" label="密码" width="120">
				</el-table-column>
				<el-table-column prop="userDesc" label="姓名" width="120">
				</el-table-column>
				<!-- <el-table-column prop="customerName" label="客户" width="270">
				</el-table-column> -->
				<el-table-column prop="attr3"  label="客户id" width="270">
				</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)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<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="user" label-width="100px">
				<el-form-item label="用户编码" v-if="isShow">
					<el-input v-model="user.id"></el-input>
				</el-form-item>
				<el-form-item label="用户名">
					<el-input v-model="user.userName"></el-input>
				</el-form-item>
				<el-form-item label="密码">
					<el-input v-model="user.password"></el-input>
				</el-form-item>
				<el-form-item label="姓名">
					<el-input v-model="user.userDesc"></el-input>
				</el-form-item>
				<el-form-item label="选择客户">
					<el-input  v-model="user.attr3"  :readonly="true"><el-button @click="clearCustomer" slot="append" icon="el-icon-delete"></el-button><el-button @click="popCust" slot="append" icon="el-icon-search"></el-button></el-input>
				</el-form-item>
			<!-- 	<el-form-item label="客户id" v-if="false">
					<el-input v-model="user.attr3"></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>
		<!-- <el-dialog title="修改密码" :visible.sync="dialogVisible2" width="500px"> -->
			<popup-customer ref="popupCustomer" @receviceFromChild="receviceFromCustomer"></popup-customer>
		<!-- </el-dialog> -->


	</div>
</template>

<script>
	import axios from '@/axios';
	import PopupStore from '../../components/PopupStore.vue';
	import PopupCustomer   from '../../components/PopupCustomer.vue'
	export default {
		components: {
			PopupStore,
			PopupCustomer,
		},
		data() {
			return {
				//表格初始化参数
				tableData: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [5, 10, 15, 20],
				pageSize: 10,
				//查询条件
				queryUserDesc: '',
				//录入框
				dialogVisible: false,
				user: {
					id: '',
					userName: '',
					password: '',
					userDesc: '',
					attr3:'',
					attr4:'',
				},
				isShow: false,
				loading: true,
				dialogVisible2:false

			}
		},
		//界面渲染完毕调用   初始化表格参数
		mounted() {
			this.getTableData()
		},
		methods: {
			// 获取table数据
			getTableData() {
				this.loading = true
				var url = 'SysUser/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					userDesc: this.queryUserDesc
				}
				axios.get(url, param).then(response => {
					if (response.data.code == 0) {
						this.tableData = response.data.data.list
						this.totalRows = response.data.data.total
					} else {
						this.$message({
							type: 'error',
							message: '保存失败' + response.data.msg,
						});
					}
					this.loading = false
				});
			},
			// 修改每页条数事件
			handleSizeChange(val) {
				
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
			
				this.currentPage = val
				this.getTableData()
			},
			//+++++++弹出选择窗口
			handleSearchStore() {
			
				this.$refs.popupStore.show()
			},
			//获取项目信息  赋值
			receviceFromStore(store) {
				console.log(JSON.stringify(store))
				console.log(store.agentCd)
				this.user.attr1 = store.agentCd
			},
			//保存按钮事件
			handleSave() {

				if(this.user.attr3.length == 0 && this.user.attr4.length != 0){
					this.user.attr4 = '';
				}
				if((this.user.attr3.length != 0 && this.user.attr4.length == 0) ){
					this.$message({
							type: 'warning',
							message: '请选择客户',
					});
					return;
				}
				var json = JSON.stringify(this.user)
				var url = 'SysUser/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();
				});



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

				this.user = {
					id: '',
					userName: '',
					password: '',
					userDesc: '',
					attr3:'',
					attr4:''
				}
			},
			// 编辑按钮事件
			handleEdit(row) {
				this.dialogVisible = true
				this.user = row
			},

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

					var url = 'SysUser/remove.do'
					var param = {
						id: row.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: '已取消删除'
					});
				});
			},
			// 查询按钮事件
			search() {
				this.getTableData()
			},
			popCust(){
				// this.dialogVisible2 = true;
				this.$refs.popupCustomer.show();
			},
			receviceFromCustomer(customer) {
				this.user.attr3 = customer.name;
				this.user.attr4 = customer.id;

			},
			clearCustomer(){
				this.user.attr4 = '';
				this.user.attr3 = '';
			}
		}
	}
</script>
