<template>
  <el-form :model="form" ref="formRef" label-width="auto">
    <el-form-item v-if="getConfig('saleCommissionRelateDealConfig')">
      <template #label>
        <Tooltip
          v-if="getConfig('saleCommissionRelateDealConfig').remark"
          :message="getConfig('saleCommissionRelateDealConfig').remark"
        />
        <span>是否关联成交率</span>
      </template>
      <el-radio-group
        v-model="form.saleCommissionRelateDealConfig"
        @change="form.saleCommissionRelateRulesConfig = []"
      >
        <el-radio
          v-for="(item, index) in getConfig('saleCommissionRelateDealConfig').options"
          :key="index"
          :label="item.id"
        >
          {{ item.name }}
        </el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="关联规则" v-if="form.saleCommissionRelateDealConfig == 'true'">
      <div>
        <el-button @click="form.saleCommissionRelateRulesConfig.push({})">新增规则</el-button>
        <div
          v-for="(item, index) in form.saleCommissionRelateRulesConfig"
          :key="index"
          class="mt-10px flex justify-center items-center"
        >
          <span>成交率达</span>
          <el-input
            class="ml-10px"
            v-model="item.successRate"
            placeholder="成交率"
            style="width: 100px"
            type="number"
            :min="0"
          >
            <template #suffix>%</template>
          </el-input>
          <span>，可结算</span>
          <el-input
            class="ml-10px"
            v-model="item.comissionRate"
            placeholder="提成率"
            style="width: 100px"
            type="number"
            :min="0"
          >
            <template #suffix>%</template>
          </el-input>
          <span>提成</span>
        </div>
      </div>
    </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([])

const jsonArr = ['saleCommissionRelateRulesConfig']

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

function getData() {
  ConfigApi.getConfigList({ module: 101 }).then((data) => {
    configList.value = data
    // 获取所有配置项
    data.map((it) => {
      form.value[it.configKey] = it.configValue
      if (it.configKey == 'saleCommissionRelateRulesConfig') {
        if (!form.value[it.configKey]) {
          form.value[it.configKey] = []
        } else {
          form.value[it.configKey] = JSON.parse(it.configValue)
        }
      }
    })
  })
}

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

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

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