/*
 * Copyright 2020 Netsyms Technologies.
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


function clientSearchAsync(routeTo, routeFrom, resolve, reject) {
    app.dialog.preloader("Searching...");

    apirequest(
            "searchclients",
            {
                q: routeTo.params.q
            },
            function (resp) {
                app.dialog.close();
                if (resp.status == "ERROR") {
                    app.dialog.alert(resp.msg, "Error");
                    reject();
                } else {
                    var context = {
                        clients: resp.clients,
                        q: routeTo.params.q
                    };

                    resolve({
                        templateUrl: "pages/clients.html",
                    }, {
                        context: context
                    });
                }
            },
            function (xhr) {
                app.dialog.close();
                var error = $.parseJSON(xhr.responseText);
                if (error && typeof error.msg != 'undefined') {
                    app.dialog.alert(error.msg, "Error");
                } else {
                    app.dialog.alert("A server or network error occurred.", "Error");
                }
                reject();
            });
}

function clientGetAsync(routeTo, routeFrom, resolve, reject) {
    app.dialog.preloader("Expanding client...");

    apirequest(
            "getclient",
            {
                clientid: routeTo.params.clientid
            },
            function (resp) {
                app.dialog.close();
                if (resp.status == "ERROR") {
                    app.dialog.alert(resp.msg, "Error");
                    reject();
                } else {

                    var machinelist = [];
                    for (var i = 0; i < resp.machines.length; i++) {
                        var machine = resp.machines[i];
                        machine["props"] = [];
                        for (var k in machine.formdata.labels) {
                            if (machine.hasOwnProperty(k)) {
                                switch (k) {
                                    case "type":
                                    case "clientid":
                                        // skip these
                                        break;
                                    default:
                                        var value = machine.info[k];
                                        if (value == false || value == 0) {
                                            value = false;
                                        } else {
                                            value = value.toString().replace("\n", " <br />");
                                        }
                                        machine.props.push({
                                            name: k,
                                            value: value
                                        });
                                }
                            }
                        }
                        console.log(machine);
                        machinelist.push(machine);
                    }

                    // Add <br> for newlines
                    for (var k in resp.client) {
                        if (resp.client.hasOwnProperty(k)) {
                            resp.client[k] = resp.client[k].toString().replace("\n", " <br>");
                        }
                    }

                    if (typeof resp.client.phone == 'string') {
                        resp.client.phonestripped = resp.client.phone.replace(/\D/g, '');
                    }

                    var context = {
                        client: resp.client,
                        machines: machinelist
                    };

                    resolve({
                        templateUrl: "pages/client.html",
                    }, {
                        context: context
                    });
                }
            },
            function (xhr) {
                app.dialog.close();
                var error = $.parseJSON(xhr.responseText);
                if (error && typeof error.msg != 'undefined') {
                    app.dialog.alert(error.msg, "Error");
                } else {
                    app.dialog.alert("A server or network error occurred.", "Error");
                }
                reject();
            });

}