<?php
include("db.php");
$typePage = $_POST['typePage'];
if ($typePage == "subscripStudentInRev") {
    $idStudent = $_POST['idStudent'];
    $idSubscription = $_POST['idSubscription'];
    $idEmp = $_POST['id_emp'];
    $type = $_POST['type'];
    $dateToday = date('Y-m-d');
    $txtGetSubscription = "select * from `subscription` where `id_subscription` = '$idSubscription'";
    $q = mysqli_query($db, $txtGetSubscription);
    $rowSubscription = mysqli_fetch_assoc($q);
    $idTeacher = $rowSubscription['id_teacher'];
    $priceSeccion = $rowSubscription['session_price'];
    if ($type == 0) {
        $price = $rowSubscription['session_price'];
    }
    if ($type == 1) {
        $price = 0;
    }
    $txtAddSubscription = "insert into `subscription_student` 
        (`id_student`,`id_subscription`,`id_user`,`id_emp`,`price`,`date_start`,`date_end`)
values  ('$idStudent','$idSubscription','$idPublicUser','$idEmp','$price','$dateToday','$dateToday')";
    $q = mysqli_query($db, $txtAddSubscription);

    $getLastId = mysqli_query($db, "SELECT id_subscription_student FROM subscription_student ORDER BY id_subscription_student DESC LIMIT 1");
    $row = mysqli_fetch_assoc($getLastId);
    $lastId = $row['id_subscription_student'];

    if ($type == 1) {
        $txtAddPriceToSafe = "insert into `payment` 
        (`id_subscription_student`,`id_emp`,`paid`,`stay`,`all_mony`,`id_user`) VALUES
        ('$lastId','$idEmp','$priceSeccion','0','$priceSeccion','$idPublicUser')
        ";
        $q = mysqli_query($db, $txtAddPriceToSafe);
    }
    $txtAddRev = "insert into `rev` (`id_student`,`id_subscription_student`,`count_after_rev`,`price_rev`,`id_teacher`,`id_emp`,`id_user`)
                values ('$idStudent','$lastId','0','$priceSeccion','$idTeacher','$idEmp','$idPublicUser')
                ";
    $q = mysqli_query($db, $txtAddRev);
}
if ($typePage == "getDataStudentYear") {
    $idStudent = $_POST['idStudent'];
    $idSubscription = $_POST['idSubscription'];
    $txt = "select `subscription`.`id_year` from `subscription` where `id_subscription` = '$idSubscription '";
    $q = mysqli_query($db, $txt);
    $row = mysqli_fetch_assoc($q);
    $idYearSubscription = $row['id_year'];
    $txt = "select `student`.`id_year` from `student` where `id_student` = '$idStudent' ";
    $q = mysqli_query($db, $txt);
    $row = mysqli_fetch_assoc($q);
    $idYearStudent = $row['id_year'];
    if ($idYearStudent == $idYearSubscription) {
        $data['a'] = 1;
    } else {
        $data['a'] = 0;
    }
}
if ($typePage == "revUsingBarcode") {
    $idEmp = $_POST['id_emp'];
    $code = $_POST['code'];
    $idSubscrition = $_POST['subscription'];
    $txt = "select * from `student` where `code_student` = '$code'";
    $q = mysqli_query($db, $txt);
    $num = mysqli_num_rows($q);
    if ($num == 0) {
        $data['checkID'] = 0;
    } else {


        $data['checkID'] = 1;
        $data['student'] = mysqli_fetch_assoc($q);
        $idStudent = $data['student']['id_student'];
        $data['idStudent'] = $idStudent;
        $txt = "select * from `subscription_student`
        left join `subscription` on `subscription`.`id_subscription` = `subscription_student`.`id_subscription`
        where `subscription_student`.`id_student` = '$idStudent' and `subscription_student`.`id_subscription` = '$idSubscrition' and `subscription_student`.`active_subscription_student` = 0 order by `subscription_student`.`id_subscription_student` DESC ";
        $data['txt'] = $txt;
        $q = mysqli_query($db, $txt);
        $num = mysqli_num_rows($q);
        $row = mysqli_fetch_assoc($q);

        if ($num == 0) {

            $data['numSubscription'] = 0;
        } else {
            $data['numSubscription'] = 1;


            if ($row['count_session'] > 0 && $row['date_end'] > date('Y-m-d')) {
                $data['successRev'] = 1;
                $data['rev'] = $row;
                $idSubscriptionStudent = $row['id_subscription_student'];


                $txt = "select * from `subscription_student` left join `subscription` on `subscription`.`id_subscription` = `subscription_student`.`id_subscription` where `subscription_student`.`id_subscription_student` = '$idSubscriptionStudent'";
                $q = mysqli_query($db, $txt);
                $row = mysqli_fetch_assoc($q);
                $idStudent = $row['id_student'];
                $countSession = $row['count_session'];
                $sessionPrice = $row['session_price'];
                $idTeacher = $row['id_teacher'];
                $id_subscription_student = $row['id_subscription_student'];
                $today = date('Y-m-d');

                $txt = "select * from `rev` where `id_student` = '$idStudent' and `id_subscription_student` = '$id_subscription_student' and `date_add` = '$today'";
                $data['txtRev'] = $txt;
                $q = mysqli_query($db, $txt);
                $num = mysqli_num_rows($q);
                if ($num > 0) {
                    $data['rev_today'] = 0;

                } else {
                    $txt = "update `subscription_student` set `count_session` = `count_session` - 1 where `id_subscription_student` = '$idSubscriptionStudent'";
                    $q = mysqli_query($db, $txt);
                    $data['rev_today'] = 1;
                    $txt = "insert into `rev` (`id_student`,`id_subscription_student`,`count_after_rev`,`price_rev`,`id_teacher`,`id_emp`,`id_user`)
                values ('$idStudent','$idSubscriptionStudent','$countSession','$sessionPrice','$idTeacher','$idEmp','$idPublicUser')
                ";
                    $q = mysqli_query($db, $txt);
                }

            } else {
                $data['successRev'] = 0;
            }
        }
    }
}
if ($typePage == "getRevStudent") {
    $s = $_POST['typeRev'];
    $today = date('Y-m-d');
    if ($s == 0) {

        $txt = "select * from `rev`
        left join `subscription_student` on `subscription_student`.`id_subscription_student` = `rev`.`id_subscription_student`
        left join `student` on `student`.`id_student`  = `rev`.`id_student`
        left join `subscription` on `subscription`.`id_subscription`  = `subscription_student`.`id_subscription`
        where `rev`.`date_add` = '$today'
        ";

    } else {
        $txt = "select * from `rev`
        left join `subscription_student` on `subscription_student`.`id_subscription_student` = `rev`.`id_subscription_student`
        left join `student` on `student`.`id_student`  = `rev`.`id_student`
                left join `subscription` on `subscription`.`id_subscription`  = `subscription_student`.`id_subscription`

        where `rev`.`date_add` = '$today' and `subscription`.`id_subscription` = '$s'
        ";

    }

    $query = mysqli_query($db, $txt);
    while ($row = mysqli_fetch_assoc($query)) {
        $data[] = $row;
    }
}

echo json_encode($data);
?>