<?php

    header('Cache-Control: no-cache');
    header('Pragma: no-cache');

    $username = $_SESSION["Usuario"];
    $password = $_SESSION["Password"];
    $campana = $_SESSION["Campana"];
    $phone = $_SESSION["extension"];
    $phonep = $_SESSION["pass_ext"];
    $dial_method = $_SESSION["dial_method"];
    $url = "https://" . SERVER_IP . "/" . DIRECTORIO_AGC . "/vicidial.php?VD_login=$username&VD_campaign=$campana&phone_login=$phone&phone_pass=$phonep&VD_pass=$password#";

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ApiAgente</title>
</head>

<body>
    <iframe src="<?=$url;?>" id="dv_frame" width="800" height="800" allow="microphone"></iframe>
    <script src="<?=LIB_JS;?>axios.min.js" type="text/javascript"></script>
    <script>
        const iframe = document.getElementById("dv_frame")
        const wiframe = iframe.contentWindow
        let __statusInterval = -1;

        const __post = (url, body) => {
            const fd = new FormData()
            if (body) {
                Object.keys(body).forEach(k => {
                    fd.append(k, body[k]);
                })
            }
            return axios.post("<?php echo MODULOS; ?>" + url, fd).then(r => r.data);
        }

        const __get = (url) => {
            return axios.get("<?php echo MODULOS; ?>" + url).then(r => r.data);
        }

        window.addEventListener("message", evt => {

            const d = evt.data // Informacion/contenido del mensaje
            const o = evt.origin // Origen del mensaje

            /**
             * Funcion para comunicar informacion al padre del iframe
             * debido a un mensaje
             */
            const __s = (data, action) => {
                window.parent.postMessage({
                    action: action ? action : d.action,
                    data: data ? data : {
                        status: "ok"
                    }
                }, o);
            }

            switch (d.action) {
                case "start":
                    __post("ApiAgente/startAgent?token=" + d.token, {}).then(r => {
                        if (r.error) {
                            console.log(r);
                            __s({
                                status: "error",
                                error: r.code
                            });
                        } else {
                            __s();
                        }
                    }).catch(e => {
                        console.log(e);
                        __s({
                            status: 'error',
                            error: -1
                        });
                    });
                    if (d.getStatus) {
                        __statusInterval = setInterval(() => {
                            __post("ApiAgente/status?token=" + d.token, {}).then(r => {
                                if (r.error) {
                                    __s({
                                        status: "error",
                                        error: r.code
                                    });
                                } else {
                                    __s({
                                        status: "ok",
                                        info: r.data,
                                    }, "status")
                                }
                            }).catch(e => {
                                __s({
                                    status: "error",
                                    error: -1
                                }, "status")
                            })
                        }, 1500);
                    }
                    break;
                case "dial":
                    wiframe.NeWManuaLDiaLCalL('NO', '', '', '', '', 'YES');
                    wiframe.document.getElementById('MDPhonENumbeR').value = d.phone;
                    wiframe.NeWManuaLDiaLCalLSubmiT('NOW', 'YES');
                    __s();
                    break;
                case "dialnextprepare":
                    wiframe.AutoDial_ReSume_PauSe(
                        'VDADpause',
                        '',
                        '',
                        '',
                        '',
                        '',
                        '',
                        'YES'
                    );
                    wiframe.PauseCodeSelectContent_create('YES');
                    wiframe.PauseCodeSelect_submit('NEXT', 'YES');
                    setTimeout(() => {
                        __s();
                    }, 2000);
                    break;
                case "dialnext":
                    wiframe.ManualDialNext(
                        '',
                        '',
                        '',
                        '',
                        '',
                        '0',
                        '',
                        '',
                        'YES'
                    );
                    __s();
                    break;
                case "hangup":
                    wiframe.dialedcall_send_hangup('', '', '', '', 'YES');
                    __s();
                    break;
                case "pause":
                    wiframe.AutoDial_ReSume_PauSe(
                        'VDADpause',
                        '',
                        '',
                        '',
                        '',
                        '',
                        '',
                        'YES'
                    );
                    wiframe.PauseCodeSelect_submit(d.code, 'YES');
                    __s()
                    break;
                case "play":
                    wiframe.AutoDial_ReSume_PauSe(
                        'VDADready',
                        '',
                        '',
                        '',
                        '',
                        '',
                        '',
                        'YES'
                    );
                    __s()
                    break;
                case "logout":
                    console.log("logout", d);
                    __post("ApiAgente/logout?token=" + d.token, {}).then(r => {
                        if (r.error) {
                            __s({
                                status: "error",
                                error: r.code
                            });
                        } else {
                            clearInterval(__statusInterval);
                            __s();
                        }
                    }).catch(e => {
                        __s({
                            status: 'error',
                            error: -1
                        });
                    });
                    break;
                case "qualification":
                    wiframe.DispoSelectContent_create(d.status, 'ADD', 'YES');
                    wiframe.DispoSelect_submit('', '', 'YES');
                    __s();
                    break;

                case "pausecodes":
                    __post("ApiAgente/getPauseCodes?token=" + d.token, {}).then(r => {
                        if (r.error) {
                            __s({
                                status: "error",
                                error: r.code
                            });
                        } else {
                            __s(Object.assign({
                                status: "ok"
                            }, r.data))
                        }
                    }).catch(e => {
                        console.log(e);
                        __s({
                            status: 'error',
                            error: -1
                        });
                    });
                    break;
                case "qualificationstatuses":
                    __post("ApiAgente/getQualificationStatuses?token=" + d.token, {}).then(r => {
                        if (r.error) {
                            __s({
                                status: "error",
                                error: r.code
                            });
                        } else {
                            __s(Object.assign({
                                status: "ok"
                            }, r.data))
                        }
                    }).catch(e => {
                        console.log(e);
                        __s({
                            status: 'error',
                            error: -1
                        });
                    });
                    break;
                    break;
            }

        }, false)
    </script>
</body>

</html>