<template>
  <el-form :model="form" ref="formRef" label-width="auto">
    <el-form-item v-if="getConfig('usePhoneConfig')">
      <template #label>
        <Tooltip
          v-if="getConfig('usePhoneConfig').remark"
          :message="getConfig('usePhoneConfig').remark"
        />
        <span>使用外呼</span>
      </template>
      <el-radio-group v-model="form.usePhoneConfig">
        <el-radio
          v-for="(item, index) in getConfig('usePhoneConfig').options"
          :key="index"
          :label="item.id"
        >
          {{ item.name }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item v-if="getConfig('showFollowConfig')">
      <template #label>
        <Tooltip
          v-if="getConfig('showFollowConfig').remark"
          :message="getConfig('showFollowConfig').remark"
        />
        <span>跟进信息</span>
      </template>
      <el-radio-group v-model="form.showFollowConfig">
        <el-radio
          v-for="(item, index) in getConfig('showFollowConfig').options"
          :key="index"
          :label="item.id"
        >
          {{ item.name }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button @click="getData">刷新</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup name="BasicSettingClue">
import * as ConfigApi from '@/api/system/set'

const message = useMessage()

const form = ref({})

const configList = ref([])

function getConfig(val) {
  return configList.value.find((it) => it.configKey == val)
}

function getData() {
  ConfigApi.getConfigList({ module: 100 }).then((data) => {
    configList.value = data
    // 获取所有配置项
    data.map((it) => {
      form.value[it.configKey] = it.configValue
    })
  })
}

function onSubmit() {
  const params = configList.value.map((it) => ({
    configId: it.configId,
    configKey: it.configKey,
    configValue: form.value[it.configKey]
  }))
  ConfigApi.updateConfig(params).then(() => {
    message.success('保存成功')
  })
}

onMounted(() => {
  getData()
})
</script>

<style lang="scss" scoped></style>
