<template>
  <ContentWrap>
    <el-form
      label-position="top"
      ref="formRef"
      v-loading="formLoading"
      status-icon
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="团队奖励" prop="teamReward">
        <el-input v-model="formData.teamReward" placeholder="" />
        <span style="color: gray">任务团队奖励 ， 用| 分割</span>
      </el-form-item>
      <el-form-item label="做任务最低累计金额" prop="taskMinimumAccumulatedAmount">
        <el-input v-model="formData.taskMinimumAccumulatedAmount" placeholder="" />
        <span style="color: gray">达到(累计充值+累计奖金总额)可开始做任务</span>
      </el-form-item>
      <el-form-item label="做任务最低剩余金额" prop="taskMinimumRemainingAmount">
        <el-input v-model="formData.taskMinimumRemainingAmount" placeholder="" />
        <span style="color: gray">做任务至少有的余额(余额+冻结金额)</span>
      </el-form-item>
      <el-form-item label="卡单默认差值" prop="defaultOrderDifference">
        <el-input v-model="formData.defaultOrderDifference" placeholder="" />
      </el-form-item>
      <el-form-item label="任务余额范围最小值" prop="taskBalanceRangeMin">
        <el-input v-model="formData.taskBalanceRangeMin" placeholder="" />
        <span style="color: gray">余额的%。 30%填0.3</span>
      </el-form-item>
      <el-form-item label="任务余额范围最大值" prop="taskBalanceRangeMax">
        <el-input v-model="formData.taskBalanceRangeMax" placeholder="" />
        <span style="color: gray">余额的%。 80%填0.8，不能大于1</span>
      </el-form-item>
      <el-form-item label="完成任务次日是否重置订单" prop="resetOrderNextDayAfterCompletion">
        <el-radio-group v-model="formData.resetOrderNextDayAfterCompletion" class="ml-4">
          <el-radio label="1" size="large">是</el-radio>
          <el-radio label="0" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="负数是否可获得奖金" prop="negativeValueEligibleForReward">
        <el-row>
          <el-radio-group v-model="formData.negativeValueEligibleForReward" class="ml-4">
            <el-radio label="1" size="large">是</el-radio>
            <el-radio label="0" size="large">否</el-radio>
          </el-radio-group>
        </el-row>
      </el-form-item>
      <el-form-item label="连单是否算1单" prop="consecutiveOrdersCountAsOne">
        <el-row>
          <el-col :span="24">
            <el-radio-group v-model="formData.consecutiveOrdersCountAsOne" class="ml-4">
              <el-radio label="1" size="large">是</el-radio>
              <el-radio label="0" size="large">否</el-radio>
            </el-radio-group>
          </el-col>
          <el-col :span="24">
            <span style="color: gray">客户余额负数时，是否获得奖金并且进入冻结金额</span>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="任务开始时间" prop="taskStartTime">
        <el-time-picker v-model="formData.taskStartTime" />
      </el-form-item>
      <el-form-item label="任务结束时间" prop="taskEndTime">
        <el-time-picker v-model="formData.taskEndTime" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import * as ConfigApi from '@/api/infra/config'
import { ElMessage } from 'element-plus'
defineOptions({ name: 'TaskConfig' })

const formLoading = ref(false)
const formData = ref({
  teamReward: '',
  taskMinimumAccumulatedAmount: '',
  taskMinimumRemainingAmount: '0',
  defaultOrderDifference: '',
  taskBalanceRangeMin: '',
  taskBalanceRangeMax: '',
  resetOrderNextDayAfterCompletion: '',
  negativeValueEligibleForReward: '',
  consecutiveOrdersCountAsOne: '',
  taskStartTime: '',
  taskEndTime: ''
})
const formRules = reactive({
  timezone: [{ required: true, message: '系统时区不能为空', trigger: 'blur' }],
  serviceLink: [{ required: true, message: '客服链接', trigger: 'blur' }],
  serviceOnlineTime: [{ required: true, message: '客服在线时间不能为空', trigger: 'blur' }]
})

const getList = async () => {
  const params = {
    category: 'task',
    pageSize: 50
  }
  const res = await ConfigApi.getConfigPage(params)
  if (res.code === 200) {
    ElMessage.success('更新成功')
  }
  const list = res.list
  list.forEach((item) => {
    if (item.key === 'teamReward') {
      formData.value.teamReward = item.value
    }
    if (item.key === 'taskMinimumAccumulatedAmount') {
      formData.value.taskMinimumAccumulatedAmount = item.value
    }
    if (item.key === 'taskMinimumRemainingAmount') {
      formData.value.taskMinimumRemainingAmount = item.value
    }
    if (item.key === 'defaultOrderDifference') {
      formData.value.defaultOrderDifference = item.value
    }
    if (item.key === 'taskBalanceRangeMin') {
      formData.value.taskBalanceRangeMin = item.value
    }
    if (item.key === 'taskBalanceRangeMax') {
      formData.value.taskBalanceRangeMax = item.value
    }
    if (item.key === 'resetOrderNextDayAfterCompletion') {
      formData.value.resetOrderNextDayAfterCompletion = item.value
    }
    if (item.key === 'negativeValueEligibleForReward') {
      formData.value.negativeValueEligibleForReward = item.value
    }
    if (item.key === 'consecutiveOrdersCountAsOne') {
      formData.value.consecutiveOrdersCountAsOne = item.value
    }
    if (item.key === 'taskStartTime') {
      formData.value.taskStartTime = item.value
    }
    if (item.key === 'taskEndTime') {
      formData.value.taskEndTime = item.value
    }
  })
}

const onSubmit = async () => {
  formLoading.value = true
  let list = [
    {
      key: 'teamReward',
      value: formData.value.teamReward,
      name: '团队奖励'
    },
    {
      key: 'taskMinimumAccumulatedAmount',
      value: formData.value.taskMinimumAccumulatedAmount,
      name: '做任务最低累计金额'
    },
    {
      key: 'taskMinimumRemainingAmount',
      value: formData.value.taskMinimumRemainingAmount,
      name: '做任务最低剩余金额'
    },
    {
      key: 'defaultOrderDifference',
      value: formData.value.defaultOrderDifference,
      name: '卡单默认差值'
    },
    {
      key: 'taskBalanceRangeMin',
      value: formData.value.taskBalanceRangeMin,
      name: '任务余额范围最小值'
    },
    {
      key: 'taskBalanceRangeMax',
      value: formData.value.taskBalanceRangeMax,
      name: '任务余额范围最大值'
    },
    {
      key: 'resetOrderNextDayAfterCompletion',
      value: formData.value.resetOrderNextDayAfterCompletion,
      name: '完成任务次日是否重置订单'
    },
    {
      key: 'negativeValueEligibleForReward',
      value: formData.value.negativeValueEligibleForReward,
      name: '负数是否可获得奖金'
    },
    {
      key: 'consecutiveOrdersCountAsOne',
      value: formData.value.consecutiveOrdersCountAsOne,
      name: '连单是否算1单'
    },
    {
      key: 'taskStartTime',
      value: formData.value.taskStartTime,
      name: '任务开始时间'
    },
    {
      key: 'taskEndTime',
      value: formData.value.taskEndTime,
      name: '任务结束时间'
    }
  ]
  try {
    const res = await ConfigApi.batchCreateTaskConfig({ list: list })
  } finally {
    formLoading.value = false
  }
}

// 初始化
onMounted(() => {
  getList()
})
</script>
