<template>
    <div :class="isSubmit ? 'common-btn-box2 font12' : 'common-btn-box font12'" @click="handleBtnClick()">
        <span>提交</span>
    </div>
</template>

<script setup lang="ts">
import { commitListApi } from "@/api/modules/global";
import { cloneDeep } from "lodash-es";
import { useMsg } from "@/hooks/useMsg";
import { useRefresh } from "@/stores/modules/refresh";
const refreshStore = useRefresh();
const props = defineProps<{
    selectionList?: any[];
    handleChildBtnCallback?: () => void;
    ruleForm: any;
    isSubmit: any;
    isAdd: any;
}>();
const $router = useRouter();
const $route = useRoute();
// 接口地址映射
const URLS: Record<string, string> = {
    subscriptionDetails: "SubscribeNotification/Save"
};
const URLS1: Record<string, string> = {
    subscriptionDetails: "SubscribeNotification/Edit"
};

const emits = defineEmits<{
    (e: "handleChildBtnCallback", result: Record<string, any>): void;
}>();

const IsEmail = (str: any) => {
    let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    return reg.test(str);
};

//执行请求
const handleHttp = async () => {
    const routeName: string = $router.currentRoute.value.name as string;
    let params: any = cloneDeep(props.ruleForm);
    let telephones = [];
    let emails = [];
    if (params.telephones.indexOf(",") > -1) {
        telephones = params.telephones.split(",");
    } else {
        telephones.push(params.telephones);
    }
    if (params.emails.indexOf(",") > -1) {
        emails = params.emails.split(",");
    } else {
        emails.push(params.emails);
    }
    params.telephones = telephones;
    params.emails = emails;
    // params.telephones = params.telephones.indexOf(",");
    // params.emails = params.emails.split(",");

    let url = $route.query.type === "add" ? URLS[routeName] : URLS1[routeName];

    const result = await commitListApi(url, { ...params });
    if (result.status === 200) {
        useMsg("success", "操作成功 ！");
        refreshStore.setRefreshSubscription(true);
        emits("handleChildBtnCallback", {
            type: "Submit",
            data: result.data
        });
    } else {
        useMsg("error", result.message);
    }
};
// 数字验证
const isNumber = (value: any) => {
    return /^0|^\d+$/.test(value); // 判断是否为正整数|0
};

//验证
const formValidate = async () => {
    const MSGS: any = {
        customerName: "客户名称不能为空 ！",
        customerNumber: "客户编码不能为空 ！",
        telephones: "订阅手机号不能为空 ！"
    };
    // const MSGS_KEY: any = ["customerName", "customerNumber", "telephones"];
    if (!props.ruleForm.customerName) {
        useMsg("warning", MSGS["customerName"]);
        return;
    }
    if (!props.ruleForm.customerNumber) {
        useMsg("warning", MSGS["customerNumber"]);
        return;
    }
    if (!props.ruleForm.telephones) {
        useMsg("warning", MSGS["telephones"]);
        return;
    }
    if (props.ruleForm.telephones) {
        let phones = cloneDeep(props.ruleForm.telephones);
        if (phones.indexOf(",") > -1) {
            let phonesArray = phones.split(",");
            let length = phonesArray.length;
            for (let i = 0; i < length; i++) {
                if (!isNumber(Number(phonesArray[i])) || phonesArray[i].length !== 11) {
                    useMsg("warning", "请输入正确的订阅手机号码 ！");
                    return;
                }
            }
        } else {
            if (!isNumber(phones)) {
                useMsg("warning", "请输入正确的订阅手机号码 ！");
                return;
            }
        }
    }

    if (props.ruleForm.emails) {
        let emails = cloneDeep(props.ruleForm.emails);
        if (emails.indexOf(",") > -1) {
            let emailsArray = emails.split(",");
            let length = emailsArray.length;
            for (let i = 0; i < length; i++) {
                if (!IsEmail(emailsArray[i])) {
                    useMsg("warning", "请输入正确的订阅邮箱 ！");
                    return;
                }
            }
        } else {
            if (!IsEmail(emails)) {
                useMsg("warning", "请输入正确的订阅邮箱 ！");
                return;
            }
        }
    }
    await handleHttp();
};
// 提交事件
const handleBtnClick = async () => {
    if (props.isSubmit) {
        return;
    }
    refreshStore.setRefreshSubscription(false);
    await formValidate();
};
</script>

<style scoped>
@import "../index.scss";
</style>
