<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">

      <!-- ==================== CID配置 ==================== -->
      <el-tab-pane label="CID配置" name="cId.config">
        <el-form :model="form40" label-width="200px">
          <el-form-item label="是否开启手机号配置" prop="enablePhoneConfig">
            <el-switch v-model="form40.enablePhoneConfig"></el-switch>
          </el-form-item>
          <template v-if="form40.enablePhoneConfig">
            <el-form-item label="生成条数" prop="generateCount">
              <el-input-number v-model="form40.generateCount" :min="1" :step="1" :precision="0" placeholder="请输入生成条数"></el-input-number>
            </el-form-item>
            <el-form-item label="开始位置" prop="startIndex">
              <el-input-number v-model="form40.startIndex" :min="1" :step="1" :precision="0" placeholder="例如: 1"></el-input-number>
              <span class="tip-text">（从第几位开始生成）</span>
            </el-form-item>
            <el-form-item label="结束位置" prop="endIndex">
              <el-input-number v-model="form40.endIndex" :min="1" :step="1" :precision="0" placeholder="例如: 11"></el-input-number>
              <span class="tip-text">（到第几位结束）</span>
            </el-form-item>
          </template>
          <el-form-item label="是否开手机号拨打次数限制" prop="enablePhoneLimitConfig">
            <el-switch v-model="form40.enablePhoneLimitConfig"></el-switch>
          </el-form-item>
          <template v-if="form40.enablePhoneLimitConfig">
            <el-form-item label="拨打次数限制" prop="startIndex">
              <el-input-number v-model="form40.numberCalls" :min="1" :step="1" :precision="0" placeholder="例如: 1"></el-input-number>
            </el-form-item>
          </template>
          <el-form-item label="DeepSeekChat模型并发数" prop="DeepSeekChatConcurrency">
            <el-input v-model="form40.concurrency" placeholder="请输入DeepSeekChat模型并发数"></el-input>
          </el-form-item>
          <el-form-item label="DeepSeekChat服务地址" prop="DeepSeekChatServerAddress">
            <el-input v-model="form40.serverAddress" placeholder="请输入DeepSeekChat服务地址"></el-input>
          </el-form-item>
          <el-form-item label="DeepSeekChat_apiKey" prop="DeepSeekChatApiKey">
            <el-input v-model="form40.apiKey" placeholder="请输入DeepSeekChat_apiKey"></el-input>
          </el-form-item>
          <el-form-item label="DeepSeekChat模型名称" prop="DeepSeekChatModelName">
            <el-input v-model="form40.modelName" placeholder="请输入DeepSeekChat模型名称"></el-input>
          </el-form-item>
          <el-form-item label="是否限制外呼网关" prop="enableGateWayLimit">
            <el-switch v-model="form40.enableGateWayLimit"></el-switch>
          </el-form-item>
          <el-form-item label="系统可见外呼网关" prop="showGatewayIds" v-if="!!form40.enableGateWayLimit">
            <el-select v-model="form40.showGatewayIds" multiple filterable placeholder="请选择系统可见外呼网关">
              <el-option
                v-for="item in gatewayList"
                :key="item.id"
                :label="item.gwDesc"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <div class="footer">
            <el-button type="primary" @click="submitForm40">提 交</el-button>
          </div>
        </el-form>
      </el-tab-pane>

      <!-- ==================== 个微配置 ==================== -->
      <el-tab-pane label="个微配置" name="wx.config">
        <el-form :model="form30" label-width="200px">
          <el-form-item label="新账号判断时间" prop="newAccountTime">
            <el-input v-model="form30.newAccountTime" placeholder="请输入天" style="width: 150px">
              <template slot="append">天</template>
            </el-input>
          </el-form-item>
          <el-form-item label="新账号每天添加数量" prop="newAccountAddNum">
            <el-input v-model="form30.newAccountAddNum" placeholder="请输入个" style="width: 150px">
              <template slot="append">个</template>
            </el-input>
          </el-form-item>
          <el-form-item label="普通账号每天添加数量" prop="accountAddNum">
            <el-input v-model="form30.accountAddMin" placeholder="请输入个" style="width: 150px">
              <template slot="append">个</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.accountAddMax" placeholder="请输入个" style="width: 150px">
              <template slot="append">个</template>
            </el-input>
          </el-form-item>
          <el-form-item label="文字消息（条/分）" prop="txtMsg">
            <el-input v-model="form30.txtMsgMinNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.txtMsgMaxNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
          </el-form-item>
          <el-form-item label="图片消息（条/分）" prop="imgMsg">
            <el-input v-model="form30.imgMsgMinNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.imgMsgMaxNum" placeholder="请输入条" style="width: 150px">
              <template slot="append">条</template>
            </el-input>
          </el-form-item>
          <el-form-item label="新号添加好友频率(条/分)" prop="newAccountAddWx">
            <el-input v-model="form30.newAccountAddWxMin" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.newAccountAddWxMax" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
          </el-form-item>
          <el-form-item label="新号消息发送频率(条/秒)" prop="newAccountSendMsg">
            <el-input v-model="form30.newAccountSendMsgMin" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.newAccountSendMsgMax" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
          </el-form-item>
          <el-form-item label="添加好友频率(条/分)" prop="accountAddWx">
            <el-input v-model="form30.accountAddWxMin" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.accountAddWxMax" placeholder="请输入分" style="width: 150px">
              <template slot="append">分</template>
            </el-input>
          </el-form-item>
          <el-form-item label="消息发送频率(条/秒)" prop="accountSendMsg">
            <el-input v-model="form30.accountSendMsgMin" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
            <span> 到 </span>
            <el-input v-model="form30.accountSendMsgMax" placeholder="请输入秒" style="width: 150px">
              <template slot="append">秒</template>
            </el-input>
          </el-form-item>
          <div class="footer">
            <el-button type="primary" @click="submitForm30">提 交</el-button>
          </div>
        </el-form>
      </el-tab-pane>

    </el-tabs>
  </div>
</template>

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

export default {
  name: 'Config',
  data() {
    return {
      gatewayList: [],
      activeName: 'cId.config',
      configId: null,
      configKey: null,
      // 个微配置
      form30: {},
      // CID配置
      form40: {
        enablePhoneConfig: false,
        enablePhoneLimitConfig: false,
        generateCount: 1,
        startIndex: 1,
        endIndex: 11,
        numberCalls: 1,
      },
    }
  },
  created() {
    this.getConfigByKey(this.activeName)
  },
  methods: {
    handleClick(tab) {
      this.getConfigByKey(tab.name)
    },
    safeParseConfig(configValue, fallback = {}) {
      if (configValue === null || configValue === undefined || configValue === '' || configValue === 'null') {
        return fallback
      }
      try {
        const parsed = JSON.parse(configValue)
        if (parsed === null || parsed === undefined) {
          return fallback
        }
        return parsed
      } catch (e) {
        return fallback
      }
    },
    getConfigByKey(key) {
      getConfigByKey(key).then(response => {
        const configValue = response && response.data ? response.data.configValue : null
        if (!!response.data) {
          this.configId = response.data.configId
          this.configKey = response.data.configKey
          if (key == 'cId.config') {
            this.form40 = JSON.parse(response.data.configValue)
          }
        } else {
          this.configId = null
          this.configKey = key
          if (key == 'cId.config') {
            this.form40 = { enablePhoneConfig: false, generateCount: 1 }
          }
        }
        if (key == 'wx.config') {
          if (!!response.data) {
            this.configId = response.data.configId
            this.configKey = response.data.configKey
            this.form30 = { ...this.form30, ...this.safeParseConfig(configValue, {}) }
          }
        }
        if (key == 'cId.config' && !!this.form40.enableGateWayLimit) {
          getGatewayList().then(res => {
            this.gatewayList = res.data
          })
        }
      })
    },
    submitForm30() {
      const param = { configId: this.configId, configKey: this.configKey, configValue: JSON.stringify(this.form30) }
      updateConfigByKey(param).then(response => {
        if (response.code === 200) {
          this.msgSuccess('修改成功')
        }
      })
    },
    submitForm40() {
      if (this.form40.enablePhoneConfig) {
        if (!this.form40.generateCount || this.form40.generateCount < 1) {
          this.msgError('生成条数不能为空且不能小于1')
          return false
        }
        if (!this.form40.startIndex || this.form40.startIndex < 1 || this.form40.startIndex > 11) {
          this.msgError('开始位置不能为空，且必须在1到11之间')
          return false
        }
        if (!this.form40.endIndex || this.form40.endIndex < 1 || this.form40.endIndex > 11) {
          this.msgError('结束位置不能为空，且必须在1到11之间')
          return false
        }
        if (this.form40.startIndex > this.form40.endIndex) {
          this.msgError('开始位置不能大于结束位置')
          return false
        }
        if (this.form40.startIndex === this.form40.endIndex) {
          this.msgError('开始位置不能等于结束位置')
          return false
        }
        let num = this.form40.endIndex - this.form40.startIndex
        if (num < 4) {
          this.msgError('开始和结束位置不能小于4位')
          return false
        }
      }
      if (this.form40.enablePhoneLimitConfig) {
        if (this.form40.numberCalls == null || this.form40.numberCalls === 0) {
          this.msgError('限制次数不能为空或者大于0！')
          return false
        }
        if (this.form40.numberCalls > 100000) {
          this.msgError('限制次数不能超过10万次！')
          return false
        }
      }
      this.saveConfig40()
    },
    saveConfig40() {
      const param = {
        configId: this.configId,
        configKey: this.configKey,
        configValue: JSON.stringify(this.form40)
      }
      updateConfigByKey(param).then(response => {
        if (response.code === 200) {
          this.msgSuccess('修改成功')
        }
      })
    },
  }
}
</script>
<style scoped>
.footer {
  width: 100%;
  display: flex;
  margin-top: 50px;
  align-items: flex-end;
  justify-content: flex-end;
}
</style>
