HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/agile-selling-wpb/public/js/users.js
$(document).ready(function () {
    $("#chk_popup_active").change(function() {
        if($("#chk_popup_active").is(":checked")){
            $("#container_popuptext").removeClass("hide");
        }else{
            $("#container_popuptext").addClass("hide");
        }
    });

    $("#btn-menu-usuario").on("click", function () {
        $("#menuUsuario").toggle("slow");
    });

    $("input[name='phone']").keyup(function () {
        this.value = (this.value + "").replace(/[^0-9]/g, "");
    });
    $("input[name='document']").keyup(function () {
        this.value = (this.value + "").replace(/[^0-9]/g, "");
    });
    $("input[name='document']").keyup(function () {
        this.value = (this.value + "").replace(/[^0-9]/g, "");
    });
    $("form")
        .find("input[type=email]")
        .blur(function () {
            caracteresCorreoValido($(this).val(), "#email");
        });

    $("input[name=email]").keyup(function () {
        var regex = /[\w-\.]{2,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;

        if (regex.test($("#email").val().trim())) {
            $(this).css("border", "1px solid green");
            document.getElementById("btn-create-user").disabled = false;
        } else {
            $(this).css("border", "1px solid red");
            document.getElementById("btn-create-user").disabled = true;
        }
    });
    $("input[name=email]").keyup(function () {
        var regex = /[\w-\.]{2,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;

        if (regex.test($("#email").val().trim())) {
            $(this).css("border", "1px solid green");
            document.getElementById("btn-create-user").disabled = false;
        } else {
            $(this).css("border", "1px solid red");
            document.getElementById("btn-create-user").disabled = true;
        }
    });
});

///crear cliente
function createClient() {
    $("#createClient").validator("update");
    $("#createClient")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();
                let bo_price_list_id = null;
                if ($("#createClient select[name='bo_price_list_id']").val()) {
                    bo_price_list_id = $(
                        "#createClient select[name='bo_price_list_id']"
                    ).val();
                }

                if ($("#bo_price_list_id"))
                    var info = {
                        first_name: $(
                            " #createClient input[name='first_name']"
                        ).val(),
                        phone: $(" #createClient input[name='phone']").val(),
                        email: $(" #createClient input[name='email']").val(),
                        document: $(
                            " #createClient input[name='document']"
                        ).val(),
                        last_name: $(
                            " #createClient input[name='last_name']"
                        ).val(),
                        password: $(
                            " #createClient input[name='confirmPassword']"
                        ).val(),
                        direction: $(
                            "#createClient input[name='direction']"
                        ).val(),
                        district: $(
                            "#createClient input[name='district']"
                        ).val(),
                        indications: $(
                            "#createClient textarea[name='indications']"
                        ).val(),
                        lat: $("#createClient input[name='direction']").attr(
                            "data-lat"
                        ),
                        long: $("#createClient input[name='direction']").attr(
                            "data-long"
                        ),
                        city_id: $(
                            "#createClient select[name='city_id']"
                        ).val(),
                        bo_price_list_id,
                    };

                    if(!info.document){
                        info.document = new Date().getTime();
                    }

                swal(Lang.get("messagesClient.user_client_tag1"), {
                    buttons: {
                        cancel: "No",
                        Ok: true,
                    },
                }).then((val) => {
                    if (val == "Ok") {
                        $.ajax({
                            url: "/clients/create",
                            type: "POST",
                            contentType: "application/json",
                            headers: {
                                "X-CSRF-TOKEN": $(
                                    'meta[name="csrf-token"]'
                                ).attr("content"),
                                "Content-Type": "application/json",
                            },
                            data: JSON.stringify(info),
                            success: function (r) {
                                if (r.r) {
                                    swal(r.m, {
                                        icon: "success",
                                        buttons: {
                                            Ok: true,
                                        },
                                    }).then((val) => {
                                        if (val == "Ok") {
                                            console.log("creado");
                                            $(location).attr(
                                                "href",
                                                "/clients"
                                            );
                                        }
                                    });
                                } else {
                                    if (r.r == false) {
                                        swal("Error", r.m, "warning");
                                    } else {
                                        swal(
                                            "Error",
                                            Lang.get(
                                                "messagesClient.user_client_tag3"
                                            ),
                                            "error"
                                        );
                                    }
                                }
                            },
                        });
                    }
                });
            }
        });
}

function createDeliveryMan() {
    $("#createDeliveryMan").validator("update");
    $("#createDeliveryMan")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();

                password = $(
                    " #createDeliveryMan input[name='passwordM']"
                ).val();
                confirmada = $(
                    " #createDeliveryMan input[name='confirmPasswordM']"
                ).val();

                sucursal = $(
                    " #createDeliveryMan select[name='sucursal_id']"
                ).val();
                company = $(
                    " #createDeliveryMan select[name='courier_company_id']"
                ).val();
                if (sucursal == "") {
                    swal(
                        "Ok",
                        Lang.get("messagesClient.user_deliveryMan_tag1"),
                        "warning"
                    );
                } else if (company == "") {
                    swal(
                        "Ok",
                        Lang.get("messagesClient.user_deliveryMan_tag2"),
                        "warning"
                    );
                } else if (password != confirmada) {
                    swal("Error", "¡Las contraseñas deben coincidir!", "error");
                } else {
                    var info = {
                        first_name: $(
                            " #createDeliveryMan input[name='first_name']"
                        ).val(),
                        phone: $(
                            " #createDeliveryMan input[name='phone']"
                        ).val(),
                        email: $(
                            " #createDeliveryMan input[name='email']"
                        ).val(),
                        courier_company: $(
                            " #createDeliveryMan input[name='courier_company']"
                        ).val(),
                        document: $(
                            " #createDeliveryMan input[name='document']"
                        ).val(),
                        last_name: $(
                            " #createDeliveryMan input[name='last_name']"
                        ).val(),
                        plate: $(
                            " #createDeliveryMan input[name='plate']"
                        ).val(),
                        password: $(
                            " #createDeliveryMan input[name='confirmPasswordM']"
                        ).val(),
                        sucursal_id: $(
                            " #createDeliveryMan select[name='sucursal_id']"
                        ).val(),
                        courier_company_id: $(
                            " #createDeliveryMan select[name='courier_company_id']"
                        ).val(),
                    };
                    swal(Lang.get("messagesClient.user_deliveryMan_tag3"), {
                        buttons: {
                            cancel: "No",
                            Ok: true,
                        },
                    }).then((val) => {
                        if (val == "Ok") {
                            $.ajax({
                                url: "/deliveryMan/create",
                                type: "POST",
                                contentType: "application/json",
                                headers: {
                                    "X-CSRF-TOKEN": $(
                                        'meta[name="csrf-token"]'
                                    ).attr("content"),
                                    "Content-Type": "application/json",
                                },
                                data: JSON.stringify(info),
                                success: function (r) {
                                    if (r.r) {
                                        if (r.id) {
                                            idUser = r.id;
                                            swal(r.m, {
                                                icon: "warning",
                                                buttons: {
                                                    Ok: true,
                                                },
                                            }).then((val) => {
                                                if (val == "Ok") {
                                                    console.log(
                                                        "recuperar cuenta id: " +
                                                            idUser
                                                    );
                                                    recoverAccount(idUser);
                                                }
                                            });
                                        } else {
                                            var img =
                                                document.getElementById(
                                                    "photo"
                                                );
                                            if (img != null) {
                                                console.log(r.d);
                                                saveImageUser(
                                                    r.d,
                                                    "/user/saveImage"
                                                );
                                            } else {
                                                swal(r.m, {
                                                    icon: "success",
                                                    buttons: {
                                                        Ok: true,
                                                    },
                                                }).then((val) => {
                                                    if (val == "Ok") {
                                                        console.log("creado");
                                                        $(location).attr(
                                                            "href",
                                                            "/deliveryMan"
                                                        );
                                                    }
                                                });
                                            }
                                        }
                                    } else {
                                        swal("Error", r.m, "error");
                                    }
                                },
                                error: function (e) {
                                    swal(
                                        "Error",
                                        Lang.get(
                                            "messagesClient.user_deliveryMan_tag4"
                                        ),
                                        "error"
                                    );
                                },
                            });
                        }
                    });
                }
            }
        });
}

function createAdmin() {
    $("#createAdmin").validator("update");
    $("#createAdmin")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();

                password = $(" #createAdmin input[name='password']").val();
                confirmada = $(
                    " #createAdmin input[name='confirmPasswordA']"
                ).val();
                if (password != confirmada) {
                    swal("Error", "¡Las contraseñas deben coincidir!", "error");
                } else {
                    console.log("esta bueno");
                    var info = {
                        first_name: $(
                            " #createAdmin input[name='first_name']"
                        ).val(),
                        phone: $(" #createAdmin input[name='phone']").val(),
                        email: $(" #createAdmin input[name='email']").val(),
                        last_name: $(
                            " #createAdmin input[name='last_name']"
                        ).val(),
                        document: $(
                            " #createAdmin input[name='document']"
                        ).val(),
                        rol_id: $(" #createAdmin select[name='rol_id']").val(),
                        sucursal_id: $(
                            " #createAdmin select[name='sucursal_id']"
                        ).val(),
                        password: $(
                            " #createAdmin input[name='confirmPasswordA']"
                        ).val(),
                        bo_sales_people_id: $(
                            " #createAdmin select[name='bo_sales_people_id']"
                        ).val(),
                    };

                    if(!info.document){
                        info.document = new Date().getTime();
                    }

                    swal(Lang.get("messagesClient.user_admin_tag1"), {
                        buttons: {
                            cancel: "No",
                            Ok: true,
                        },
                    }).then((val) => {
                        if (val == "Ok") {
                            $.ajax({
                                url: "/admins/create",
                                type: "POST",
                                contentType: "application/json",
                                headers: {
                                    "X-CSRF-TOKEN": $(
                                        'meta[name="csrf-token"]'
                                    ).attr("content"),
                                    "Content-Type": "application/json",
                                },
                                data: JSON.stringify(info),
                                success: function (r) {
                                    if (r.r) {
                                        swal(r.m, {
                                            icon: "success",
                                            buttons: {
                                                Ok: true,
                                            },
                                        }).then((val) => {
                                            if (val == "Ok") {
                                                console.log("creado");
                                                $(location).attr(
                                                    "href",
                                                    "/admins"
                                                );
                                            }
                                        });
                                    } else {
                                        swal("Error", r.m, "error");
                                    }
                                },
                                error: function (e) {
                                    swal(
                                        "Error",
                                        "Error al crear usuario",
                                        "error"
                                    );
                                },
                            });
                        }
                    });
                }
            }
        });
}

// MODAL INFO USUARIO
function clickInfoAdmins(value) {
    var info = {
        id: value,
    };
    $.ajax({
        url: "admins/info",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            $("#first_name").text(r.u.first_name);
            if (r.u.last_name != null) {
                $("#last_name").text(r.u.last_name);
            } else {
                $("#last_name").text("");
            }
            $("#document").text(r.u.document);
            $("#email").text(r.u.email);
            $("#phone").text(r.u.phone);
            var date = new Date(r.u.created_at);
            var month = date.getMonth() + 1;
            $("#created_at").text((month.length > 1 ? "0" + month: month ) + "/" + date.getDate() + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
            // $("#created_at").text(r.u.created_at);
            $("#sucursal").text(r.i.sucursal_id);

            if (r.i.active == 1) {
                $("#active").text("Si");
            } else {
                $("#active").text("No");
            }
            $("#modal-info-admin").modal();
        },
    });
}

// MODAL INFO CLIENTE
function clickInfoClient(value) {
    var info = {
        id: value,
    };
    $.ajax({
        url: "clients/info",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            console.log(r);
            $("#first_name").text(r.first_name);
            if (r.last_name != null) {
                $("#last_name").text(r.last_name);
            } else {
                $("#last_name").text("");
            }

            $("#document").text(r.document);
            $("#category").text(r.bo_price_list ? r.bo_price_list.list_name : '');
            $("#email").text(r.email);
            $("#phone").text(r.phone);
            var date = new Date(r.created_at);
            var month = date.getMonth() + 1;
            $("#created_at").text((month.length > 1 ? "0" + month: month ) + "/" + date.getDate() + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
            // $("#created_at").text(r.created_at)
            if (r.active == 1) {
                $("#active").text("Si");
            } else {
                $("#active").text("No");
            }
            $("#modal-info-client").modal();
        },
    });
}

// MODAL INFO MENSAJERO
function clickInfoDeliveryMan(value) {
    var info = {
        id: value,
    };

    $.ajax({
        url: "deliveryMan/info",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            $("#first_name").text(r.u.first_name);
            if (r.u.last_name != null) {
                $("#last_name").text(r.u.last_name);
            } else {
                $("#last_name").text("");
            }
            $("#document").text(r.u.document);
            $("#email").text(r.u.email);
            $("#phone").text(r.u.phone);
            $("#created_at").text(r.u.created_at);

            $("#plate").text(r.i.plate);
            $("#courier_company_id").text(r.i.courier_company_id);
            console.log(r.img);
            $("#photo").attr("src", r.img);

            if (r.u.active == 1) {
                $("#active").text("Si");
            } else {
                $("#active").text("No");
            }
            $("#modal-info-deliveryMan").modal();
        },
    });
}

//editar cliente
function clickEditClient(value) {
    _table = $("#example2").DataTable();
    index = _table.page();
    localStorage.setItem("productsTableIndex", index);
    $(location).attr("href", "/clients/edit/" + value);
}

function editClient() {
    var dir = $("#editClient input[name='direction']").val();

    if (dir != "") {
        $(" #editClient select[name='city_id']").attr("required", true);
    } else {
        $(" #editClient select[name='city_id']").attr("required", false);
    }

    let bo_price_list_id = null;
    if ($("#editClient select[name='bo_price_list_id']").val()) {
        bo_price_list_id = $(
            "#editClient select[name='bo_price_list_id']"
        ).val();
    }

    $("#editClient").validator("update");
    $("#editClient")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();

                let popup_active = $("#chk_popup_active").is(":checked");
                let popup_text = $(" #editClient input[name='popup_text']").val();
                if(popup_active && popup_text == ""){
                    swal(
                        "Ok",
                        Lang.get("messagesClient.product_tag11"),
                        "warning"
                    );
                    return false;
                }

                var info = {
                    id: $("#btn-edit-client").attr("data-id"),
                    first_name: $(
                        " #editClient input[name='first_name']"
                    ).val(),
                    phone: $(" #editClient input[name='phone']").val(),
                    email: $(" #editClient input[name='email']").val(),
                    direction_used: $(
                        " #editClient select[name='direction_id']"
                    ).val(),
                    document: $(" #editClient input[name='document']").val(),
                    last_name: $(" #editClient input[name='last_name']").val(),
                    // "password": $(" #createClient input[name='confirmPassword']").val(),
                    direction: $("#editClient input[name='direction']").val(),
                    district: $("#editClient input[name='district']").val(),
                    indications: $(
                        "#editClient textarea[name='indications']"
                    ).val(),
                    lat: $("#editClient input[name='direction']").attr(
                        "data-lat"
                    ),
                    long: $("#editClient input[name='direction']").attr(
                        "data-long"
                    ),
                    city_id: $(" #editClient select[name='city_id']").val(),
                    bo_price_list_id,
                    popup_active,
                    popup_text,
                    required_po: $("#chk_required_po").is(":checked")
                };

                swal(Lang.get("messagesClient.user_client_tag5"), {
                    buttons: {
                        cancel: "No",
                        Ok: true,
                    },
                }).then((val) => {
                    if (val == "Ok") {
                        $.ajax({
                            url: "/clients/update",
                            type: "PUT",
                            contentType: "application/json",
                            headers: {
                                "X-CSRF-TOKEN": $(
                                    'meta[name="csrf-token"]'
                                ).attr("content"),
                                "Content-Type": "application/json",
                            },
                            data: JSON.stringify(info),
                            success: function (r) {
                                if (r.r) {
                                    swal(r.m, {
                                        icon: "success",
                                        buttons: {
                                            Ok: true,
                                        },
                                    }).then((val) => {
                                        if (val == "Ok") {
                                            console.log("creado");
                                            localStorage.setItem(
                                                "setPageTable",
                                                true
                                            );
                                            $(location).attr(
                                                "href",
                                                "/clients"
                                            );
                                        }
                                    });
                                } else {
                                    swal("Error", r.m, "error");
                                }
                            },
                            error: function (e) {
                                swal(
                                    "Error",
                                    Lang.get("messagesClient.user_client_tag6"),
                                    "error"
                                );
                            },
                        });
                    }
                });
            }
        });
}

//editar mensajero
function clickEditDeliveryMan(value) {
    _table = $("#example2").DataTable();
    index = _table.page();
    localStorage.setItem("productsTableIndex", index);
    $(location).attr("href", "/deliveryMan/edit/" + value);
}

function editDeliveryMan() {
    $("#editDeliveryMan").validator("update");
    $("#editDeliveryMan")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();
                sucursal = $(
                    " #editDeliveryMan select[name='sucursal_id']"
                ).val();
                company = $(
                    " #editDeliveryMan select[name='courier_company_id']"
                ).val();
                if (sucursal == "") {
                    swal(
                        "Ok",
                        Lang.get("messagesClient.user_deliveryMan_tag1"),
                        "warning"
                    );
                } else if (company == "") {
                    swal(
                        "Ok",
                        Lang.get("messagesClient.user_deliveryMan_tag2"),
                        "warning"
                    );
                } else {
                    var info = {
                        id: $("#btn-edit-deliveryMan").attr("data-id"),
                        first_name: $(
                            " #editDeliveryMan input[name='first_name']"
                        ).val(),
                        phone: $(" #editDeliveryMan input[name='phone']").val(),
                        email: $(" #editDeliveryMan input[name='email']").val(),
                        courier_company: $(
                            " #editDeliveryMan input[name='courier_company']"
                        ).val(),
                        document: $(
                            " #editDeliveryMan input[name='document']"
                        ).val(),
                        last_name: $(
                            " #editDeliveryMan input[name='last_name']"
                        ).val(),
                        plate: $(" #editDeliveryMan input[name='plate']").val(),
                        password: $(
                            " #editDeliveryMan input[name='confirmPasswordM']"
                        ).val(),
                        sucursal_id: $(
                            " #editDeliveryMan select[name='sucursal_id']"
                        ).val(),
                        courier_company_id: $(
                            " #editDeliveryMan select[name='courier_company_id']"
                        ).val(),
                    };
                    swal(Lang.get("messagesClient.user_deliveryMan_tag5"), {
                        buttons: {
                            cancel: "No",
                            Ok: true,
                        },
                    }).then((val) => {
                        if (val == "Ok") {
                            $.ajax({
                                url: "/deliveryMan/update",
                                type: "POST",
                                contentType: "application/json",
                                headers: {
                                    "X-CSRF-TOKEN": $(
                                        'meta[name="csrf-token"]'
                                    ).attr("content"),
                                    "Content-Type": "application/json",
                                },
                                data: JSON.stringify(info),
                                success: function (r) {
                                    if (r.r) {
                                        swal(r.m, {
                                            icon: "success",
                                            buttons: {
                                                Ok: true,
                                            },
                                        }).then((val) => {
                                            if (val == "Ok") {
                                                console.log("creado");
                                                localStorage.setItem(
                                                    "setPageTable",
                                                    true
                                                );
                                                $(location).attr(
                                                    "href",
                                                    "/deliveryMan"
                                                );
                                            }
                                        });
                                    } else {
                                        swal("Error", r.m, "error");
                                    }
                                },
                                error: function (e) {
                                    swal(
                                        "Error",
                                        Lang.get(
                                            "messagesClient.user_deliveryMan_tag6"
                                        ),
                                        "error"
                                    );
                                },
                            });
                        }
                    });
                }
            }
        });
}

//editar admin
function clickEditAdmins(value) {
    _table = $("#example2").DataTable();
    index = _table.page();
    localStorage.setItem("productsTableIndex", index);
    $(location).attr("href", "/admins/edit/" + value);
}

function editAdmin() {
    $("#editAdmin").validator("update");
    $("#editAdmin")
        .validator("update")
        .on("submit", function (e) {
            if (e.isDefaultPrevented()) {
            } else {
                e.preventDefault();

                var info = {
                    id: $("#btn-edit-user").attr("data-id"),
                    first_name: $(" #editAdmin input[name='first_name']").val(),
                    phone: $(" #editAdmin input[name='phone']").val(),
                    email: $(" #editAdmin input[name='email']").val(),
                    last_name: $(" #editAdmin input[name='last_name']").val(),
                    document: $(" #editAdmin input[name='document']").val(),
                    rol_id: $(" #editAdmin select[name='rol_id']").val(),
                    sucursal_id: $(
                        " #editAdmin select[name='sucursal_id']"
                    ).val(),
                    password: $(" #editAdmin input[name='password']").val(),
                    bo_sales_people_id: $(
                        "#editAdmin select[name='bo_sales_people_id']"
                    ).val(),
                };
                swal(Lang.get("messagesClient.user_admin_tag3"), {
                    buttons: {
                        cancel: "No",
                        Ok: true,
                    },
                }).then((val) => {
                    if (val == "Ok") {
                        $.ajax({
                            url: "/admins/update",
                            type: "POST",
                            contentType: "application/json",
                            headers: {
                                "X-CSRF-TOKEN": $(
                                    'meta[name="csrf-token"]'
                                ).attr("content"),
                                "Content-Type": "application/json",
                            },
                            data: JSON.stringify(info),
                            success: function (r) {
                                if (r.r) {
                                    swal(r.m, {
                                        icon: "success",
                                        buttons: {
                                            Ok: true,
                                        },
                                    }).then((val) => {
                                        if (val == "Ok") {
                                            console.log("editado");
                                            localStorage.setItem(
                                                "setPageTable",
                                                true
                                            );
                                            $(location).attr("href", "/admins");
                                        }
                                    });
                                } else {
                                    swal("Error", r.m, "error");
                                }
                            },
                            error: function (e) {
                                swal(
                                    "Error",
                                    Lang.get("messagesClient.user_admin_tag4"),
                                    "error"
                                );
                            },
                        });
                    }
                });
            }
        });
}

function initMap() {
    var map = new google.maps.Map(document.getElementById("map"), {
        center: {
            lat: -34.608444,
            lng: -58.3722367,
        },
        zoom: 15,
    });
    var geocoder = new google.maps.Geocoder();

    document.getElementById("submit").addEventListener("click", function () {
        geocodeAddress(geocoder, map);
    });
}

function importArchivo() {
    var archivo = document.getElementById("archivo");
    archivo = archivo.files[0];
    if (archivo) {
        var formData = new FormData();

        formData.append("archivo", archivo);

        $.ajax({
            type: "POST",
            dataType: "json",
            processData: false,
            contentType: false,
            headers: {
                "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            },
            data: formData,
            url: "/Clientes/importar",
            success: function (r) {
                if (r[0]["respuesta"]) {
                    swal("Carga realizada con éxito!", {
                        icon: "success",
                        buttons: {
                            Ok: true,
                        },
                    }).then((val) => {
                        if (val == "Ok") {
                            console.log("creado");
                            $(location).attr("href", "/Clientes");
                        }
                    });
                } else {
                    swal(
                        "Error",
                        "¡Error al ingresar datos por réplica de clientes!",
                        "error"
                    );
                }
            },
        });
    } else {
        swal("Ok", "¡No hay archivo!", "warning");
    }
}

function importArchivoMensajeros() {
    var archivo = document.getElementById("archivo");
    archivo = archivo.files[0];
    if (archivo) {
        var formData = new FormData();

        formData.append("archivo", archivo);

        $.ajax({
            type: "POST",
            dataType: "json",
            processData: false,
            contentType: false,
            headers: {
                "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            },
            data: formData,
            url: "/Mensajeros/importar",
            success: function (r) {
                if (r[0]["respuesta"]) {
                    swal("Carga realizada con éxito!", {
                        icon: "success",
                        buttons: {
                            Ok: true,
                        },
                    }).then((val) => {
                        if (val == "Ok") {
                            console.log("creado");
                            $(location).attr("href", "/Mensajeros");
                        }
                    });
                } else {
                    swal(
                        "Error",
                        "¡Error al ingresar datos por réplica de Mensajeros!",
                        "error"
                    );
                }
            },
        });
    } else {
        swal("Ok", "¡No hay archivo!", "warning");
    }
}

/**Activar/Desactivar clientes usando switch*/
function chkClient(value) {
    var state;
    if ($("#Checkactive" + value).is(":checked")) {
        state = true;
    } else {
        state = false;
    }

    var info = {
        id: value,
        state: state,
    };

    $.ajax({
        url: "/clients/active",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            if (r.r) {
                swal("Ok", "¡Cliente modificado con éxito!");
            } else {
                swal(
                    "Error",
                    Lang.get("messagesClient.user_client_tag6"),
                    "error"
                );
            }
        },
        error: function (e) {
            swal("Error", Lang.get("messagesClient.user_client_tag6"), "error");
        },
    });
}

/**Activar/Desactivar mensajeros usando switch*/
function chkDeliveryMan(value) {
    var state;
    if ($("#Checkactive" + value).is(":checked")) {
        state = true;
    } else {
        state = false;
    }

    var info = {
        id: value,
        state: state,
    };

    $.ajax({
        url: "/deliveryMan/active",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            if (r.r) {
                swal("Ok", r.m);
            } else {
                swal(
                    "Error",
                    Lang.get("messagesClient.user_deliveryMan_tag6"),
                    "error"
                );
            }
        },
        error: function (e) {
            swal(
                "Error",
                Lang.get("messagesClient.user_deliveryMan_tag6"),
                "error"
            );
        },
    });
}

function chkOnlineDelivery(value) {
    var state;
    if ($("#Checkonline" + value).is(":checked")) {
        state = true;
    } else {
        state = false;
    }

    var info = {
        id: value,
        state: state,
    };

    $.ajax({
        url: "/deliveryMan/online",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            if (r.r) {
                swal("Ok", r.m);
            } else {
                swal(
                    "Error",
                    Lang.get("messagesClient.user_deliveryMan_tag6"),
                    "error"
                );
            }
        },
        error: function (e) {
            swal(
                "Error",
                Lang.get("messagesClient.user_deliveryMan_tag6"),
                "error"
            );
        },
    });
}

/**Activar/Desactivar administrador usando switch*/
function chkAdmins(value) {
    var state;

    if (this.checked) {
        state = true;
    } else {
        state = false;
    }

    var info = {
        id: value,
        state: state,
    };

    $.ajax({
        url: "/admins/active",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            if (r.r) {
                swal("Ok", r.m);
            } else {
                swal(
                    "Error",
                    Lang.get("messagesClient.user_admin_tag4"),
                    "error"
                );
            }
        },
    });
}

/**Validación de confirmar contraseña para Cliente */
$("#confirmPassword").change(function () {
    var password = $(" #createClient input[name='password']").val();
    var confirmada = $(" #createClient input[name='confirmPassword']").val();
    if (password != confirmada) {
        swal("Error", "¡Las contraseñas deben coincidir!", "error");
    }
});

/**Validación de confirmar contraseña para Mensajero */
$("#confirmPasswordM").change(function () {
    var password = $(" #createDeliveryMan input[name='passwordM']").val();
    var confirmada = $(
        " #createDeliveryMan input[name='confirmPasswordM']"
    ).val();
    if (password != confirmada) {
        swal("Error", "¡Las contraseñas deben coincidir!", "error");
    }
});

/**Validación de confirmar contraseña para Admin */
$("#confirmPasswordA").change(function () {
    var password = $(" #createAdmin input[name='password']").val();
    var confirmada = $(" #createAdmin input[name='confirmPasswordA']").val();
    if (password != confirmada) {
        console.log("mala: " + password);
        swal("Error", "¡Las contraseñas deben coincidir!", "error");
    }
});

/**Método para eliminar admin desde la tabla de admins y en editar */
function clickDeleteAdmins(value) {
    swal(Lang.get("messagesClient.user_admin_tag5"), {
        buttons: {
            cancel: "No",
            Ok: true,
        },
    }).then((val) => {
        if (val == "Ok") {
            var info = {
                id: value,
            };
            console.log(info);

            $.ajax({
                url: "/admins/delete",
                type: "POST",
                contentType: "application/json",
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
                        "content"
                    ),
                    "Content-Type": "application/json",
                },
                data: JSON.stringify(info),
                success: function (r) {
                    console.log(r);
                    if (r.r) {
                        swal(r.m, {
                            icon: "success",
                            buttons: {
                                Ok: true,
                            },
                        }).then((val) => {
                            if (val == "Ok") {
                                console.log("creado");
                                $(location).attr("href", "/admins");
                            }
                        });
                    } else {
                        swal(
                            "Ok",
                            Lang.get("messagesClient.user_admin_tag6"),
                            "error"
                        );
                    }
                },
                error: function (e) {
                    swal(
                        "Ok",
                        Lang.get("messagesClient.user_admin_tag6"),
                        "error"
                    );
                },
            });
        }
    });
}

/**Método para eliminar mensajero desde la tabla de admins y en editar */
function clickDeleteDeliveryMan(value) {
    swal(Lang.get("messagesClient.user_deliveryMan_tag8"), {
        buttons: {
            cancel: "No",
            Ok: true,
        },
    }).then((val) => {
        if (val == "Ok") {
            var info = {
                id: value,
            };

            $.ajax({
                url: "/deliveryMan/delete",
                type: "POST",
                contentType: "application/json",
                headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
                        "content"
                    ),
                    "Content-Type": "application/json",
                },
                data: JSON.stringify(info),
                success: function (r) {
                    console.log(r);
                    if (r.r) {
                        swal(r.m, {
                            icon: "success",
                            buttons: {
                                Ok: true,
                            },
                        }).then((val) => {
                            if (val == "Ok") {
                                $(location).attr("href", "/deliveryMan");
                            }
                        });
                    } else {
                        swal(
                            "Error",
                            Lang.get("messagesClient.user_deliveryMan_tag8"),
                            "error"
                        );
                    }
                },
                error: function (r) {
                    swal(
                        "Error",
                        Lang.get("messagesClient.user_deliveryMan_tag8"),
                        "error"
                    );
                },
            });
        }
    });
}

/**Activar/Desactivar sesion multiple de mensajeros usando switch*/
function chkMSDeliveryMan(value) {
    var state;
    if ($("#CheckMS" + value).is(":checked")) {
        state = 1;
    } else {
        state = 0;
    }

    var info = {
        id: value,
        state: state,
    };

    $.ajax({
        url: "/deliveryMan/multipleSession",
        type: "POST",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            if (r.r) {
                swal("Ok", r.m);
            } else {
                swal(
                    "Error",
                    Lang.get("messagesClient.user_deliveryMan_tag7"),
                    "error"
                );
            }
        },
        error: function (e) {
            swal(
                "Error",
                Lang.get("messagesClient.user_deliveryMan_tag7"),
                "error"
            );
        },
    });
}

function createDirectionSucursal() {
    var type = $("#select_type_order option:selected").val();
    console.log("type: ", type);

    if (type && type == 1) {
        //obtener valores modal
        var lat = $("#pac-input").attr("data-lat");
        var long = $("#pac-input").attr("data-long");
        var instru = $("#indications-modal").val();
        var district = $("#district-modal").val();
        var valor = $("#pac-input").val();

        //set valores vista
        $("#direction").val(valor);
        $("#direction").attr("data-id", "");
        $("#direction").attr("data-lat", lat);
        $("#direction").attr("data-long", long);
        $("#indications").val(instru);
        $("#district").val(district);
        $("#modal_register_direction").css("display", "none");
        $(".modal-backdrop").attr("class", "modal fade");
        document.documentElement.style.overflow = "scroll";

        if (lat && long) {
            // calculoCobertura(lat, long);
        } else {
            swal("Advertencia", "¡Dirección incorrecta!", "warning");
        }
    } else if (type && type == 5) {
        //obtener valores modal
        var lat = $("#pac-input").attr("data-lat");
        var long = $("#pac-input").attr("data-long");
        var instru = $("#indications-modal").val();
        var district = $("#district-modal").val();
        var valor = $("#pac-input").val();

        //set valores vista
        $("#address_id").append(
            $("<option>", {
                value: 0,
                text: valor,
            })
        );
        $("#address_id").attr("ec-data-id", "");
        $("#address_id").attr("ec-data-direction", valor);
        $("#address_id").attr("ec-data-lat", lat);
        $("#address_id").attr("ec-data-long", long);
        $("#address_id").attr("ec-data-district", district);
        $("#address_id").attr("ec-data-instru", instru);
        $("#modal_register_direction").css("display", "none");
        $(".modal-backdrop").attr("class", "modal fade");
        document.documentElement.style.overflow = "scroll";

        $("#address_id").val(0).change();

        if (lat && long) {
            // calculoCobertura(lat, long);
        } else {
            swal("Advertencia", "¡Dirección incorrecta!", "warning");
        }
    } else {
        //obtener valores modal
        var lat = $("#pac-input").attr("data-lat");
        var long = $("#pac-input").attr("data-long");
        var instru = $("#indications-modal").val();
        var district = $("#district-modal").val();
        var valor = $("#pac-input").val();

        //set valores vista
        $("#direction").val(valor);
        $("#direction").attr("data-id", "");
        $("#direction").attr("data-lat", lat);
        $("#direction").attr("data-long", long);
        $("#indications").val(instru);
        $("#district").val(district);
        $("#modal_register_direction").css("display", "none");
        $(".modal-backdrop").attr("class", "modal fade");
        document.documentElement.style.overflow = "scroll";

        if (lat && long) {
            // calculoCobertura(lat, long);
        } else {
            swal("Advertencia", "¡Dirección incorrecta!", "warning");
        }
    }
}

function recoverAccount(id) {
    var info = {
        id: id,
    };

    $.ajax({
        url: "/deliveryMan/recover",
        type: "PUT",
        contentType: "application/json",
        headers: {
            "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            "Content-Type": "application/json",
        },
        data: JSON.stringify(info),
        success: function (r) {
            if (r.r) {
                swal(r.m, {
                    icon: "success",
                    buttons: {
                        Ok: true,
                    },
                }).then((val) => {
                    if (val == "Ok") {
                        console.log("cuenta recuperada");
                        clickEditDeliveryMan(r.id);
                    }
                });
            } else {
                swal("Error", r.m, "error");
            }
        },
        error: function (e) {
            swal(
                "Error",
                Lang.get("messagesClient.user_deliveryMan_tag9"),
                "error"
            );
        },
    });
}

function saveImageUser(id, ruta) {
    var fImage = document.getElementById("photo");
    fImage = fImage.files[0];
    if (fImage) {
        var formData = new FormData();
        formData.append("id", id);
        formData.append("photo", fImage);

        $.ajax({
            type: "POST",
            dataType: "json",
            processData: false,
            contentType: false,
            headers: {
                "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
            },
            data: formData,
            url: ruta,
            success: function (r) {
                if (r.r) {
                    swal(r.m, {
                        icon: "success",
                        buttons: {
                            Ok: true,
                        },
                    }).then((val) => {
                        if (val == "Ok") {
                            if (ruta == "/user/saveImage") {
                                $(location).attr("href", "/deliveryMan");
                            } else {
                                $("#photo").attr("src", r.file);
                            }
                        }
                    });
                } else {
                    swal("Ok", "¡Error al guardar imagen!", "error");
                    $("#btn-create-user").prop("disabled", false);
                }
            },
            error: function (e) {
                swal("Ok", "¡Error al guardar imagen!", "error");
                $("#btn-create-user").prop("disabled", false);
            },
        });
    } else {
        swal("Ok", "Mensajero creado", {
            icon: "success",
            buttons: {
                Ok: true,
            },
        }).then((val) => {
            if (val == "Ok") {
                $(location).attr("href", "/deliveryMan");
            }
        });
    }
}