<template>
  <div style="padding: 0.5%">
    <!-- <el-col :span="12">
           <el-button type="primary" @click="Add" >新增</el-button>
        </el-col> -->
    <el-col :span="24" style="margin-top: 0.5%">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column
          align="center"
          prop="num"
          label="序号"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="rateTypeName"
          label="费率类型"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="rateTypeTime"
          label="时间段(时)"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="rate"
          label="电费(元/kW/h)"
        ></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" type="primary" size="mini"
              >配置</el-button
            >
            <!-- <el-button type="primary" size="small">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <el-dialog
      :close-on-click-modal="false"
      title="配置"
      :visible.sync="dialogVisible"
      width="25%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="费率类型">
          <el-input v-model="form.rateTypeName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="时间段">
          <el-time-select
            style="width: 40%"
            placeholder="起始时间"
            v-model="startTime"
            :picker-options="{ start: '00:00', end: '24:00' }"
          ></el-time-select>
          <el-time-select
            style="width: 40%"
            placeholder="结束时间"
            v-model="endTime"
            :picker-options="{ start: '00:00', end: '24:00' }"
          ></el-time-select>
        </el-form-item>
        <el-form-item label="电费">
          <el-input v-model="form.rate" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submint">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import api from "@/api/energy/sysconfig";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        rateTypeId: "",
        rateTypeName: "",
        rateTypeTime: "",
        rate: "",
      },
      tableData: [],
      startTime: "",
      endTime: "",
    };
  },
  created() {},
  mounted() {
    this.queryData();
  },
  methods: {
    //查询获取列表详情
    queryData() {
      api.getEleRateDetail().then((res) => {
        if (res.code === "A0000") {
          var arr = [];
          var data = res.data;
          for (var i = 0; i < data.length; i++) {
            var map = {
              num: i + 1,
              rateTypeId: data[i].rateTypeId,
              rateTypeName: data[i].rateTypeName,
              rateTypeTime: data[i].rateTypeTime,
              rate: data[i].rate,
            };
            arr.push(map);
          }
          this.tableData = arr;
        }
      });
    },
    edit(row) {
      this.dialogVisible = true;
      this.form.rateTypeId = row.rateTypeId;
      this.form.rateTypeName = row.rateTypeName;
      var str = row.rateTypeTime;
      var str1 = str.split("-");
      this.startTime = str1[0];
      this.endTime = str1[1];
      this.form.rate = row.rate;
    },
    submint() {
      var data = {
        id: this.form.rateTypeId,
        rate: this.form.rate,
        time: this.startTime + "-" + this.endTime,
      };
      api.add(data).then((res) => {
        if (res.code == "A0000") {
          this.$message.success("配置成功");
          this.dialogVisible = false;
          this.queryData();
        }
      });
    },
  },
};
</script>