<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园区代码" prop="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入园区代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量设备" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入计量设备"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录时间">
        <el-date-picker
          v-model="daterangeRecordTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ems:elecMeterH:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ems:elecMeterH:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="elecMeterHList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="园区名称" align="center" prop="areaCode" />
      <el-table-column label="计量设备" align="center" prop="deviceCode" />
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间序列" align="center" prop="timeIndex" />
      <el-table-column label="用电量(kW·h)" align="center" prop="elecQuantity" />
      <el-table-column label="计量类型" align="center" prop="meterType" >
        <template slot-scope="scope">
          <span>{{ getElecMeterType(scope.row.meterType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位电价" align="center" prop="meterUnitPrice" />
      <el-table-column label="小时电费" align="center" prop="useElecCost" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用电计量-小时对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="园区代码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入园区代码" />
        </el-form-item>
        <el-form-item label="计量设备" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入计量设备" />
        </el-form-item>
        <el-form-item label="记录时间" prop="recordTime">
          <el-input v-model="form.recordTime" placeholder="记录时间" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期 yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-time-picker clearable
            v-model="form.time"
            type="date"
            value-format="HH:mm:ss"
            placeholder="请选择时间 HH:mm:ss">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="时间序列" prop="timeIndex">
          <el-input v-model="form.timeIndex" placeholder="请输入时间序列" />
        </el-form-item>
        <el-form-item label="用电量" prop="elecQuantity">
          <el-input v-model="form.elecQuantity" placeholder="请输入用电量 单位：kW-h" />
        </el-form-item>
        <el-form-item label="单位电价" prop="meterUnitPrice">
          <el-input v-model="form.meterUnitPrice" placeholder="请输入单位电价" />
        </el-form-item>
        <el-form-item label="小时电费" prop="useElecCost">
          <el-input v-model="form.useElecCost" placeholder="请输入小时电费" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listElecMeterH, delElecMeterH, addElecMeterH, updateElecMeterH } from "@/api/device/elecMeterH";

export default {
  name: "ElecMeterH",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用电计量-小时表格数据
      elecMeterHList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 小时电费时间范围
      daterangeRecordTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        deviceCode: null,
        recordTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: [
          { required: true, message: "园区代码不能为空", trigger: "blur" }
        ],
        deviceCode: [
          { required: true, message: "计量设备不能为空", trigger: "blur" }
        ],
        recordTime: [
          { required: true, message: "记录时间不能为空", trigger: "blur" }
        ],
        date: [
          { required: true, message: "日期 yyyy-MM-dd不能为空", trigger: "blur" }
        ],
        time: [
          { required: true, message: "时间 HH:mm:ss不能为空", trigger: "blur" }
        ],
        timeIndex: [
          { required: true, message: "时间序列不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用电计量-小时列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRecordTime && '' != this.daterangeRecordTime) {
        this.queryParams.params["beginRecordTime"] = this.daterangeRecordTime[0];
        this.queryParams.params["endRecordTime"] = this.daterangeRecordTime[1];
      }
      listElecMeterH(this.queryParams).then(response => {
        this.elecMeterHList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaCode: null,
        deviceCode: null,
        recordTime: null,
        date: null,
        time: null,
        timeIndex: null,
        elecQuantity: null,
        meterType: null,
        meterUnitPrice: null,
        useElecCost: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRecordTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用电计量-小时";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateElecMeterH(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElecMeterH(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ems/elecMeterH/export', {
        ...this.queryParams
      }, `elecMeterH_${new Date().getTime()}.xlsx`)
    },
    getElecMeterType(meterType) {
      const meterTypeMap = {
        '-1': '低谷电',
        '0': '平峰电',
        '1': '高峰电',
        '2': '尖峰电'
      };
      return meterTypeMap[meterType] || '未知';
    },
  }
};
</script>
