<script lang="ts" setup>
/**
 * @Name: target-rule
 * @Description: 自动化-定向规则
 * @Author: Cheney
 */

import { ref } from 'vue';
import TargetSelect from '../target-select.vue';
import SelectTmpl from './select-tmpl.vue';
import FreqSetting from '../freq-setting.vue';
import { userFormData } from './common';
import TargetRuleSetting from '../target-rule-setting.vue';

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 ruleSettingRef = ref();
const { formData, submitFormData } = userFormData(props);

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

function cancel() {
  emits('refresh');
}
</script>

<template>
  <div class="mx-5">
    <div class="asj-h2">定向规则</div>
    <SelectTmpl :data="formData" />
    <el-card class="mt-3">
      <el-form class="custom-card" :model="formData" label-position="top" ref="formRef">
        <el-form-item>
          <!--<template #label>
            <span class="custom-title-icon"></span>
            <span class="asj-h3">生效对象</span>
          </template>-->
          <FreqSetting :rule="formData.rule" :disabled="formData.useTmpl" />
          <el-divider />
          <TargetSelect
            mode="auto"
            :data="formData.rule"
            :useTmpl="formData.useTmpl"
            :campaign-id="data.campaignId"></TargetSelect>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt-3">
      <TargetRuleSetting :rule="formData.rule" ref="ruleSettingRef" :disabled="formData.useTmpl" />
    </el-card>

    <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>

<style lang="scss" scoped>
:deep(.custom-card .el-form-item__content) {
  display: block;
}
</style>
