<?php
include_once("db.php");
// $typePage = "getData";
// $idTypePage = 3;
// $page = "getPowerEmp";

$typePage = $_POST['type'];
// $typePage = "getPowers";

if (isset($_POST['idTypePage'])) {
    $idTypePage = $_POST['idTypePage'];

}

if($typePage == "getDataCompany"){
    $txt = "select * from `users` left join `setting_print` on `users`.`id_user` = `setting_print`.`id_user` where `users`.`id_user` = '$idPublicUser'";
    $q = mysqli_query($db, $txt);
    $row = mysqli_fetch_assoc($q);
    $data[] = $row;


}
if ($typePage == "getDateToday") {
    $dateToday = date('Y-m-d');
    $data['dateToday'] = $dateToday;
}
if ($typePage == "getCountEmp") {
    $userename = $_POST['page'];
    $dataArray = $_POST['dataArray'];
    $txt = "select * from `emp` where `username` = '$userename' and `id_user` = '$idPublicUser' and `active_emp` ='0'";
    $q = mysqli_query($db, $txt);
    $num = mysqli_num_rows($q);
    $data['num'] = $num;
}
if ($typePage == "addPrivate") {
    $dataArray = $_POST['dataArray'];
    $txt = "insert into `private_subscription` (`id_subscription`,`paid`,`count`,`date_add`,`description`,`id_emp`,`id_user`)
    values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$dataArray[4]','$idPublicUser')
    ";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "getData") {
    $page = $_POST['page'];
    if($page == "getTypePaid"){
        $txt = "select * from `type_paids` where `id_type_paids` = '$idTypePage'";
    }
    if ($page == "getJob") {
        $txt = "select * from `job` where `id_job` = '$idTypePage'";
    }
    if($page == "getCus"){
        $txt = "select * from `cus` where `id_cus` = '$idTypePage'";
    }
    if ($page == "geCatService") {
        $txt = "select * from `cat_service` where `id_cat_service` = '$idTypePage'";
    }
    if ($page == "getService") {
        $txt = "select * from `service` where `id_service` = '$idTypePage'";
    }
    if ($page == "getCat") {
        $txt = "select * from `cat` where `id_cat` = '$idTypePage'";
    }
    if ($page == "getExp") {
        $txt = "select * from `exp` where `id_exp` = '$idTypePage'";
    }
    if ($page == "getEmp") {
        $txt = "select * from `emp` where `id_emp` = '$idTypePage'";
    }
    if ($page == "getInp") {
        $txt = "select * from `inp` where `id_inp` = '$idTypePage'";
    }
    if ($page == "getSubject") {
        $txt = "select * from `subject` where `id_subject` = '$idTypePage'";
    }
    if ($page == "getTeacher") {
        $txt = "select * from `teacher` where `id_teacher` = '$idTypePage'";
    }
    if ($page == "getAddationSubscription") {
        $txt = "select * from `date_addation_subscription` where `id_date_addation_subscription` = '$idTypePage'";
    }
    if ($page == "getSubscription") {
        $txt = "select * from `subscription` where `id_subscription` = '$idTypePage'";
    }
    if ($page == "getSubscriptionFromStudent") {
        $txt = "select * from `subscription` where `id_subscription` = '$idTypePage'";
    }
    if ($page == "getStudent") {
        $txt = "select * from `student` where `id_student` = '$idTypePage'";
    }
    if ($page == "getSubscriptionToStudent") {
        $txt = "select * from `subscription`
        left join `date_addation_subscription` on `date_addation_subscription`.`id_date_addation_subscription` = `subscription`.`id_date_addation_subscription`
        where `subscription`.`id_subscription` = '$idTypePage'";
    }

    if ($page == "getSubscriptionByDay") {
        $days = [
            "Saturday" => "day1",
            "Sunday" => "day2",
            "Monday" => "day3",
            "Tuesday" => "day4",
            "Wednesday" => "day5",
            "Thursday" => "day6",
            "Friday" => "day7"
        ];

        $today = date("l");
        if ($idTypePage == 0) {
            $txt = "select * from `subscription` where `$days[$today]` = 1 and `active_subscription` = 0";
        } else {
            $txt = "select * from `subscription` where `$days[$today]` = 1 and `active_subscription` = 0 and `id_year` = '$idTypePage'";
        }
        //$data['a'] = $txt;
    }
    if ($page == "getCatService") {
        $txt = "select * from `cat_service` where `active_cat_service` = 0 and `id_user` = '$idPublicUser'";

    }
    ////////////////////////

    if ($page == "getPowerEmp") {
        $txt = "select * from `powers` where `id_emp` = '$idTypePage' and `id_user` = '$idPublicUser'";
    }

    $query = mysqli_query($db, $txt);

    while ($row = mysqli_fetch_assoc($query)) {
        $data[] = $row;
    }
}
if ($typePage == "sendPayment") {
    $dataArray = $_POST['dataArray'];
    $txt = "insert into `payment` (`id_subscription_student`,`stay`,`all_mony`,`paid`,`id_emp`,`id_user`)
    values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$idPublicUser')
    ";
    $query = mysqli_query($db, $txt);

    $txtUpdateMoney = "update `subscription_student` set `price` = '$dataArray[0]' where `id_subscription_student` = '$idTypePage'";
    $query = mysqli_query($db, $txtUpdateMoney);

    $data['a'] = 1;
}

if ($typePage == "sendDiscount") {
    $dataArray = $_POST['dataArray'];
    $txt = "insert into `discount` (`id_subscription_student`,`money_stay`,`money_discount`,`id_emp`,`id_student`,`id_user`)
    values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$idPublicUser')
    ";
    $query = mysqli_query($db, $txt);

    $txtUpdateMoney = "update `subscription_student` set `price` = '$dataArray[0]' where `id_subscription_student` = '$idTypePage'";
    $query = mysqli_query($db, $txtUpdateMoney);

    $data['a'] = 1;
}
if ($typePage == "getDataSelect") {
    $page = $_POST['page'];
    if ($page == "getEmp") {
        $txt = "select * from `emp` where `active_emp` = 0 and   `id_user` = '$idPublicUser'";
    }
    if($page == "getService"){
        $txt = "select * from `service` where `active_service` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getProNoIDcat") {
        $txt = "select * from `pro` where `active_pro` = '0' and  `id_user` = '$idPublicUser'";
    }
    if ($page == "getSubscriptionNoIDyear") {
        $txt = "select * from `subscription` where `active_subscription` = '0' and `id_user` = '$idPublicUser' ";

    }
    if ($page == "getTeacherNoID") {
        $txt = "select * from `teacher` where `active_teacher` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getSubscriptionWithIDyear") {
        $txt = "select * from `subscription` where `active_subscription` = '0' and `id_user` = '$idPublicUser' and `id_year` = '$idTypePage' ";
        //echo $txt;
    }
    if ($page == "getProWithIDCat") {
        $txt = "select * from `pro` where `active_pro` = '0' and `id_user` = '$idPublicUser' and `id_cat` = '$idTypePage' ";
        //echo $txt;
    }
    if ($page == "getSubscriptionWithIDteacher") {
        $txt = "select * from `subscription` where `active_subscription` = '0' and `id_user` = '$idPublicUser' and `id_teacher` = '$idTypePage' ";
        //echo $txt;
    }
    if ($page == "getSubscription") {
        $txtGetYear = "select id_year from `student` where `id_student` = '$idTypePage'";
        $qGetYear = mysqli_query($db, $txtGetYear);
        $rGetYear = mysqli_fetch_assoc($qGetYear);
        $idYear = $rGetYear['id_year'];
        $txt = "select * from `subscription` where `active_subscription` = '0' and `id_user` = '$idPublicUser' and `id_year` = '$idYear' ";
    }
    if ($page == "geCatService") {
        $txt = "select * from `cat_service` where `active_cat_service` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getExp") {
        $txt = "select * from `exp` where `active_exp` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getInp") {
        $txt = "select * from `inp` where `active_inp` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getJob") {
        $txt = "select * from `job` where `active_job` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getCat") {
        $txt = "select * from `cat` where `active_cat` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getSubject") {
        $txt = "select * from `subject` where `active_subject` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getTeacher") {
        $txt = "select * from `teacher` where `active_teacher` = '0' and `id_user` = '$idPublicUser' ";
    }
    if ($page == "getDateAddationSubscription") {
        $txt = "select * from `date_addation_subscription` where `active_date_addation_subscription` = '0' and `id_user` = '$idPublicUser'";
    }

    $query = mysqli_query($db, $txt);
    while ($row = mysqli_fetch_array($query)) {
        $data[] = $row;
    }
}
if($typePage == "delete"){
    $nameTable = $_POST['dataArray'];
    $txt = "update `$nameTable` set `active_$nameTable` = 1 where `id_$nameTable` = '$idTypePage' and `id_user` = '$idPublicUser' ";
    $query = mysqli_query($db, $txt);
    $data['a'] = $txt ;
}
if($typePage == "savePowers"){
    $dataArrayPowers = $_POST['dataArray'];
    $countPowers = 25;
    for($i = 1;$i <= $countPowers;$i++){
        $typePower = $dataArrayPowers[$i - 1];
        $txt = "update `powers` set `type_power` = '$typePower' where `id_emp` = '$idTypePage' and `power` = '$i'";
        $q = mysqli_query($db, $txt);
    }
    $data['a'] = 1;
}
if ($typePage == "addStockPro") {
    $dataArray = $_POST['dataArray'];
    $txt = "insert into `in_pro` (`id_pro`,`count`,`notes`,`id_emp`,`id_user`)
    values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$idPublicUser')
    ";
    $q = mysqli_query($db, $txt);
    $txt = "update `pro` set `count_pro` =`count_pro` + $dataArray[0] where `id_pro` = '$idTypePage' ";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addOutStockPro") {
    $dataArray = $_POST['dataArray'];
    $txt = "insert into `sales` (`id_pro`,`count`,`price`,`total_before_price`,`discount`,`total_after_price`,`note`,`id_emp`,`id_user`)
    values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$dataArray[4]','$dataArray[5]','$dataArray[6]','$idPublicUser')
    ";
    $q = mysqli_query($db, $txt);
    $txt = "update `pro` set `count_pro` =`count_pro` - $dataArray[0] where `id_pro` = '$idTypePage' ";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;

}
if ($typePage == "addCatService") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `cat_service` (`name_cat_service`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `cat_service` set `name_cat_service` = '$nameAr'  where `id_cat_service` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addCus") {
    $dataArray = $_POST['dataArray'];
    if ($idTypePage == 0) {
        $txt = "insert into `cus` (`name_cus`,`phone_cus`,`id_user`) values ('$dataArray[0]','$dataArray[1]','$idPublicUser')";
    } else {
        $txt = "update `cus` set `name_cus` = '$dataArray[0]' , `phone_cus` = '$dataArray[1]',  where `id_user` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addService") {
    $dataArray = $_POST['dataArray'];
    if ($idTypePage == 0) {
        $txt = "insert into `service` (`name_service`,`id_cat_service`,`price_service`,`id_user`) values ('$dataArray[0]','$dataArray[1]','$dataArray[2]','$idPublicUser')";
    } else {
        $txt = "update `service` set `name_service` = '$dataArray[0]' , `id_cat_service` = '$dataArray[1]', `price_service` = '$dataArray[2]'  where `id_service` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addEmp") {
    $dataArray = $_POST['dataArray'];
    if ($idTypePage == 0) {
        $txt = "insert into `emp` (`name_emp`,`phone_emp`,`identy_emp`,`username`,`password`,`id_job`,`id_user`) 
    values ('$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$dataArray[4]','$dataArray[5]','$idPublicUser')";
    } else {
        $txt = "update `emp` set `name_emp` = '$dataArray[0]',`phone_emp` = '$dataArray[1]',`identy_emp` = '$dataArray[2]',`username` = '$dataArray[3]',`password` = '$dataArray[4]',`id_job` = '$dataArray[5]',`id_user` = '$idPublicUser' where `id_emp` = '$idPublicUser'";
    }
    $q = mysqli_query($db, $txt);

    if ($idTypePage == 0) {
        $txt = "select id_emp from `emp` order by `id_emp` DESC limit 1";
        $qTxt = mysqli_query($db, $txt);
        $rTxt = mysqli_fetch_assoc($qTxt);
        addPowersEmp($rTxt['id_emp'], $idPublicUser, $db);
    }
    $data['a'] = 1;

}
if ($typePage == "addJob") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `job` (`name_job`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `job` set `name_job` = '$nameAr'  where `id_job` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addCat") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `cat` (`name_cat`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `cat` set `name_cat` = '$nameAr'  where `id_cat` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addExp") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `exp` (`name_exp`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `exp` set `name_exp` = '$nameAr'  where `id_exp` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addTypePaid") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `type_paids` (`name_type_paids`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `type_paids` set `name_type_paids` = '$nameAr'  where `id_type_paids` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addOutExp") {
    $dataArray = $_POST['dataArray'];

    $txt = "insert into `out_exp` (`id_exp`,`id_emp`,`exp`,`notes`,`id_user`) values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$idPublicUser')";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addOutInp") {
    $dataArray = $_POST['dataArray'];

    $txt = "insert into `out_inp` (`id_inp`,`id_emp`,`inp`,`notes`,`id_user`) values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$idPublicUser')";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}

if ($typePage == "addSubject") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `subject` (`name_subject`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `subject` set `name_subject` = '$nameAr'  where `id_subject` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}

if ($typePage == "addTeacher") {
    $dataArray = $_POST['dataArray'];
    $nameAr = $dataArray[0];
    if ($idTypePage == 0) {
        $txt = "insert into `teacher` (`name_teacher`,`id_user`) values ('$nameAr','$idPublicUser')";
    } else {
        $txt = "update `teacher` set `name_teacher` = '$nameAr'  where `id_teacher` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}

if ($typePage == "addAddationSubscription") {
    $dataArray = $_POST['dataArray'];
    $name = $dataArray[0];
    $count = $dataArray[1];
    if ($idTypePage == 0) {
        $txt = "insert into `date_addation_subscription` (`name_date_addation_subscription`,`count_days`,`id_user`) values ('$name','$count','$idPublicUser')";
    } else {
        $txt = "update `date_addation_subscription` set `name_date_addation_subscription` = '$name' , `count_days` = '$count'  where `id_date_addation_subscription` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addSubscription") {
    $dataArray = $_POST['dataArray'];
    $day1 = $dataArray[8][0] ?? 0;
    $day2 = $dataArray[8][1] ?? 0;
    $day3 = $dataArray[8][2] ?? 0;
    $day4 = $dataArray[8][3] ?? 0;
    $day5 = $dataArray[8][4] ?? 0;
    $day6 = $dataArray[8][5] ?? 0;
    $day7 = $dataArray[8][6] ?? 0;
    if ($idTypePage == 0) {

        $txt = "insert into `subscription` (`name_subscription`,`id_year`,`id_subject`,`id_teacher`,`id_date_addation_subscription`,`tax_teacher`,`count_sec`,`price_subscription`,`net_subscription_price`,`session_price`,`price_teacher`,`net_session_price`,`price_teacher_session`,`id_user`,`day1`,`day2`,`day3`,`day4`,`day5`,`day6`,`day7`) 
        values ('$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$dataArray[4]','$dataArray[5]','$dataArray[6]','$dataArray[7]','$dataArray[9]','$dataArray[10]','$dataArray[11]','$dataArray[12]','$dataArray[13]','$idPublicUser','$day1','$day2','$day3','$day4','$day5','$day6','$day7')";
    } else {
        $txt = "update `subscription` set `name_subscription` = '$dataArray[0]', `id_year` = '$dataArray[1]', `id_subject` = '$dataArray[2]', `id_teacher` = '$dataArray[3]', `id_date_addation_subscription` = '$dataArray[4]', `tax_teacher` = '$dataArray[5]',  `count_sec` = '$dataArray[6]', `price_subscription` = '$dataArray[7]',`net_subscription_price` = '$dataArray[9]',`session_price` = '$dataArray[10]', `price_teacher` = '$dataArray[11]',`net_session_price` = '$dataArray[12]',`price_teacher_session` = '$dataArray[13]', `id_user` = '$idPublicUser', `day1` = '$day1',  `day2` = '$day2', `day3` = '$day3', `day4` = '$day4', `day5` = '$day5', `day6` = '$day6', `day7` = '$day7' where `id_subscription` = '$idTypePage'";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addStudent") {
    $dataArray = $_POST['dataArray'];
    $name = $dataArray[0];
    $phone = $dataArray[1];
    $code = $dataArray[2];
    $born = $dataArray[3];
    $phone_parent = $dataArray[4];
    $addreess = $dataArray[5];
    $gender = $dataArray[6];
    $year = $dataArray[7];
    $idemp = $dataArray[8];
    if ($idTypePage == 0) {
        $txt = "insert into `student` 
        (`name_student`,`phone_student`,`code_student`,`born_student`,`phone_parent_student`,`address_student`,`gender_student`,`id_year`,`id_user`,`id_emp`)
        values 
        ('$name','$phone','$code','$born','$phone_parent','$addreess','$gender','$year','$idPublicUser',$idemp)
        ";
    } else {
        $txt = "update `student` set
        `name_student` = '$name',`phone_student` = '$phone',`code_student` = '$code',`born_student` = '$born',`phone_parent_student` = '$phone_parent',`address_student` = '$addreess',`gender_student` = '$gender',`id_year` = '$year'
        where `id_student` = '$idTypePage'
        ";
    }
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if ($typePage == "addImageStudent") {
    $dataArray = $_POST['dataArray'];
    $txt = "update `student` set `image_student` = '$dataArray[0]' where `id_student` = '$idTypePage'";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}


/////////// addation function ///////////////
if ($typePage == "checkCardStudent") {
    $vaild = false;
    $txt = "select * from `student` where `code_student` = '$idTypePage' and `id_user` = '$idPublicUser'";
    $q = mysqli_query($db, $txt);
    $num = mysqli_num_rows($q);
    if ($num == 1) {
        $vaild = false;
    } else {
        $txtCard = "select * from `validcard` where `card` = '$idTypePage' and `id_user` = '$idPublicUser'";
        $qCard = mysqli_query($db, $txtCard);
        $numCard = mysqli_num_rows($qCard);
        if ($numCard != 0) {
            $vaild = true;
        }
    }
    $data['vaild'] = $vaild;
}
if ($typePage == "cancelSubscription") {
    $dataArray = $_POST['dataArray'];
    $idEmp = $dataArray[0];
    $idSunscription = $dataArray[1];
    $notes = $dataArray[2];
    $txt = "update `subscription_student` set `active_subscription_student` = 1 where `id_subscription_student` = '$idSunscription'";
    $q = mysqli_query($db, $txt);

    $txtAddCancelData = "insert into `cancel_subscription_student` (`id_subscription_student`,`id_emp`,`id_user`,`notes`) values ('$idSunscription','$idEmp','$idPublicUser','$notes')";
    $q = mysqli_query($db, $txtAddCancelData);

    $data['a'] = 1;

}
if ($typePage == "getDataIndebtednessSubscriptionUsingStudent") {
    $dateToday = date('Y-m-d');
    $txtCount = "select * from subscription_student
    left join `subscription` on `subscription`.`id_subscription` = `subscription_student`.`id_subscription`
    where  `subscription_student`.`id_student` = '$idTypePage' and `subscription_student`.`active_subscription_student` = '0' and `subscription_student`.`price` > 0  ";
    $qCount = mysqli_query($db, $txtCount);
    $data['txt'] = $txtCount;
    $numCount = mysqli_num_rows($qCount);

    if ($numCount > 0) {
        while ($row = mysqli_fetch_assoc($qCount)) {
            $data['subscription'][] = $row;
        }
    } else {
        $data['subscription'][0] = null;
        $data['txt'] = $txtCount;
    }
}
if ($typePage == "getDataSubscriptionUsingStudent") {
    $dateToday = date('Y-m-d');
    $txtCount = "select * from subscription_student
    left join `subscription` on `subscription`.`id_subscription` = `subscription_student`.`id_subscription`
    where  `subscription_student`.`id_student` = '$idTypePage' and `subscription_student`.`active_subscription_student` = '0' and `subscription_student`.`count_session` > 0 and `subscription_student`.`date_end` >= '$dateToday'  ";
    $qCount = mysqli_query($db, $txtCount);
    $data['txt'] = $txtCount;
    $numCount = mysqli_num_rows($qCount);

    if ($numCount > 0) {
        while ($row = mysqli_fetch_assoc($qCount)) {
            $data['subscription'][] = $row;
        }
    } else {
        $data['subscription'][0] = null;
        $data['txt'] = $txtCount;
    }
}
if ($typePage == "getDataSubscription") {
    $today = date('Y-m-d');
    $idStudent = $_POST['page'];
    $txt = "select * from `subscription` left join `date_addation_subscription` on `date_addation_subscription`.`id_date_addation_subscription` = `subscription`.`id_date_addation_subscription` where `subscription`.`id_subscription` = $idTypePage ";
    $q = mysqli_query($db, $txt);
    $data['subscription'] = mysqli_fetch_assoc($q);

    $txtCount = "select * from subscription_student where `id_subscription` = '$idTypePage' and `id_student` = '$idStudent' and `active_subscription_student` = '0' and `subscription_student`.`count_session` > 0 and `subscription_student`.`date_end` > '$today' ";
    $qCount = mysqli_query($db, $txtCount);
    $numCount = mysqli_num_rows($qCount);
    $data['count'] = $numCount;
    $countDat = $data['subscription']['count_days'];
    $data['endAfterAdd'] = date("Y-m-d", strtotime("+$countDat days"));
    $data['start'] = date('Y-m-d');


}

if ($typePage == "addSubscriptionToStudent") {
    $dataArray = $_POST['dataArray'];
    $txt = "insert into `subscription_student` (`id_student`,`id_subscription`,`type`,`count_session`,`date_start`,`date_end`,`price`,`id_emp`,`id_user`) 
     values ('$idTypePage','$dataArray[0]','$dataArray[1]','$dataArray[2]','$dataArray[3]','$dataArray[4]','$dataArray[5]','$dataArray[6]','$idPublicUser')";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;
}
if($typePage == "getPowers"){
     $id = $_POST['id'];

    $txtPowers = "select type_power from `powers` where `id_emp` = '$id' order by power ASC ";
            $qPowers = mysqli_query($db,$txtPowers);
            $i = 1;
            while($rowPowers = mysqli_fetch_assoc($qPowers)){
                $data[] =$rowPowers['type_power'];
            }
}

function addPowersEmp($lastId, $idPublicUser, $db)
{
    $countPowers = 18;
    for ($i = 1; $i <= $countPowers; $i++) {
        $txt = "insert into `powers` (`id_emp`,`power`,`id_user`) VALUES  ('$lastId','$i','$idPublicUser')";
        $q = mysqli_query($db, $txt);
    }
    $data['a'] = 1;

}
echo json_encode($data);
?>