<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <template #detail>
        <a-form ref="formRef">
          <a-row>
            <a-col :span="24">
              <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" :rules="[formRules.email]">
                <a-input type="text" v-model:value="modelRef.email" placeholder="请输入邮箱" />
              </a-form-item>
              <a-form-item label="黑白名单" :labelCol="labelCol" :wrapperCol="wrapperCol" :rules="[formRules.blackOrWhite]">
                <a-select placeholder="请选择黑白名单" v-model:value="modelRef.blackOrWhite">
                  <a-select-option :value="0">黑名单</a-select-option>
                  <a-select-option :value="1">白名单</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
              <a-button @click="submitForm">提 交</a-button>
            </a-col>
          </a-row>
        </a-form>
      </template>
    </j-form-container>
  </a-spin>
</template>

<script lang="ts" name="EnquiryPublicBlackEmailForm" setup>
  import { getAction, httpAction } from '/@/api/manage/manage';
  import pick from 'lodash.pick';

  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  import { computed, onBeforeMount, reactive, ref, toRaw } from 'vue';
  import { Form } from 'ant-design-vue';
  import { useMessage } from '@/hooks/web/useMessage';

  const emit = defineEmits(['ok', 'close']);

  const { createMessage, createConfirm } = useMessage();

  const useForm = Form.useForm;

  const props = defineProps({
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  });

  let modelRef = reactive({ id: '', email: '', blackOrWhite: null });

  const labelCol = reactive({
    xs: { span: 24 },
    sm: { span: 5 },
  });

  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 16 },
  });

  const confirmLoading = ref(false);

  const url = reactive({
    add: '/adweb/enquiryPublicBlackEmail/add',
    edit: '/adweb/enquiryPublicBlackEmail/edit',
    queryById: '/adweb/enquiryPublicBlackEmail/queryById',
  });

  const formRules = reactive({
    email: [{ required: true, message: '请输入邮箱' }],
    blackOrWhite: [{ required: true, message: '请输入黑白名单', trigger: 'blur' }],
  });

  const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, formRules);

  // 定义一个计算属性
  const formDisabled = computed(() => {
    if (props.formBpm === true) {
      return props.formData.disabled !== false;
    }
    return props.disabled;
  });

  const showFlowSubmitButton = computed(() => {
    if (props.formBpm === true) {
      if (props.formData.disabled === false) {
        return true;
      }
    }
    return false;
  });

  onBeforeMount(() => {
    showFlowData();
  });

  function add() {
    edit({});
  }

  async function edit(record) {
    if (Object.keys(record).length === 0) {
      modelRef.id = '';
      modelRef.email = '';
      modelRef.blackOrWhite = null;
    } else {
      Object.assign(modelRef, pick(record, 'id', 'email', 'blackOrWhite'));
    }
  }

  function showFlowData() {
    if (props.formBpm === true) {
      let params = { id: props.formData.dataId };
      getAction(url.queryById, params).then((res) => {
        if (res.code === 200) {
          edit(res.result);
        }
      });
    }
  }

  const onSubmit = () => {
    validate()
      .then(() => {
        confirmLoading.value = true;

        let httpUrl = '';
        let method = '';

        if (!modelRef.id) {
          httpUrl = url.add;
          method = 'post';
        } else {
          httpUrl = url.edit;
          method = 'put';
        }
        let formData = toRaw(modelRef);
        console.log('表单提交数据', formData);

        httpAction(httpUrl, formData, method)
          .then((res) => {
            if (res.success) {
              createMessage.success(res.message);
              emit('ok');
            } else {
              createMessage.warning(res.message);
            }
          })
          .finally(() => {
            confirmLoading.value = false;
          });
      })
      .catch((err) => {
        console.log('error', err);
      });
  };

  function submitForm() {
    onSubmit();
  }

  defineExpose({
    submitForm,
    add,
    edit,
  });
</script>
