<template>
  <div>
    <div class="asj-h2">定向规则</div>
    <el-form :model="formData" label-position="top" style="margin-top: 20px" ref="formRef">
      <el-form-item prop="name" :rules="{ required: true, message: '必填项', trigger: 'blur' }">
        <template #label>
          <span class="asj-h3">模板名称</span>
        </template>
        <el-input v-model="formData.name" style="width: 30%"></el-input>
      </el-form-item>
      <el-form-item>
        <template #label>
          <span class="asj-h3">生效对象</span>
        </template>
        <TargetSelect :mode="mode" :data="formData.rule"></TargetSelect>
      </el-form-item>
    </el-form>

    <TargetRuleSetting :rule="formData.rule" ref="ruleSettingRef" />
    <FreqSetting :rule="formData.rule" />

    <div class="auto-page-foot">
      <el-button style="width: 200px" @click="cancel">取消</el-button>
      <el-button style="width: 200px" type="primary" @click="submitForm">提交</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue'
import TargetSelect from '../target-select.vue'
import TargetRuleSetting from '../target-rule-setting.vue'
import FreqSetting from '../freq-setting.vue'
import XEUtils from 'xe-utils'

interface Props {
  mode: string
  data: AutoTemplate
  submitFormData: Function
}
const props = defineProps<Props>()
const emits = defineEmits(['refresh'])
const formRef = ref()
const formData = ref(props.data)
const ruleSettingRef = ref()

const submitForm = async () => {
  const valid2 = await ruleSettingRef.value.validateForm()
  formRef.value.validate(async (valid: boolean) => {
    if (valid && valid2) {
      await props.submitFormData()
      emits('refresh')
    } else {
      console.log('验证失败：', [valid, valid2])
    }
  })
}
const cancel = () => {
  emits('refresh')
}
</script>

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