<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never">
      <div slot="header">
        <span>个微配置</span>
      </div>
      <el-form ref="form" :model="form" label-width="220px">
        <el-card shadow="never" class="section-card">
          <div slot="header"><span>账号配置</span></div>
          <el-form-item label="新账号判断时间" prop="newAccountTime">
            <el-input v-model="form.newAccountTime" placeholder="请输入天" style="width: 150px">
              <template slot="append">天</template>
            </el-input>
          </el-form-item>
          <el-form-item label="新账号每天添加数量" prop="newAccountAddNum">
            <el-input v-model="form.newAccountAddNum" placeholder="请输入个" style="width: 150px">
              <template slot="append">个</template>
            </el-input>
          </el-form-item>
          <el-form-item label="普通账号每天添加数量" prop="accountAddNum">
            <el-input v-model="form.accountAddMin" placeholder="请输入个" style="width: 150px">
              <template slot="append">个</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.accountAddMax" placeholder="请输入个" style="width: 150px">
              <template slot="append">个</template>
            </el-input>
          </el-form-item>
        </el-card>

        <el-card shadow="never" class="section-card">
          <div slot="header"><span>消息频率配置</span></div>
          <el-form-item label="文字消息（条/分）" prop="txtMsg">
            <el-input v-model="form.txtMsgMinNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.txtMsgMaxNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
          </el-form-item>
          <el-form-item label="图片消息（条/分）" prop="imgMsg">
            <el-input v-model="form.imgMsgMinNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.imgMsgMaxNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
          </el-form-item>
        </el-card>

        <el-card shadow="never" class="section-card">
          <div slot="header"><span>新号频率配置</span></div>
          <el-form-item label="新号添加好友频率(条/分)" prop="newAccountAddWx">
            <el-input v-model="form.newAccountAddWxMin" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.newAccountAddWxMax" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
          </el-form-item>
          <el-form-item label="新号消息发送频率(条/秒)" prop="newAccountSendMsg">
            <el-input v-model="form.newAccountSendMsgMin" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.newAccountSendMsgMax" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
          </el-form-item>
        </el-card>

        <el-card shadow="never" class="section-card">
          <div slot="header"><span>普通号频率配置</span></div>
          <el-form-item label="添加好友频率(条/分)" prop="accountAddWx">
            <el-input v-model="form.accountAddWxMin" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.accountAddWxMax" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
          </el-form-item>
          <el-form-item label="消息发送频率(条/秒)" prop="accountSendMsg">
            <el-input v-model="form.accountSendMsgMin" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
            <span class="range-separator"> 到 </span>
            <el-input v-model="form.accountSendMsgMax" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
          </el-form-item>
        </el-card>

      </el-form>
      <div style="text-align: center; margin-top: 20px; padding-bottom: 20px;">
        <el-button type="primary" size="medium" @click="submitForm" :loading="submitLoading">保存配置</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getConfigByKey, updateConfigByKey } from '@/api/system/config'

export default {
  name: 'AdminWxConfig',
  data() {
    return {
      loading: false,
      submitLoading: false,
      configId: null,
      configKey: 'wx.config',
      form: {
        newAccountTime: '',
        newAccountAddNum: '',
        accountAddMin: '',
        accountAddMax: '',
        txtMsgMinNum: '',
        txtMsgMaxNum: '',
        imgMsgMinNum: '',
        imgMsgMaxNum: '',
        newAccountAddWxMin: '',
        newAccountAddWxMax: '',
        newAccountSendMsgMin: '',
        newAccountSendMsgMax: '',
        accountAddWxMin: '',
        accountAddWxMax: '',
        accountSendMsgMin: '',
        accountSendMsgMax: ''
      }
    }
  },
  created() {
    this.loadConfig()
  },
  methods: {
    loadConfig() {
      this.loading = true
      getConfigByKey(this.configKey).then(response => {
        if (response.data) {
          this.configId = response.data.configId
          try {
            const parsed = JSON.parse(response.data.configValue)
            this.form = { ...this.form, ...parsed }
          } catch (e) {
            // 使用默认值
          }
        } else {
          this.configId = null
        }
      }).finally(() => {
        this.loading = false
      })
    },
    submitForm() {
      this.submitLoading = true
      const param = {
        configId: this.configId,
        configKey: this.configKey,
        configValue: JSON.stringify(this.form)
      }
      updateConfigByKey(param).then(response => {
        if (response.code === 200) {
          this.msgSuccess('修改成功')
        }
      }).finally(() => {
        this.submitLoading = false
      })
    }
  }
}
</script>

<style scoped>
.section-card {
  margin-bottom: 20px;
}
.range-separator {
  margin: 0 8px;
  color: #606266;
}

</style>
