<script lang="ts" setup>
/**
 * @Name: neg-keyword.vue
 * @Description: 自动化-添加否定词
 * @Author: Cheney
 */
import { ref } from 'vue';
import SelectTmpl from './select-tmpl.vue';
import SearchTermAdd from '../search-term-add.vue';
import FreqSetting from '../freq-setting.vue';
import conditionBuilder from '/@/components/conditionBuilder/index.vue';
import XEUtils from 'xe-utils';
import { userFormData } from './common';

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

const submitForm = async () => {
  const valid2 = await searchTermAddRef.value.validate();
  const valid3 = !XEUtils.includes(await condiBuilderRef.value.validate(), false);
  formRef.value.validate(async (valid: boolean) => {
    if (valid && valid2 && valid3) {
      await submitFormData();
      emits('refresh');
    } else {
      console.log('验证失败：', [valid, valid2, valid3]);
    }
  });
};
const cancel = () => {
  emits('refresh');
};
</script>

<template>
  <div class="mx-5">
    <div class="asj-h2">添加否定词</div>
    <SelectTmpl :data="formData" />
    <el-card class="mt-3">
      <FreqSetting :rule="formData.rule" :disabled="formData.useTmpl" />
      <el-divider />
      <el-form :model="formData" label-position="top" ref="formRef" :disabled="formData.useTmpl">
        <SearchTermAdd :rule="formData.rule" ref="searchTermAddRef" :disabled="formData.useTmpl" />
        <el-form-item
          prop="rule.action.matchType"
          required
          :rules="{ required: true, message: '请至少选择一种匹配方式', trigger: 'change' }">
          <template #label>
            <span class="asj-h3">匹配方式</span>
          </template>
          <el-checkbox-group v-model="formData.rule.action.matchType">
            <el-checkbox label="EXECT">精准</el-checkbox>
            <el-checkbox label="PHRASE">词组</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="mt-3">

    <conditionBuilder :data="formData.rule.conditions" ref="condiBuilderRef" :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 scoped></style>
