<template>
  <div class="ems-dashboard-editor-container" v-loading="loading">
    <el-card shadow="always" class="common-card-container">
      <div slot="header" class="clearfix">
        <span class="card-title">运行参数配置</span>
        <span class="site-tag">站点：{{ siteId || '-' }}</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px" style="max-width: 760px;">
        <el-form-item label="SOC下限(%)" prop="socDown">
          <el-input-number v-model="form.socDown" :min="0" :max="100" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="SOC上限(%)" prop="socUp">
          <el-input-number v-model="form.socUp" :min="0" :max="100" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="防逆流阈值(kW)" prop="antiReverseThreshold">
          <el-input-number v-model="form.antiReverseThreshold" :min="0" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="防逆流阈值上浮比例(%)" prop="antiReverseRangePercent">
          <el-input-number v-model="form.antiReverseRangePercent" :min="0" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="防逆流恢复上限(kW)" prop="antiReverseUp">
          <el-input-number v-model="form.antiReverseUp" :min="0" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="防逆流降功率比例(%)" prop="antiReversePowerDownPercent">
          <el-input-number v-model="form.antiReversePowerDownPercent" :min="0" :max="100" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item label="防逆流硬停阈值(kW)" prop="antiReverseHardStopThreshold">
          <el-input-number v-model="form.antiReverseHardStopThreshold" :min="0" :step="1" :precision="2" controls-position="right" style="width: 220px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="saveLoading" @click="handleSave">保存</el-button>
          <el-button @click="init">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import getQuerySiteId from '@/mixins/ems/getQuerySiteId'
import { getStrategyRuntimeConfig, saveStrategyRuntimeConfig } from '@/api/ems/dzjk'

const emptyForm = () => ({
  siteId: '',
  socDown: 0,
  socUp: 100,
  antiReverseThreshold: 30,
  antiReverseRangePercent: 20,
  antiReverseUp: 100,
  antiReversePowerDownPercent: 10,
  antiReverseHardStopThreshold: 20
})

export default {
  name: 'DzjkClpzRuntimeParam',
  mixins: [getQuerySiteId],
  data() {
    return {
      loading: false,
      saveLoading: false,
      form: emptyForm(),
      rules: {
        socDown: [
          { required: true, message: '请输入SOC下限', trigger: 'change' }
        ],
        socUp: [
          { required: true, message: '请输入SOC上限', trigger: 'change' }
        ],
        antiReverseThreshold: [
          { required: true, message: '请输入防逆流阈值', trigger: 'change' }
        ],
        antiReverseRangePercent: [
          { required: true, message: '请输入防逆流阈值上浮比例', trigger: 'change' }
        ],
        antiReverseUp: [
          { required: true, message: '请输入防逆流恢复上限', trigger: 'change' }
        ],
        antiReversePowerDownPercent: [
          { required: true, message: '请输入防逆流降功率比例', trigger: 'change' }
        ],
        antiReverseHardStopThreshold: [
          { required: true, message: '请输入防逆流硬停阈值', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init() {
      if (!this.siteId) {
        this.form = emptyForm()
        return
      }
      this.loading = true
      getStrategyRuntimeConfig(this.siteId).then(response => {
        const data = response?.data || {}
        this.form = {
          ...emptyForm(),
          ...data,
          siteId: this.siteId
        }
      }).finally(() => {
        this.loading = false
      })
    },
    handleSave() {
      if (!this.siteId) {
        this.$message.error('缺少站点ID')
        return
      }
      this.$refs.form.validate(valid => {
        if (!valid) return
        if (Number(this.form.socDown) > Number(this.form.socUp)) {
          this.$message.error('SOC下限不能大于SOC上限')
          return
        }
        this.saveLoading = true
        saveStrategyRuntimeConfig({ ...this.form, siteId: this.siteId }).then(response => {
          if (response?.code === 200) {
            this.$message.success('保存成功')
            this.init()
          }
        }).finally(() => {
          this.saveLoading = false
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.site-tag {
  float: right;
  color: #909399;
  font-size: 13px;
}
</style>
