<?php
require ROOT . "/pages/start.php"; ?>
<style>
    #upload_file {
        display: none;
    }
</style>
<style>
        .btn-outline-primary {
            --mdb-btn-active-bg: #386bc0;
            --mdb-btn-active-color: #fff;
        }

        [class*=btn-outline-] {
            --mdb-btn-border-width: 1px;
        }
</style>
<?php
require ROOT . "/pages/header.php";

_selectRowNoParam(
    "SELECT user_type FROM teacher WHERE tuluv = '1' and id = '" .
        $_SESSION["user_id"] .
        "'",
    $user_type
);

$sumRow = 0;
$sum = 0;
?>

<main id="main" class="main p-3">
    <section class="section">
        <div class="row">
            <div class="col">
                <h3>ҮЙЛДВЭРЧНИЙ ЭВЛЭЛИЙН ХУРААМЖ</h3>
            </div>
        </div>
        <?php include "balance.php"; ?>
        <?php if($user_type == "1") {
            _selectNoParam(
                $stj,
                $coj,
                "SELECT jil FROM `ui_income` GROUP BY jil  ORDER BY jil ASC",
                $loop_jil
            );
        ?>
        <div class="card">
            <div class="card-body">
                <div class="btn-group w-100 mb-4 justify-content-center " role="group">
                    <?php while (_fetch($stj)) { ?>
                    <input type="radio" class="btn-check" id="jil<?=$loop_jil?>" autocomplete="off" name="user_role" value="<?=$loop_jil?>" checked="">
                    <label class="btn btn-outline-primary d-flex align-items-center justify-content-center" for="jil<?=$loop_jil?>">
                        <?=$loop_jil?>
                    </label>
                    <?php } ?>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body" id="inner">
                
            </div>
        </div>
        <?php } else { 
            _selectNoParam(
                $stj,
                $coj,
                "SELECT jil FROM `ui_income` WHERE user_id = '" .
                    $_SESSION["user_id"] .
                    "' GROUP BY jil  ORDER BY jil ASC",
                $loop_jil
            );
            
            _selectNoParam(
                $st,
                $co,
                "SELECT jil, sar, money, tai, user FROM ui_income WHERE user_id = '" .
                    $_SESSION["user_id"] .
                    "'",
                $jil,
                $sar,
                $money,
                $tai,
                $user
            );
            
            $List = [];
            
            while (_fetch($st)) {
                $item = new stdClass();
                $item->jil = $jil;
                $item->sar = $sar;
                $item->money = $money;
                $item->tai = $tai;
                $item->user = $user;
            
                array_push($List, $item);
            }
        ?>
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title"></h5>
                        <div class="row m-3">
                            <table class="table table-bordered table-hover w-100">
                                <tr style="background: #d2f1d3">
                                    <th rowspan = "2" class="text-center">ОН</th>
                                    <th colspan = "12" class="text-center">САР</th>
                                    <th rowspan = "2" class="text-center">ДҮН</th>
                                </tr>
                                
                                <tr style="background: #f1f1d2">
                                    <?php
                                    $dd = 1;
                                    while ($dd < 13) { ?>
                                        <th class="text-center"><?= $dd ?></th>
                                    <?php $dd++;}
                                    ?>
                                </tr>
                                <?php while (_fetch($stj)) { ?>
                                    <tr>
                                        <th class="text-center"><?= $loop_jil ?></th>
                                        <?php
                                        $sumRow = 0;
                                        $dd = 1;
                                        while ($dd < 13) {
                                            $vv = "";
                                            foreach ($List as $el) {
                                                if ($el->jil == $loop_jil && $el->sar == $dd) {
                                                    $vv .= "$el->money ";
                                                    $sumRow += $el->money;
                                                }
                                            }
                                                ?>
                                            <td class="text-center"><?= $vv ?></td>
                                        <?php $dd++;
                                        }
                                        $sum += $sumRow;
                                        ?>
                                        <th class="text-center"><?= $sumRow ?></th>
                                    </tr>
                                <?php } ?>
                                <tr style="background: #d9d9d9">
                                    <th colspan = "13" class="text-center">НИЙТ</th>
                                    <th class="text-center"><?=$sum?></th>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php } ?>
    </section>
</main><!-- End #main -->

<?php require ROOT . "/pages/footer.php"; ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script>
    <?php if($user_type == "1") { ?>
    
    function getJil(jil) {
        $.ajax({
            url: "ajax",
            type: "POST",
            data: {
                mode: "income",
                jil: jil
            },
            error: function(xhr, textStatus, errorThrown) {
                //console.log("Алдаа гарлаа");
            },
            beforeSend: function() {
                //console.log("Түр хүлээнэ үү");
            },
            success: function(data) {
                
            },
            async: true
        });
    }
    
    getJil(<?=$loop_jil?>);
    <? } ?>
</script>
<?php require ROOT . "/pages/end.php";
?>
