<template>
	<div>
		<el-row>
			<el-form :model="query" label-width="100px" inline="">
				<el-form-item label="客户名称">
					<el-input v-model="query.customerName" size="small" style="width: 300px;">
						<el-button slot="append" icon="el-icon-search" @click="showCustomer"></el-button>
					</el-input>
				</el-form-item>
				<el-button type="info" @click="search" size="small">查询</el-button>
				<el-button type="primary" @click="handleAdd" size="small">新增</el-button>
			</el-form>
		</el-row>

		<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="customer.name" label="客户名称" width="180" show-overflow-tooltip ></el-table-column>
			<el-table-column prop="keepDay" label="保存天数" width="180" show-overflow-tooltip ></el-table-column>
			<el-table-column prop="ordPrice" label="价格" width="180" show-overflow-tooltip ></el-table-column>
			<el-table-column prop="outOrdPrice" label="超期价格" width="180" show-overflow-tooltip ></el-table-column>
			<el-table-column prop="addUser.userDesc" label="添加人" width="120" show-overflow-tooltip ></el-table-column>
			<el-table-column prop="addTime" label="添加时间" width="200" show-overflow-tooltip ></el-table-column>
			<el-table-column label="操作" width="200">
				<template slot-scope="scope">
					<!-- 编辑、删除向后台传参数时，传整行数据row  删除时id=row.id -->
					<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>


		<!-- 分页 -->
		<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>

		<!-- 弹出框  另起名字  var json = JSON.stringify(this.自己起的名字)
        var url = 后台对应的url 'MaintPrice/save.do' -->
		<el-dialog title="价格配置" :visible.sync="dialogVisible" width="500px">
			<el-form :model="obj" label-width="100px">
				<el-form-item label="id" v-if="isShow">
					<el-input v-model="obj.id"></el-input>
				</el-form-item>
				<el-form-item label="客户名称">
					<el-input v-model="obj.customer.name" style="width: 300px;">
						<el-button slot="append" icon="el-icon-search" @click="showCustomerDialog"></el-button>
					</el-input>
				</el-form-item>
				<el-form-item label="保存时间">
					<el-input v-model="obj.keepDay" style="width: 300px;"></el-input>
				</el-form-item>
				<el-form-item label="价格">
					<el-input v-model="obj.ordPrice" style="width: 300px;"></el-input>
				</el-form-item>
				<el-form-item label="超期价格">
					<el-input v-model="obj.outOrdPrice" style="width: 300px;"></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>


		<popup-material ref="PopupMaterial" @receviceFromChild="receviceFromMaterial"></popup-material>
		<popup-customer ref="PopupCustomer" @receviceFromChild="receviceFromCustomer"></popup-customer>
		<popup-material-dialog ref="PopupMaterialDialog" @receviceFromChild="receviceFromMaterialDialog"></popup-material-dialog>
		<popup-customer-dialog ref="PopupCustomerDialog" @receviceFromChild="receviceFromCustomerDialog"></popup-customer-dialog>
	</div>
</template>
<script>
	import axios from '@/axios'
	import PopupMaterial from '@/components/PopupMaterial.vue'
	import PopupMaterialDialog from '@/components/PopupMaterial.vue'
	import PopupCustomer from '@/components/PopupCustomer.vue'
	import PopupCustomerDialog from '@/components/PopupCustomer.vue'
	export default {
		components: {
			PopupMaterial,
			PopupCustomer,
			PopupMaterialDialog,
			PopupCustomerDialog
		},
		data() {
			return {
				options: [{
					value: '0',
					label: '未货权转移'
				}, {
					value: '1',
					label: '已货权转移'
				}],
				tableData: [],
				currentPage: 1,
				totalRows: 0,
				pageSizes: [50,100,200,500],
				pageSize: 50,
				//查询条件
				query: {
					customerName: ''
				},
				//录入框
				dialogVisible: false,
				isShow: false,
				loading: true,
				obj: {
					customer: {
						id: '',
						name: '',
					},
					keepDay: '',
					ordPrice: '',
					outOrdPrice: '',
				},

			}
		},
		//界面渲染完毕调用   初始化表格参数
		mounted() {
			this.getTableData()
		},
		methods: {
			typeFormatter: function(row, column) {
				switch (row.storageType) {
					case "0":
						return "未货权转移";
						break;
					default:
						return "已货权转移";
						break;
				}
			},
			
			showMaterial() {
				this.$refs.PopupMaterial.show()
			},
			showCustomer() {
				this.$refs.PopupCustomer.show()
			},
			receviceFromMaterial(material) {
				this.query.materialName = material.name
				this.query.standard = material.standard
			},
			receviceFromCustomer(customer) {
				this.query.customerName = customer.name
			},
			showMaterialDialog() {
				this.$refs.PopupMaterialDialog.show()
			},
			showCustomerDialog() {
				this.$refs.PopupCustomerDialog.show()
			},
			receviceFromMaterialDialog(material) {
				this.obj.material.id = material.id
				this.obj.material.name = material.name
				this.obj.material.standard = material.standard
			},
			receviceFromCustomerDialog(customer) {
				this.obj.customer.id = customer.id
				this.obj.customer.name = customer.name
			},
			getTableData() {
				this.loading = true
				var url = 'MaintPrice/query.do'
				var param = {
					page: this.currentPage,
					rows: this.pageSize,
					materialName: this.query.customerName,
				}
				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) {
				console.log(`每页 ${val} 条`);
				this.pageSize = val
				this.getTableData()
			},
			// 修改当前页事件
			handleCurrentChange(val) {
				console.log(`当前页: ${val}`);
				this.currentPage = val
				this.getTableData()
			},

			//保存按钮事件     
			handleSave(row) {
				var json = JSON.stringify(this.obj)
				var url = 'MaintPrice/save.do'
				var param = {
					json: json
				}
				axios.post(url, param).then(response => {
					//alert(JSON.stringify(response))
					if (response.data.code == 0) {
						this.$message({
							type: 'success',
							message: '保存成功!',
						});
						this.getTableData()
						this.dialogVisible = false
					} else {
						this.$message({
							type: 'error',
							message: '保存失败:' + response.data,
						});
					}
					// loading.close();
				});

				//         console.log(json)
				//
			},
			//保存取消按钮事件   关闭编辑对话框
			handleCancelSave(row) {
				this.dialogVisible = false
			},
			// 新增按钮事件
			handleAdd() {
				this.dialogVisible = true
				this.obj = {
					customer: {
						id: '',
						name: '',
					},
					keepDay: '',
					ordPrice: '',
					outOrdPrice: '',
				}

			},
			// 编辑按钮事件
			handleEdit(row) {
				this.dialogVisible = true
				this.obj = row
			},

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

					var url = 'MaintPrice/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.currentPage = 1
				this.getTableData()
			}
		}
	}
</script>
