<?php

\session_start();
\ob_start();
require __DIR__ . '/common.php';
use Ministra\Lib\Admin;
use Ministra\Lib\AdminAccess;
use Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e;
$error = '';
\Ministra\Lib\Admin::checkAuth();
\Ministra\Lib\Admin::checkAccess(\Ministra\Lib\AdminAccess::ACCESS_VIEW);
if (@$_GET['archive'] == 1 && @$_GET['id']) {
    \Ministra\Lib\Admin::checkAccess(\Ministra\Lib\AdminAccess::ACCESS_CONTEXT_ACTION);
    $id = (int) @$_GET['id'];
    if (!empty($_GET['to_date'])) {
        $year = \date('Y', \strtotime($_GET['to_date']));
        $month = \date('n', \strtotime($_GET['to_date']));
    } else {
        $year = \date('Y');
        $month = \date('n');
        if ($month == 1) {
            $month = 12;
            --$year;
        } else {
            --$month;
        }
    }
    $archive_id = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->from('tasks_archive')->where(['month' => $month, 'year' => $year])->get()->first('id');
    if (empty($archive_id)) {
        $archive_id = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->insert('tasks_archive', ['date' => 'NOW()', 'year' => $year, 'month' => $month])->insert_id();
    }
    \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->update('moderator_tasks', ['archived' => $archive_id, 'archived_time' => 'NOW()'], ['archived' => 0, 'ended' => 1, 'to_usr' => $id]);
    $archive_id = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->from('karaoke_archive')->where(['month' => $month, 'year' => $year])->get()->first('id');
    if (empty($archive_id)) {
        $archive_id = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->insert('karaoke_archive', ['date' => 'NOW()', 'year' => $year, 'month' => $month])->insert_id();
    }
    \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->update('karaoke', ['archived' => $archive_id, 'archived_time' => 'NOW()'], ['archived' => 0, 'status' => 1, 'accessed' => 1, 'done' => 1]);
    \header('Location: last_closed_tasks.php?id=' . $id);
    exit;
}
?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">

        body {
            font-family: Arial, Helvetica, sans-serif;
            font-weight: bold;
        }

        td {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            text-decoration: none;
            color: #000000;
        }

        .list {
            border-width: 1px;
            border-style: solid;
            border-color: #E5E5E5;
        }

        a {
            color: #0000FF;
            font-weight: bold;
            text-decoration: none;
        }

        a:link, a:visited {
            color: #5588FF;
            font-weight: bold;
        }

        a:hover {
            color: #0000FF;
            font-weight: bold;
            text-decoration: underline;
        }

        a.msgs:hover, a.msgs:visited, a.msgs:link {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #000000;
            font-weight: bold;
            text-decoration: none;
        }
    </style>
    <title><?php 
echo \_('Completed tasks');
?></title>
</head>
<body>
<table align="center" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td align="center" valign="middle" width="100%" bgcolor="#88BBFF">
            <font size="5px" color="White"><b>&nbsp;<?php 
echo \_('Completed tasks');
?>&nbsp;</b></font>
        </td>
    </tr>
    <tr>
        <td width="100%" align="left" valign="bottom">
            <a href="tasks.php"><< <?php 
echo \_('Back');
?></a>
        </td>
    </tr>
    <tr>
        <td align="center">
            <font color="Red">
                <strong>
                    <?php 
echo $error;
?>
                </strong>
            </font>
            <br>
            <br>
        </td>
    </tr>
    <tr>
        <td>

            <table border="0" align="center" width="620">
                <tr>
                    <td>
                        <font color="Gray">* <?php 
echo \_('report generated by closed-month movies');
?></font>
                    </td>
                </tr>
            </table>

        </td>
    </tr>
</table>
<?php 
$where = '';
if (\Ministra\Lib\Admin::isPageActionAllowed()) {
    $uid = @$_GET['id'];
} else {
    if (@$_SESSION['uid'] != @$_GET['id']) {
        $uid = 0;
    } else {
        $uid = $_SESSION['uid'];
    }
}
?>

<table border="0" align="center" width="760">
    <tr>
        <td align="center">

            <center><?php 
echo \_('SD movies');
?></center>
            <table border="1" width="100%" cellspacing="0">
                <tr>
                    <td>#</td>
                    <td><?php 
echo \_('Movie');
?></td>
                    <td><?php 
echo \_('Opening date');
?></td>
                    <td><?php 
echo \_('Closing date');
?></td>
                    <td><?php 
echo \_('Duration, min');
?></td>
                </tr>
                <?php 
$from_time = \date('Y-m-d H:i:s', \strtotime('-1 month'));
$tasks = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->select('video.name, video.time, moderator_tasks.id, start_time, end_time')->from('moderator_tasks')->where(['ended' => 1, 'rejected' => 0, 'archived' => 0, 'to_usr' => $uid, 'hd' => 0])->join('video', 'video.id', 'media_id', 'INNER')->orderby('end_time')->get();
$length = 0;
$total_length = 0;
$num = 0;
while ($arr = $tasks->next()) {
    ++$num;
    $length = $arr['time'];
    $total_length += $length;
    echo '<tr>';
    echo "<td>{$num}</td>";
    echo "<td><a href='msgs.php?task={$arr['id']}'>" . $arr['name'] . '</a></td>';
    echo '<td>' . $arr['start_time'] . '</td>';
    echo '<td>' . $arr['end_time'] . '</td>';
    echo "<td align='right'>" . $length . '</td>';
    echo '</tr>';
}
?>
            </table>
            <table border="0" width="100%">
                <tr>
                    <td width="100%" align="right"> <?php 
echo \_('Total duration, min');
?>:
                        <b><?php 
echo $total_length;
?></b></td>
                </tr>
            </table>
            <br>
            <br>

            <center><?php 
echo \_('HD movies');
?></center>
            <table border="1" width="100%" cellspacing="0">
                <tr>
                    <td>#</td>
                    <td><?php 
echo \_('Movie');
?></td>
                    <td><?php 
echo \_('Opening date');
?></td>
                    <td><?php 
echo \_('Closing date');
?></td>
                    <td><?php 
echo \_('Duration, min');
?></td>
                </tr>
                <?php 
$from_time = \date('Y-m-d H:i:s', \strtotime('-1 month'));
$tasks = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->select('video.name, video.time, moderator_tasks.id, start_time, end_time')->from('moderator_tasks')->where(['ended' => 1, 'rejected' => 0, 'archived' => 0, 'to_usr' => $uid, 'hd' => 1])->join('video', 'video.id', 'media_id', 'INNER')->orderby('end_time')->get();
$length = 0;
$total_length = 0;
$num = 0;
while ($arr = $tasks->next()) {
    ++$num;
    $length = $arr['time'];
    $total_length += $length;
    echo '<tr>';
    echo "<td>{$num}</td>";
    echo "<td><a href='msgs.php?task={$arr['id']}'>" . $arr['name'] . '</a></td>';
    echo '<td>' . $arr['start_time'] . '</td>';
    echo '<td>' . $arr['end_time'] . '</td>';
    echo "<td align='right'>" . $length . '</td>';
    echo '</tr>';
}
?>
            </table>
            <table border="0" width="100%">
                <tr>
                    <td width="100%" align="right"> <?php 
echo \_('Total duration, min');
?>:
                        <b><?php 
echo $total_length;
?></b></td>
                </tr>
            </table>
            <br>
            <br>

            <center><?php 
echo \_('Rejected tasks');
?></center>
            <table border="1" width="100%" cellspacing="0">
                <tr>
                    <td>#</td>
                    <td><?php 
echo \_('Movie');
?></td>
                    <td><?php 
echo \_('Opening date');
?></td>
                    <td><?php 
echo \_('Closing date');
?></td>
                    <td><?php 
echo \_('Duration, min');
?></td>
                </tr>
                <?php 
$tasks = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->from('moderator_tasks')->where(['ended' => 1, 'rejected' => 1, 'archived' => 0, 'to_usr' => $uid])->get();
$num = 0;
while ($arr = $tasks->next()) {
    ++$num;
    $length = \Ministra\OldAdmin\get_media_length_by_id($arr['media_id']);
    echo '<tr>';
    echo "<td>{$num}</td>";
    echo "<td><a href='msgs.php?task={$arr['id']}'>" . \Ministra\OldAdmin\get_media_name_by_id($arr['media_id'], 'Video') . '</a></td>';
    echo '<td>' . $arr['start_time'] . '</td>';
    echo '<td>' . $arr['end_time'] . '</td>';
    echo "<td align='right'>" . $length . '</td>';
    echo '</tr>';
}
?>
            </table>
            <br>
            <br>
            <table border="0" width="100%">
                <tr>
                    <td align="right">
                        <?php 
if (\Ministra\Lib\Admin::isPageActionAllowed()) {
    ?>
                        <form method="get">
                            <input type="hidden" name="id" value="<?php 
    echo @$_GET['id'];
    ?>">
                            <input type="hidden" name="archive" value="1">
                            <select name="to_date">
                                <?php 
    $prev_month = \strtotime('-1 month');
    $archive_slots = [\date('d-m-Y', $prev_month) => \strftime('%B %Y', $prev_month), \date('d-m-Y') => \strftime('%B %Y')];
    foreach ($archive_slots as $key => $month) {
        ?>
                                    <option value="<?php 
        echo $key;
        ?>"><?php 
        echo $month;
        ?></option>
                                <?php 
    }
    ?>
                            </select>
                            <input type="submit"
                                   value="<?php 
    echo \htmlspecialchars(\_('Move to archive'), \ENT_QUOTES);
    ?>"
                                   onclick="if(confirm('<?php 
    echo \_('Move to the archive?');
    ?>')){return true}else{return false}">
                        <?php 
}
?>
                    </td>
                </tr>
            </table>
        <td>
    </tr>
</table>

