var userName = '';
var phone = '';

async function clickHandler() {
    userName = document.getElementById('name').value;
    phone = document.getElementById('phone').value;
    if (userName == "" || phone == "") {
        alert('请先填写加盟人姓名和加盟人电话');
    } else {
        window.sessionStorage.setItem('name', userName);
        window.sessionStorage.setItem('phone', phone);
        await postData();
        //   发送成功后 跳转  // window.open('./end.html','_self');
    }
}

window.onload = function () {
    document.getElementById('btn').addEventListener('click', clickHandler);
};

async function postData() {
    const formData = {
        name: window.sessionStorage.getItem("name"),
        phone: window.sessionStorage.getItem("phone"),
        menDian: window.sessionStorage.getItem("menDian"),
        changDi: window.sessionStorage.getItem("changDi"),
        hangYe: window.sessionStorage.getItem("hangYe"),
        ziLi: window.sessionStorage.getItem("ziLi"),
        city: window.sessionStorage.getItem("city"),
        qu: window.sessionStorage.getItem("qu"),
    };
    // console.log('data: ', data);


    try {
        const responseStream = await fetch("/record", {
            body: JSON.stringify(formData),
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
        });
        const {data} = await responseStream.json();
        if (data) {
            // document.getElementById('btn').innerText = '已申请'
            // document.getElementById('btn').removeEventListener("click", clickHandler)
            // alert("提交成功");
            window.open('./end.html','_self');
        }
    } catch (e) {
        console.error("从服务器获取数据出错，请联系管理员！", e);
    }

}