<template>
  <div>
    <div class="asj-h2">分时调价</div>
    <SelectTmpl :data="formData" />
    <el-form :model="formData" label-position="top" style="margin-top: 20px" ref="formRef">
      <el-form-item prop="rule.conditions" :rules="[{ validator: checkConditions, trigger: 'xxx' }]">
        <template #label>
          <span class="asj-h3">设置竞价</span>
        </template>
        <TimerBidTable :data="formData.rule.conditions" @click="formRef.clearValidate('rule.conditions')" :disabled="formData.useTmpl" />
      </el-form-item>
    </el-form>
    <div class="auto-page-foot">
      <el-button style="width: 200px" @click="cancel">取消</el-button>
      <el-button style="width: 200px" type="primary" @click="submit">提交</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, Ref, watch } from 'vue'
import TimerBidTable from '/@/components/TimerBidTable/index.vue'
import SelectTmpl from './select-tmpl.vue'
import { userFormData } from './common'
import XEUtils from 'xe-utils'

interface Props {
  data: {
    campaignId: string
    campaignType: string
    profileId: string
    ruleType: number
  }
  RuleStatusButton?: { [key: string]: any }
}
const props = defineProps<Props>()
const emits = defineEmits(['refresh'])
const formRef = ref()
const { formData, submitFormData } = userFormData(props)

const checkConditions = (rule: any, value: any, callback: any) => {
  for (const bidList of formData.value.rule.conditions) {
    for (const val of bidList) {
      if (val > 0) {
        return callback()
      }
    }
  }
  callback(new Error('请先设置竞价！'))
}
const submit = async () => {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      await submitFormData()
      emits('refresh')
    } else {
      console.log('验证失败')
    }
  })
}

const cancel = async () => {
  emits('refresh')
}

</script>

<style scoped></style>
