<?php

/*CONEXAO COM O BANCO*/
include "Connections/config.php";
/*********************/

$res = $db->query("SELECT * FROM flixiptv");
if (isset($_POST["submit"])) {
    $we = strtotime($_POST["expire_date"]);
    $ne = date("Y-m-d", $we);
    if ($_POST["type"] == "0") {
        $line = $_POST["dns"] . "/get.php?username=" . $_POST["username"] . "&password=" . $_POST["password"] . "&type=m3u_plus&output=ts";
    } else {
        if (empty($_POST["url"])) {
            $target_dir = "uploadm3u/";
            $target_file = $target_dir . $_POST["title"] . "_" . $ne . ".m3u";
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
            $file_path = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off" ? "https" : "http") . "://" . $_SERVER["HTTP_HOST"] . dirname($_SERVER["PHP_SELF"]) . "/uploadm3u/";
            if (file_exists($target_file)) {
                unlink($target_file);
            }
            $uploadOk = 1;
            if ($imageFileType != "m3u") {
                $uploadOk = 0;
                $erro = " - Erro Type File";
            }
            if ($uploadOk == 0) {
                $msgg = "<div class=\"alert alert-primary\" id=\"flash-msg\"><h4><i class=\"icon fa fa-check\"></i>Sorry, there was an error! Your file was not uploaded" . $erro . ".</h4></div>";
                header("Location: users_create.php?m=" . $msgg);
            } else {
                if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                    $line = "" . $file_path . htmlspecialchars($_POST["title"] . "_" . $ne . ".m3u") . "";
                } else {
                    $msgg = "<div class=\"alert alert-primary\" id=\"flash-msg\"><h4><i class=\"icon fa fa-check\"></i>Sorry, there was an error uploading your file.</h4></div>";
                }
            }
        } else {
            $line = $_POST["url"];
        }
    }
    $address1 = strtoupper($_POST["mac_address"]);
    $db->query("INSERT INTO flixiptv(mac_address,  username, password, expire_date, dns, epg_url, title, url, type) VALUES('" . $address1 . "', '" . $_POST["username"] . "', '" . $_POST["password"] . "', '" . $ne . "', '" . $_POST["dns"] . "', '" . $_POST["epg_url"] . "', '" . $_POST["title"] . "', '" . $line . "', '" . $_POST["type"] . "')");
    
    if (!empty($_POST["mac_del"])) {
        $db->query("DELETE FROM flixdemo WHERE id=" . $_POST["mac_address"]);
    }
    header("Location: users.php");
}
include "includes/header.php";
echo "        <div class=\"container-fluid\">\n\n";
if (isset($_GET["m"])) {
    echo $_GET["m"];
}
echo "          <!-- Page Heading -->\n          <h1 class=\"h3 mb-1 text-gray-800\"> Activate User</h1>\n\n              <!-- Custom codes -->\n                <div class=\"card border-left-primary shadow h-100 card shadow mb-4\">\n                <div class=\"card-header py-3\">\n                <h6 class=\"m-0 font-weight-bold text-primary\"><i class=\"fas fa-user\"></i> User Details</h6>\n                </div>\n                <div class=\"card-body\">\n                        <form method=\"post\" enctype=\"multipart/form-data\">          \n                        <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"mac_address\">\n                                        <strong>Device ID</strong> \n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"hidden\" class=\"form-control text-primary\" name=\"mac_del\" value=\"";
if (isset($_GET["mac"])) {
    echo $_GET["mac"];
}
echo "\" required/>\n                                        <input class=\"form-control mac_address text-primary\" id=\"description\" name=\"mac_address\" placeholder=\"Enter Device ID: 00:11:22:33:44:55\" type=\"text\" maxlength=\"17\" value=\"";
if (isset($_GET["mac"])) {
    echo $_GET["mac"];
}
echo "\" required/>\n                                    </div>\n                                </div>\n  \r\n\r\n<div class=\"form-group\">\n                                    \r\n<div>\n                            <strong> Select Login Mode for DNS: </strong> \r\n                                    <select class=\"select form-control type\" id=\"type\" name=\"type\" >\r\n\t\t\t\t\t\t\t\t\t    \r\n                                        <option value=\"0\" data-value=\"op0\">Use Xtream Codes\r\n</option>\r\n<option value=\"1\" data-value=\"op1\">Use List M3U8\r\n</option>\r\n                          </select>\r\n</div>\n\n  \r\n\r\n        \r\n\r\n</div>\n    \r\n                                <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"title\">\n                                        <strong>Descrição</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"title\" placeholder=\"Ex: Marcelo (tv box)\" id=\"title\" required/>\n                                    </div>\n                                </div>\n                <div class=\"active1\">\n                                <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"username\">\n                                        <strong>Username</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"username\" placeholder=\"Enter Username\" id=\"discription\"/>\n                                    </div>\n                                </div>\n                                <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"password\">\n                                        <strong>Password</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"password\" placeholder=\"Enter Password\" id=\"discription\"/>\n                                    </div>\n                                </div>\n                                 <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"dns\">\n                                        <strong>DNS</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"dns\" placeholder=\"Enter DNS Server\" id=\"discription\" />\n                                    </div>\n                                </div>\n                         </div>\n                     <div class=\"active2\">\n                                 <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"dns\">\n                                        <strong>Url List M3U</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"url\" placeholder=\"Enter Url List M3u\" id=\"discription\" />\n                                    </div>\n                                </div>\n                                <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"file_config\">\n                                        <strong>File m3u Upload</strong>\n                                    </label>\n                                    <div class=\"input-group mb-3\">\n     <div class=\"custom-file\">\r\n<input class=\"custom-file-input\" type=\"file\" class=\"form-control text-primary\" name=\"fileToUpload\" id=\"fileToUpload\" >\r\n\r\n    <label class=\"custom-file-label\" for=\"fileToUpload\">Choose your m3u file</label>\r\n  </div>\r\n\n                                    <div class=\"input-group-prepend\">\n                                        <button class=\"btn btn-danger btn-icon-split\" button type=\"button\" id=\"btn-file-reset\">\n                        <span class=\"icon text-white-50\"><i class=\"fas fa-times\"></i></span><span class=\"text\">Clear</span>\n                        </button>\n                                    </div>\n                                    </div>\n                                </div>\n                            </div>\n                                 <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"epg_url\">\n                                        <strong>EPG Url</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"epg_url\" placeholder=\"Enter EPG Guide\" id=\"epg_url\"/>\n                                    </div>\n                                </div>\n                                <div class=\"form-group \">\n                                    <label class=\"control-label \" for=\"expire_date\">\n                                        <strong>Expiration</strong>\n                                    </label>\n                                    <div class=\"input-group\">\n                                        <input type=\"text\" class=\"form-control text-primary\" name=\"expire_date\" placeholder=\"YYYY-MM-DD\" id=\"datetimepicker\" autocomplete=\"off\"/> \n                                    </div>\n\n                                </div>\n                                <div class=\"form-group\">\n                                    <div>\n                                        <button class=\"btn btn-success btn-icon-split\" name=\"submit\" type=\"submit\">\n                        <span class=\"icon text-white-50\"><i class=\"fas fa-check\"></i></span><span class=\"text\">Submit</span>\n                        </button>\n                                    </div>\n\n                                </div>\n                            </form>\n                    </div>\n                </div>\n                </div>\n    <br><br><br>\n";
include "includes/footer.php";
echo "\r\n<script>\r\n//select activecode form\r\n//var response = {};\r\n//response.val = \"op2\";\r\n//\$(\"#codemode option[data-value='\" + response.val +\"']\").attr(\"selected\",\"selected\");\r\n\r\n//hide activecode form\r\n\$('.active1').show(); \r\n\$('.active2').hide(); \r\n\r\n//Show/hide activecode select\r\n\$(document).ready(function(){\r\n  \$('.type').change(function(){\r\n    if(\$('.type').val() < 1) {\r\n      \$('.active1').show(); \r\n      \$('.active2').hide(); \r\n    } else {\r\n      \$('.active2').show(); \r\n      \$('.active1').hide(); \r\n    } \r\n  });\r\n});\r\n</script>\r\n\r\n\n    <script>\n\$('#confirm-delete').on('show.bs.modal', function(e) {\n    \$(this).find('.btn-ok').attr('href', \$(e.relatedTarget).data('href'));\n});\n\r\nvar es = document.forms[0].elements;\r\nes[1].onclick = function(){\r\n  clearInputFile(es[0]);\r\n};\r\n\r\n    </script>\r\n    <script type=\"text/javascript\">\r\n\$(document).ready(function() {\r\n   \$('#btn-file-reset').on('click', function(e) {\r\n      var \$el = \$('#fileToUpload');\r\n      \$el.wrap('<form>').closest('form').get(0).reset();\r\n      \$el.unwrap();\r\n   });\r\n});\r\n\r\n    </script>\r\n    <script type=\"text/javascript\">\r\n// @require http://code.jquery.com/jquery-latest.js\r\n// ==/UserScript==\r\ndocument.getElementById(\"description\").addEventListener('keyup', function() { \r\n  var mac = document.getElementById('description').value;\r\n  var macs = mac.split(':').join('');\r\n  macs = chunk(macs, 2).join(':');\r\n  document.getElementById('description').value = macs.toString();\r\n});\r\n\r\nfunction chunk(str, n) {\r\n    var ret = [];\r\n    var i;\r\n    var len;\r\n\r\n    for(i = 0, len = str.length; i < len; i += n) {\r\n       ret.push(str.substr(i, n))\r\n    }\r\n\r\n    return ret\r\n};\r\n    </script>\n</body>\n\n</html>";

?>