<template>
<div class="app-container">
	<el-form
		:model="queryParams"
		ref="queryForm"
		size="small"
		:inline="true"
		v-show="showSearch"
		label-width="68px"
		class="newform"
	>
		<el-form-item label="水表名称" prop="equipmentName" class="newitem">
			<el-select v-model="queryParams.equipmentName" clearable placeholder="请选择水表名称"  >
				<el-option
					v-for="item in waterselect"
					:label="item.name"
					:value="item.value">
				</el-option>
			</el-select>
		</el-form-item>	
		<el-form-item>
		<el-button
			type="primary"
			icon="el-icon-search"
			size="mini"
			@click="toSearch"
			 style="height:35px;margin-left: 20px;"
			>搜索</el-button>
			<el-button
				type="success"
				plain
				icon="el-icon-plus"
				size="mini"
				 style="height:35px;margin-left: 20px; margin-top: 20px;"
				@click="addEqupment()"
			>新增用水设备</el-button>
			<el-button
				type="primary"
				plain
				icon="el-icon-view"
				size="mini"
				 style="height:35px;margin-left: 20px; margin-top: 20px;"
				@click="seewater()"
			>查看计划总用水/取水量</el-button>
		</el-form-item>
    </el-form>
    <el-row :gutter="10" class="threestyle">
		<el-col :span="1.5">
		<div></div>
		</el-col>
		<right-toolbar
			:showSearch.sync="showSearch"
			@queryTable="allData">
		</right-toolbar>
    </el-row>
    <el-table :data="dmList"  height="535">
	<el-table-column label="序号" align="center" prop="id" width="60" type="index"/>
	<el-table-column label="水表名称" align="center" prop="equipmentName" />
	<el-table-column label="月计划限量值(m³)" align="center" prop="monthvalue" />
	<el-table-column label="时间" align="center" prop="month" />
		<el-table-column label="操作" fixed="right" width="180">
			<template slot-scope="scope">
				<el-button
					size="mini"
					type="primary"
					icon="el-icon-edit"
					@click="typeEdit($event, scope.$index, scope.row)"
					>编辑</el-button
				>
				<el-button
					size="mini"
					type="danger"
					icon="el-icon-delete"
					@click="typeDelete(scope.row)"
					>删除</el-button
				>
			</template>
		</el-table-column>
	</el-table>
   <el-pagination
		style="margin-top: 20px;"
		:page-sizes="[10, 20, 30, 40]"
		layout="total, sizes, prev, pager, next, jumper"
		:total="total"
		@size-change="handleSizeChange"
		@current-change="handleCurrentChange"
		@pagination="allData"
    />
	<!-- 添加对话框 -->
	<el-dialog :title="title" :visible.sync="open" append-to-body >
	  <el-form ref="form" :model="addform" :rules="rules" label-width="160px" style="width:100%;height: 100%;display: flex;flex-direction: column;">
	    <div style="width:100%;height: 50%; display: flex; align-items: center;">
			<el-form-item label="水表名称" prop="equipmentName">
				<el-select v-model="addform.equipmentName" clearable placeholder="请选择水表名称"  >
					<el-option
						v-for="item in waterselect"
						:label="item.name"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="时间" prop="month" style="margin-left:13px;">
				 <el-date-picker
				  v-model="addform.month"
				  type="month"
				  placeholder="选择时间"
				  value-format="yyyy-MM"
				  >
				</el-date-picker>
			</el-form-item>
		
		</div>
	
		<div style="width:100%;height: 50%; display: flex;">
			<el-form-item label="月计划限量值" prop="monthvalue" style="margin-left:13px;">
				<el-input v-model="addform.monthvalue" placeholder="请输入月计划限量值"  />
			</el-form-item>
			
		</div>
	</el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitForm">确 定</el-button>
	  </div>
	</el-dialog>
	<!-- 编辑 -->
	<el-dialog :title="title" :visible.sync="openTwo" append-to-body >
	  <el-form ref="form" :model="addform" :rules="rules" label-width="160px" style="width:100%;height: 100%;display: flex;flex-direction: column;">
	   <div style="width:100%;height: 50%; display: flex; align-items: center;">
	   	<el-form-item label="水表名称" prop="equipmentName">
	   		<el-select v-model="addform.equipmentName" clearable placeholder="请选择水表名称"  >
	   			<el-option
	   				v-for="item in waterselect"
	   				:label="item.name"
	   				:value="item.value">
	   			</el-option>
	   		</el-select>
	   	</el-form-item>
	   	<el-form-item label="时间" prop="month" style="margin-left:13px;">
	   		 <el-date-picker
	   		  v-model="addform.month"
	   		  type="month"
	   		  placeholder="选择时间"
	   		  value-format="yyyy-MM"
	   		  >
	   		</el-date-picker>
	   	</el-form-item>
	   
	   </div>
	   	
	   <div style="width:100%;height: 50%; display: flex;">
	   	<el-form-item label="月计划限量值" prop="monthvalue" style="margin-left:13px;">
	   		<el-input v-model="addform.monthvalue" placeholder="请输入月计划限量值"  />
	   	</el-form-item>
	   </div>
	  </el-form>
	 <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitedit">确 定</el-button>
	  </div>
	</el-dialog>
	
	<!-- 查看 -->
	<el-dialog :title="title" :visible.sync="openSpecific" append-to-body class="pop-contentwater">
		<div class="popwater" v-for="item in this.popData">
			<div class="popfl">
				<span>时间:{{item.month}}</span>
				<span>总计划用水量:{{item.usewater}}m³</span>
				<span>总计划取水量:{{item.intakwater}}m³</span>
			</div>
	
		</div>
	</el-dialog>
  </div>
</template>

<script>
import { getWatername,addwatername,getdata,editEqu,deleteEqu,getYq} from "@/api/historyData/plan";
import { parseTime } from "../../utils/index.js";
export default {
  data() {
    return {
		waterselect:[],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dmList: [],
	  selectList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
	  openTwo:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        watername:null
      },
	  searchTime:"",
	    // 表单参数
	    addform: {},
	    // 表单校验
	    rules: {
			equipmentName: [{ required: true, message: "设备名称不能为空", trigger: "change" }],
			month: [{ required: true, message: "时间不能为空", trigger: "change" }],
			monthvalue: [{ required: true, message: "月计划限量值不能为空", trigger: "change" }],
	    },
		openSpecific:false,
		popData:[]
    };
  },
  mounted(){ 
	getWatername().then(function(res){
		var body = res.data || [];
		this.waterselect = body;
	}.bind(this));
	this.allData();
  },
  methods: {
	  allData() {
	  	var param ={
			pageNum:this.queryParams.pageNum,
			pageSize: this.queryParams.pageSize
		}
	  	getdata(param).then((res) => {
	  	   var body = res.rows || [];
		   this.dmList = body;
		   this.total = res.total;
	  	});
	  },
	  //获取全部信息管理分页
	  handleSizeChange(val) {
	  // 每页多少条
		this.queryParams.pageSize =val;
		this.toSearch()
	  },
	  handleCurrentChange(val) {
	  // 当前是多少页
		this.queryParams.pageNum = val;
		this.toSearch()
	  },
	  toSearch() {
		var param = this.queryParams;
		getdata(param).then((res) => {
			var body = res.rows || [];
			this.dmList = body;
			this.total = res.total;
		});
	},
	
	// 新增
	addEqupment(){
	  this.open = true;
	  this.title = "新增用水设备";
	  this.addform = {};
	},
	submitForm() {
		this.$refs.form.validate((valid) => {
			if (valid) {
				var self = this;
				var param = this.addform;
				addwatername(param).then((res) =>{
					if(res.code === 200){
						this.open = false;
						this.allData();
						self.$message({
						  type: "success",
						  message: "新增成功",
						});
					}else{
						self.$message.error("新增失败");
					}
				});
			}
		}) 
	},
	// 修改
	typeEdit(e, index, item){
	  this.openTwo = true;
	  this.title = "修改用水设备";
	  this.addform = Object.assign({}, item);
	},
	submitedit() {
		this.$refs.form.validate((valid) => {
			if (valid) {
				var self = this;
				var param = this.addform;
				editEqu(param).then((res) => {
					if(res.code === 200){
						this.openTwo = false;
						this.allData();
						self.$message({
						  type: "success",
						  message: "编辑成功",
						});
					}else{
						self.$message.error("编辑失败");
					}
				});
			}
		}) 
	},
	// 删除
	typeDelete(item){
		var id = item.id;
		this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
			confirmButtonText: "确定",
			type: "warning",
		}).then(() => {
	    deleteEqu({id:id}).then((res) => {
			this.allData()
			if(res.code===200){
				this.$message({
					message: "删除成功",
					type: "success",
				});  
			}
		},
		() => {
			this.$message.error("删除失败");
		});
	  });
	},
	//查看
	seewater(){
		this.openSpecific=true;
		this.title = "查看总水量";
		getYq().then((res) => {
			var body = res.data||[];
			this.popData = body
		});
	},
	
	
	
	
	
	
  } 
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
	.newform{
		width: 1680px;
		height: 75px;
		background-color: #fff;
		display: flex;
		justify-content: flex-start;
		align-items: center;
		
		
	}
	.newitem{
		margin-left: 30px;
		margin-bottom: 0px!important;
	}
	.threestyle{
		margin-top: 15px;
		margin-bottom: 15px;
	}
	.popwater{
		width: 100%;
		height: 3rem;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	.popfl{
		height: 50%;
		font-size: 1rem;
		font-weight: bold;
		letter-spacing: 2px;
		display: flex;
		justify-content: space-around;
	}
	.pop-contentwater  .el-dialog{
	   width: 45rem;
	   height: 20rem;
	 }
	 .pop-contentwater .el-dialog__body{
	  overflow-y: scroll;
	   max-height: 20rem;
	  }
	  .pop-contentwater ::-webkit-scrollbar {
	      width: 1px;
	      background-color: #f0f0f0;
	  }
    .el-table--scrollable-y .el-table__body-wrappe{
      overflow-y: scroll!important;
    }
	

</style>
