//调用服务器接口
function SaveProductsku(tab, requestBody, funCallback) {
    let token = '';

    function onTokenReady(value) {
        console.log('[SaveProductsku] token 获取结果:', value ? '成功' : '失败');
        if (value) {
            token = value;
        } else {
            funCallback({ "Type": "Alter", "MessageType": "error", "Message": "OZ助手登陆超时，请重新登录！" }, tab, function (response) { });
            funCallback({ "Type": "CheckCollectionSKUBtnDisabled", "Disabled": false }, tab, function (response) { });
        }

        request(config.url.saveProductsku(), {
            responseType: "json"
            , body: { 'skulist': requestBody, 'token': token }
            , method: "POST"
        }).then(res => {
            //console.log(res);
            if (res !== null) {
                if (res.IsSuccess === true) {
                    funCallback({ "Type": "Alter", "MessageType": "success", "Message": "产品采集成功" }, tab, function (response) { });
                } else {
                    var msg = res.Message == null ? (res.ResponseError == null ? "服务器未知异常，请联系管理员" : res.ResponseError.Message) : res.Message;

                    var actionUrl = res.Url && res.Url != null ? res.Url : '';
                    if (actionUrl != '' && actionUrl.indexOf('http') === -1)
                        actionUrl = config.url.domain + actionUrl;

                    var actionBtnText = '';
                    if (msg.indexOf('授权') > -1)
                        actionBtnText = '去授权';
                    else if (msg.indexOf('升级插件') > -1)
                        actionBtnText = '去升级';

                    funCallback({ "Type": "Alter", "MessageType": "error", "Message": "SKU采集失败！原因：" + msg, "Url": actionUrl, "ActionBtnText": actionBtnText }, tab, function (response) { });
                }
            } else {
                funCallback({ "Type": "Alter", "MessageType": "error", "Message": "发生未知异常！请稍后重试！若长时间发生此错误，请与客服联系！" }, tab, function (response) { });
            }
        }).then(data => {
            funCallback({ "Type": "CheckCollectionSKUBtnDisabled", "Disabled": false }, tab, function (response) { });
        }).catch(reason => {
            funCallback({ "Type": "Alter", "MessageType": "error", "Message": "服务器连接失败！请稍后重试！若长时间发生此错误，请与客服联系！" }, tab, function (response) { });
            funCallback({ "Type": "CheckCollectionSKUBtnDisabled", "Disabled": false }, tab, function (response) { });
        });
    }

    if (typeof readCookieWithFallback === 'function') {
        readCookieWithFallback(config.url.domainz(), config.cookieName11, onTokenReady);
    } else {
        config.getCookie({ "url": config.url.domainz(), "name": config.cookieName11 }, function (cookie) {
            onTokenReady(cookie && cookie.value ? cookie.value : null);
        });
    }
}
//调用服务器接口
function SaveProduct(tab, requestBody, funCallback) {
    let token = '';

    function onTokenReady(value) {
        if (value) {
            token = value;
        } else {
            funCallback({ "Type": "Alter", "MessageType": "error", "Message": "OZ助手登陆超时，请重新登录！" }, tab, function (response) { });
            funCallback({ "Type": "CheckCollectionGoodsBtnDisabled", "Disabled": false }, tab, function (response) { });
        }
        request(config.url.saveProduct(), {
            responseType: "json"
            , body: { 'proinfo': requestBody, 'token': token }
            , method: "POST"
        }).then(res => {
            if (res !== null) {
                if (res.IsSuccess === true) {
                    funCallback({ "Type": "Alter", "MessageType": "success", "Message": "产品采集成功" }, tab, function (response) { });
                } else {
                    var msg = res.Message == null ? (res.ResponseError == null ? "服务器未知异常，请联系管理员" : res.ResponseError.Message) : res.Message;

                    var actionUrl = res.Url && res.Url != null ? res.Url : '';
                    if (actionUrl != '' && actionUrl.indexOf('http') === -1)
                        actionUrl = config.url.domain + actionUrl;

                    var actionBtnText = '';
                    if (msg.indexOf('授权') > -1)
                        actionBtnText = '去授权';
                    else if (msg.indexOf('升级插件') > -1)
                        actionBtnText = '去升级';

                    funCallback({ "Type": "Alter", "MessageType": "error", "Message": "产品采集失败！原因：" + msg, "Url": actionUrl, "ActionBtnText": actionBtnText }, tab, function (response) { });
                }
            } else {
                funCallback({ "Type": "Alter", "MessageType": "error", "Message": "发生未知异常！请稍后重试！若长时间发生此错误，请与客服联系！" }, tab, function (response) { });
            }
        }).then(data => {
            funCallback({ "Type": "CheckCollectionGoodsBtnDisabled", "Disabled": false }, tab, function (response) { });
        }).catch(reason => {
            funCallback({ "Type": "Alter", "MessageType": "error", "Message": "服务器连接失败！请稍后重试！若长时间发生此错误，请与客服联系！" }, tab, function (response) { });
            funCallback({ "Type": "CheckCollectionGoodsBtnDisabled", "Disabled": false }, tab, function (response) { });
        });
    }

    if (typeof readCookieWithFallback === 'function') {
        readCookieWithFallback(config.url.domainz(), config.cookieName11, onTokenReady);
    } else {
        config.getCookie({ "url": config.url.domainz(), "name": config.cookieName11 }, function (cookie) {
            onTokenReady(cookie && cookie.value ? cookie.value : null);
        });
    }
}


function SaveLiknProduct(tab, data, funCallback) {
    request(config.url.saveCollectProduct(), {
        responseType: "json"
        , body: data
        , method: "POST"
    }).then(res => {
        if (res !== null) {
            if (res.IsSuccess === true) {
                funCallback({ "Type": "Alter", "MessageType": "success", "Message": "产品采集成功" }, tab, function (response) { });
            } else {
                let msg = res.Message == null ? (res.ResponseError == null ? "服务器未知异常，请联系管理员" : res.ResponseError.Message) : res.Message;

                let actionUrl = res.Url && true ? res.Url : '';
                if (actionUrl !== '' && actionUrl.indexOf('http') === -1)
                    actionUrl = config.url.domain() + actionUrl;

                let actionBtnText = '';
                if (msg.indexOf('授权') > -1)
                    actionBtnText = '去授权';
                else if (msg.indexOf('升级插件') > -1)
                    actionBtnText = '去升级';

                funCallback({ "Type": "Alter", "MessageType": "error", "Message": "产品采集失败！原因：" + msg, "Url": actionUrl, "ActionBtnText": actionBtnText }, tab, function (response) { });
            }
        } else {
            funCallback({ "Type": "Alter", "MessageType": "error", "Message": "发生未知异常！请稍后重试！若长时间发生此错误，请与客服联系！" }, tab, function (response) { });
        }
    }).then(data => {
        funCallback({ "Type": "CheckCollectionGoodsBtnDisabled", "Disabled": false }, tab, function (response) { });
    }).catch(reason => {
        funCallback({ "Type": "Alter", "MessageType": "error", "Message": "服务器连接失败！请稍后重试！若长时间发生此错误，请与客服联系！" }, tab, function (response) { });
        funCallback({ "Type": "CheckCollectionGoodsBtnDisabled", "Disabled": false }, tab, function (response) { });
    });
}

//单张图片上传
function UploadImage(imageUrl, callback) {
    let token = '';
    config.getCookie({ "url": config.url.domainz(), "name": config.cookieName11 }, function (cookie) {
        if (cookie && cookie !== null && cookie.value !== undefined && cookie.value !== '') {
            token = cookie.value;
        }
        else {
            funCallback({ "Type": "Alter", "MessageType": "error", "Message": "OZ助手登陆超时，请重新登录！" }, tab, function (response) { });
            funCallback({ "Type": "CheckCollectionSKUBtnDisabled", "Disabled": false }, tab, function (response) { });
        }
        request(config.url.uploadImage(), {
            responseType: "json"
            , body: {
                "token": token,
                "imageUrls": [imageUrl],
                "isTemp": true
            }
            , method: "POST"
        }).then(res => {
            if (res !== null) {
                if (res.IsSuccess === true) {
                    callback(res.Url);
                } else {
                    callback(imageUrl);
                }
            } else {
                callback(imageUrl);
            }
        }).catch(reason => {
            callback(imageUrl);
        });
    });
}

//验证采集重复
function VerifyDuplicate(sourceUrl, successfulCallBack, failedCallBack) {
    request(config.url.verifyDuplicate(), {
        responseType: "json"
        , body: {
            "SourceUrl": sourceUrl
        }
        , method: "POST"
    }).then(res => {
        if (res !== null) {
            if (res.IsSuccess === true) {
                successfulCallBack(res.Data.ParentSku);
            } else {
                failedCallBack(res.Message == null ? (res.ResponseError == null ? "服务器未知异常，请联系管理员" : res.ResponseError.Message) : res.Message);
            }
        } else {
            failedCallBack("发生未知异常！请稍后重试！若长时间发生此错误，请与客服联系！");
        }
    }).catch(reason => {
        failedCallBack("服务器连接失败！请稍后重试！若长时间发生此错误，请与客服联系！");
    });
}