<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';
import SaveRuleDialog from '/@/views/components/auto/auto-campaigns/save-rule-dialog.vue';
import { ElMessage } from 'element-plus';


interface Props {
  data: {
    campaignId: string;
    campaignType: string;
    profileId: string;
    ruleType: number;
  };
  RuleStatusButton?: { [key: string]: any };
}

const props = defineProps<Props>();
const emits = defineEmits(['refresh', 'updateRuleStatusButton']);

const formRef = ref();
const searchTermAddRef = ref();
const condiBuilderRef = ref();
const { formData, submitFormData } = userFormData(props);
const submitDialogVisible = ref(false);

const submitForm = async () => {
  await submitFormData();
  ElMessage.success('保存成功');
  emits('refresh');
};

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

async function handleSubmit() {
  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) return;
    if (formData.value.useTmpl) {
      await submitForm();
    } else {
      submitDialogVisible.value = true;
    }
  });
}

watch(() => formData.value, (newData) => {
  emits('updateRuleStatusButton', newData.RuleStatusButton);
});
</script>

<template>
  <div class="mx-5">
    <div class="asj-h2">添加否定词</div>
    <SelectTmpl :data="formData" />
    <el-card class="mt-3">
      <FreqSetting :disabled="formData.useTmpl" :rule="formData.rule" />
      <el-divider />
      <el-form ref="formRef" :disabled="formData.useTmpl" :model="formData" label-position="top">
        <SearchTermAdd ref="searchTermAddRef" :disabled="formData.useTmpl" :rule="formData.rule" />
        <el-form-item
            :rules="{ required: true, message: '请至少选择一种匹配方式', trigger: 'change' }"
            prop="rule.action.matchType"
            required>
          <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 ref="condiBuilderRef" :data="formData.rule.conditions" :disabled="formData.useTmpl" />
    </el-card>
    <SaveRuleDialog
        v-model="submitDialogVisible"
        :formData="formData"
        :formRef="formRef"
        @submit="submitForm"
    />
    <div class="auto-page-foot">
      <el-button style="width: 200px" @click="cancel">取消</el-button>
      <el-button style="width: 200px" type="primary" @click="handleSubmit">提交</el-button>
    </div>
  </div>
</template>

<style scoped></style>
