<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">国网电价</el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleValencyAdd"
          v-hasPermi="['basecfg:electricity:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getValencyList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="valencyList">
      <el-table-column label="用电分类" align="center" prop="elecTypeName" />
      <el-table-column label="电压等级" align="center" prop="voltageLevel" />
      <el-table-column label="电度用电价格" align="center" prop="degreePrice" />
      <el-table-column label="分时电度用电价格" align="center">
        <el-table-column label="尖峰时段" align="center" prop="fsPeakDegreePrice" />
        <el-table-column label="高峰时段" align="center" prop="fsHighDegreePrice" />
        <el-table-column label="平峰时段" align="center" prop="fsFlatDegreePrice" />
        <el-table-column label="低谷时段" align="center" prop="fsLowDegreePrice" />
      </el-table-column>
      <el-table-column label="容(需)量用电价格" align="center">
        <el-table-column label="最大需量 (元/千瓦时·月)"  align="center" prop="maxReqPrice" width="80px"/>
        <el-table-column label="变压器容量（元/千伏安·月）" align="center" prop="transCapacityPrice" width="80px"/>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleValencyUpdate(scope.row)"
            v-hasPermi="['basecfg:electricity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleValencyDelete(scope.row)"
            v-hasPermi="['basecfg:electricity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改电价配置对话框 -->
    <el-dialog :title="title" :visible.sync="valencyOpen" width="500px" append-to-body>
      <el-form ref="valencyForm" :model="valencyFrom" :rules="valencyRules" label-width="150px">
        <el-form-item label="配置代码" prop="cfgCode">
          <el-input v-model="valencyFrom.cfgCode" maxlength="4" placeholder="请输入配置代码" />
        </el-form-item>
        <el-form-item label="用电分类" prop="elecType">
          <el-select v-model="valencyFrom.elecType">
            <el-option v-for="item in valencyTypeOptions" :label="item.name" :value="item.code" :key="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="电压等级" prop="voltageLevel">
          <el-input v-model="valencyFrom.voltageLevel" placeholder="请输入电压等级" />
        </el-form-item>
        <el-form-item label="电度用电价格" prop="degreePrice">
          <el-input v-model="valencyFrom.degreePrice" placeholder="请输入电度用电价格" />
        </el-form-item>
        <el-form-item label="尖峰时段价格" prop="fsPeakDegreePrice">
          <el-input v-model="valencyFrom.fsPeakDegreePrice" placeholder="请输入分时尖峰时段价格" />
        </el-form-item>
        <el-form-item label="高峰时段价格" prop="fsHighDegreePrice">
          <el-input v-model="valencyFrom.fsHighDegreePrice" placeholder="请输入分时高峰时段价格" />
        </el-form-item>
        <el-form-item label="平峰时段价格" prop="fsFlatDegreePrice">
          <el-input v-model="valencyFrom.fsFlatDegreePrice" placeholder="请输入分时平峰时段价格" />
        </el-form-item>
        <el-form-item label="低谷时段价格" prop="fsLowDegreePrice">
          <el-input v-model="valencyFrom.fsLowDegreePrice" placeholder="请输入分时低谷时段价格" />
        </el-form-item>
        <el-form-item label="最大需量用电价格" prop="maxReqPrice" v-if="valencyFrom.elecType === 4">
          <el-input v-model="valencyFrom.maxReqPrice" placeholder="请输入最大需量用电价格" />
        </el-form-item>
        <el-form-item label="变压器容量用电价格" prop="transCapacityPrice" v-if="valencyFrom.elecType === 4">
          <el-input v-model="valencyFrom.transCapacityPrice" placeholder="请输入变压器容量用电价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitValencyForm">确 定</el-button>
        <el-button @click="valencyCancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">价格策略</el-col>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAttrAdd"
          v-hasPermi="['basecfg:electricity:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getAttrList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attrList" >
      <el-table-column label="服务区" align="center" prop="areaName" />
      <el-table-column label="用电分类" align="center" prop="elecTypeName" />
      <el-table-column label="电压等级" align="center" prop="voltageLevel" />
      <el-table-column label="容(需）量类型" align="center" prop="reqCapacityFlag" :formatter="matchReqCapacityFlag" />
      <el-table-column label="变压器容量（千伏·安）" align="center" prop="transCapacity" />
      <el-table-column label="最大需量（千瓦）" align="center" prop="reqQuantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAttrUpdate(scope.row)"
            v-hasPermi="['basecfg:electricity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleAttrDelete(scope.row)"
            v-hasPermi="['basecfg:electricity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getAttrList"
    />
    <!-- 添加或修改服务区用电属性对话框 -->
    <el-dialog :title="title" :visible.sync="attrOpen" width="500px" append-to-body>
      <el-form ref="attrForm" :model="attrForm" :rules="attrRules" label-width="150px">
        <el-form-item label="园区" prop="areaCode">
          <el-select v-model="attrForm.areaCode">
            <el-option v-for="item in areaOptions" :label="item.label" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="价格策略" prop="priceCode" >
          <el-select v-model="attrForm.priceCode">
            <el-option v-for="item in valencyOptions" :label="`${item.elecTypeName}&lt;${item.voltageLevel}&gt;`" :value="item.cfgCode" :key="item.cfgCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="容(需）量类型" prop="reqCapacityFlag">
          <el-select v-model="attrForm.reqCapacityFlag">
            <el-option v-for="item in reqCapacityOptions" :label="item.name" :value="item.code" :key="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="变压器容量(kVA/月)" prop="transCapacity" v-if="attrForm.reqCapacityFlag === 1">
          <el-input v-model="attrForm.transCapacity" placeholder="请输入变压器容量" />
        </el-form-item>
        <el-form-item label="最大需量(kW·h/月)" prop="reqQuantity" v-if="attrForm.reqCapacityFlag === 2">
          <el-input v-model="attrForm.reqQuantity" placeholder="请输入最大需量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAttrForm">确 定</el-button>
        <el-button @click="attrCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttr, getAttr, delAttr, addAttr, updateAttr } from "@/api/basecfg/electricity";
import { listValency, listValencyAll, getValency, addValency, delValency, updateValency } from '@/api/basecfg/elecvalency'
import { getElecValencyType } from '@/api/commonApi'
import { areaTreeSelect } from "@/api/basecfg/area"

export default {
  name: "Attr",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务区用电属性表格数据
      attrList: [],
      // 电价表格
      valencyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      attrOpen: false,
      valencyOpen: false,
      // 选项定义
      valencyTypeOptions: undefined,
      areaOptions: undefined,
      valencyOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaCode: null,
        elecType: null,
        priceCode: null,
        reqCapacityFlag: null,
        transCapacity: null,
        reqQuantity: null,
      },
      // 表单参数
      attrForm: {},
      valencyFrom:{},
      // 表单校验
      valencyRules: {
        cfgCode: [
          { required: true, message: "配置代码不为空", trigger: "blur" }
        ],
        elecType: [
          { required: true, message: "请选择用电分类", trigger: "change" }
        ],
        voltageLevel: [
          { required: true, message: "电压等级不为空", trigger: "blur" }
        ]
      },

      // 表单校验
      attrRules: {
        areaCode: [
          { required: true, message: "园区代码不能为空", trigger: "blur" }
        ],
        elecType: [
          { required: true, message: "请选择用电分类", trigger: "change" }
        ],
        priceCode: [
          { required: true, message: "价格编码不能为空", trigger: "blur" }
        ],
      },
      reqCapacityOptions: [
        { code: 0, name: "不涉及"},
        { code: 1, name: "容量电价"},
        { code: 2, name: "需量电价"}
      ]
    };
  },
  created() {
    this.getAttrList();
    this.getValencyList();
    this.getValencyType();
  },
  methods: {
    getValencyList() {
      this.loading = true;
      listValency(this.queryParams).then(response => {
        this.valencyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    valencyCancel() {
      this.valencyOpen = false;
      this.valencyReset();
    },
    // 表单重置
    valencyReset() {
      this.valencyFrom = {
        id: null,
        cfgCode: null,
        elecType: null,
        voltageLevel: null,
        degreePrice: null,
        fsPeakDegreePrice: null,
        fsHighDegreePrice: null,
        fsFlatDegreePrice: null,
        fsLowDegreePrice: null,
        maxReqPrice: null,
        transCapacityPrice: null
      };
      this.resetForm("valencyForm");
    },
    /** 新增按钮操作 */
    handleValencyAdd() {
      this.valencyReset();
      this.valencyOpen = true;
      this.title = "添加电价配置";
    },
    /** 修改按钮操作 */
    handleValencyUpdate(row) {
      this.valencyReset();
      const id = row.id || this.ids
      getValency(id).then(response => {
        this.valencyFrom = response.data;
        this.valencyOpen = true;
        this.title = "修改电价配置";
      });
    },
    /** 提交按钮 */
    submitValencyForm() {
      this.$refs["valencyForm"].validate(valid => {
        if (valid) {
          if (this.valencyFrom.id != null) {
            updateValency(this.valencyFrom).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.valencyOpen = false;
              this.getValencyList();
            });
          } else {
            addValency(this.valencyFrom).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.valencyOpen = false;
              this.getValencyList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleValencyDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除电价配置编号为"' + ids + '"的数据项？').then(function() {
        return delValency(ids);
      }).then(() => {
        this.getValencyList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查询服务区用电属性列表 */
    getAttrList() {
      this.loading = true;
      listAttr(this.queryParams).then(response => {
        this.attrList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    attrCancel() {
      this.attrOpen = false;
      this.attrReset();
    },
    // 表单重置
    attrReset() {
      this.attrForm = {
        id: null,
        areaCode: null,
        elecType: null,
        priceCode: null,
        reqCapacityFlag: 0,
        transCapacity: null,
        reqQuantity: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("attrForm");
    },
    /** 搜索按钮操作 */
    handleAttrQuery() {
      this.queryParams.pageNum = 1;
      this.getAttrList();
    },
    handleValencyQuery() {
      this.queryParams.pageNum = 1;
      this.getValencyList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleAttrQuery();
      this.handleValencyQuery();
    },
    /** 新增按钮操作 */
    handleAttrAdd() {
      this.attrReset();
      this.getAreaList("Area");
      this.getValencyAll();
      this.attrOpen = true;
      this.title = "添加服务区用电属性";
    },
    /** 修改按钮操作 */
    handleAttrUpdate(row) {
      this.attrReset();
      this.getAreaList("Area");
      this.getValencyAll();
      const id = row.id || this.ids
      getAttr(id).then(response => {
        this.attrForm = response.data;
        this.attrOpen = true;
        this.title = "修改服务区用电属性";
      });
    },
    /** 提交按钮 */
    submitAttrForm() {
      this.$refs["attrForm"].validate(valid => {
        if (valid) {
          if (this.attrForm.id != null) {
            updateAttr(this.attrForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.attrOpen = false;
              this.getAttrList();
            });
          } else {
            addAttr(this.attrForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.attrOpen = false;
              this.getAttrList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleAttrDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务区用电属性编号为"' + ids + '"的数据项？').then(function() {
        return delAttr(ids);
      }).then(() => {
        this.getAttrList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    matchReqCapacityFlag(row, column, cellValue, index) {
      const reqCapacityFlagMap = {
        0: '不涉及',
        1: '容量电价',
        2: '需量电价'
      };
      return reqCapacityFlagMap[cellValue] || '未知';
    },
    getValencyType() {
      getElecValencyType().then(response => {
        this.valencyTypeOptions = response.data;
      });
    },
    // 查询区域列表
    getAreaList(tier) {
      areaTreeSelect(tier).then(response => {
        this.areaOptions = response.data;
      });
    },
    getValencyAll() {
      listValencyAll().then(response =>{
        this.valencyOptions = response.data;
      })
    }
  }
};
</script>
