<?php
include("db.php");

$type = $_POST['type'];
// $type = "getTypePaids";
if($type == "getAllService"){
    $txt = "select * from `service` where `id_user` = '$idPublicUser' and `active_service` = '0'";
    $q = mysqli_query($db,$txt);
    while($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    echo json_encode($data);
}
if($type == "getCat"){

   $txt = "select * from `cat_service` where `active_cat_service` = '0' and `id_user` = '$idPublicUser'";

   // echo $txt;
    $q = mysqli_query($db,$txt);
    while($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }
    echo json_encode($data);

}
if($type == "getService"){

    $id = $_POST['id'];
    if($id == 0){
        $txt = "select * from `service` where `active_service` = '0' and `id_user` = '$idPublicUser' order by `id_service` desc limit 20";
    }else{
        $txt = "select * from `service` where `active_service` = '0' and `id_user` = '$idPublicUser' and `id_cat_service` = '$id' order by `id_service` desc limit 20";
    }

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

}
if($type == "getPhone")
{
    $phone = $_POST['phone'];
    $txt ="select * from `cus` where `phone_cus` ='$phone'";
    $q = mysqli_query($db,$txt);
    $num = mysqli_num_rows($q);
    if($num > 0){
        $row = mysqli_fetch_assoc($q);
        $data['cus'] =$row ;
    }
    $data['num'] = $num;
     echo json_encode($data);

}
if($type == "sendHeader"){
    $id_cus = $_POST['id_cus'];
    $name_cus = $_POST['name_cus'];
    $phone_cus = $_POST['phone_cus'];
    if($id_cus == 0  || $id_cus == "" || $id_cus == null){
        $txt = "insert into `cus` (`name_cus`,`phone_cus`,`id_user`) values ('$name_cus','$phone_cus','$idPublicUser')  ON DUPLICATE KEY UPDATE name_cus='$name_cus' ";
        $q = mysqli_query($db,$txt);
        $id_cus = mysqli_insert_id($db);
    }
    $total_befor_discount = $_POST['total_befor_discount'];
    $discount = $_POST['discount'];
    $total = $_POST['total'];
    $id_emp = $_POST['id_emp'];
    $counts = $_POST['counts'];
    $typePaid = $_POST['typePaid'];
    $info = $_POST['info'];

    $txt = "insert into `sales_header` (`id_cus`,`total_befor_discount`,`discount`,`total`,`id_emp`,`id_user`,`counts`,`type_paid`,`info`)
    values ('$id_cus','$total_befor_discount','$discount','$total','$id_emp','$idPublicUser','$counts','$typePaid','$info')
    ";
    $query = mysqli_query($db,$txt);
    $data['lastId'] = mysqli_insert_id($db);
    echo json_encode($data);
}
if($type == "sendFooter"){
    $id = $_POST['id'];
    $dataFooter = $_POST['lastedArray'];
    $length = count($dataFooter);
    for($i = 0 ;$i <$length ; $i++){
        $idPro = $dataFooter[$i][0];
        $pricePro = $dataFooter[$i][1];
        $countPro = $dataFooter[$i][2];
        $txt = "insert into `sales_footer` (`id_sales_header`,`id_pro`,`price_pro`,`count_pro`,`id_user`) values ('$id','$idPro','$pricePro','$countPro','$idPublicUser')";
        $q = mysqli_query($db,$txt);
        $data['txt'] = $txt;
    }
    
    echo json_encode($data);
}
if($type == "getInv"){
    $id = $_POST['id'];
    $txt = "
    select * from `sales_footer` 
    left join `sales_header` on `sales_header`.`id_sales_header` = `sales_footer`.`id_sales_header`
    left join `emp` on `emp`.`id_emp` = `sales_header`.`id_emp`
    left join `cus` on `cus`.`id_cus` = `sales_header`.`id_cus`
    left join `service` on `service`.`id_service` = `sales_footer`.`id_pro`
    where `sales_header`.`id_user` = '$idPublicUser' and `sales_footer`.`id_sales_header` = '$id'
    ";
    $q = mysqli_query($db,$txt);
    while($row = mysqli_fetch_assoc($q)){
        $data[] = $row;
    }

    echo json_encode($data);
}

if($type == "deleteInv"){
    $id = $_POST['id'];

    $txt = "update `sales_header` set `active_sales_header` = '1' where `id_sales_header` = '$id' ";
    $q = mysqli_query($db, $txt);
    $data['a'] = 1;

     echo json_encode($data);
}
if($type == "dailyRebort"){
    $date = $_POST['date'];
    $end = $date;
    if(isset($_POST['end'])){
        $end = $_POST['end'];
    }
    $txtExp = "SELECT * FROM  `out_exp` left join `emp` on `emp`.`id_emp` = `out_exp`.`id_emp` left join `exp` on  `exp`.`id_exp` = `out_exp`.`id_exp`  where `out_exp`.`active_out_exp` = '0' and `out_exp`.`id_user` = '$idPublicUser' and `out_exp`.`date_add` BETWEEN '$date' and '$end' ";
    $queryExp = mysqli_query($db,$txtExp);
    $bodyExp = "";
    $totalExp = 0;
    while($rowExp = mysqli_fetch_assoc($queryExp)){
        $totalExp += $rowExp['exp'];
        $bodyExp .= "
        <tr>
            <td>".$rowExp['name_exp']."</td>
            <td>".$rowExp['exp']."</td>
            <td>".$rowExp['date_add']."</td>
            <td>".$rowExp['name_emp']."</td>
        </tr>
        ";
    }

    $txtSales = "SELECT * FROM  `sales_header` 
    left join `emp` on `emp`.`id_emp` = `sales_header`.`id_emp` 
    left join `type_paids` on `type_paids`.`id_type_paids` = `sales_header`.`type_paid`
    left join `cus` on `cus`.`id_cus` = `sales_header`.`id_cus`
    where `sales_header`.`active_sales_header` = '0' and  `sales_header`.`id_user` = '$idPublicUser' and `sales_header`.`date_add` BETWEEN '$date' and '$end' ";
    $querySales = mysqli_query($db,$txtSales);
    $bodySales = "";
    $totalSales = 0;
    $totalDiscountSales = 0;
    $totalBeforeDiscountSales = 0;
    while($rowSales = mysqli_fetch_assoc($querySales)){
        $totalSales += $rowSales['total'];
        $totalDiscountSales += $rowSales['discount'];
        $totalBeforeDiscountSales += $rowSales['total_befor_discount'];
        if($rowSales['type_paid'] == 0){
            $typePaid = "Cash";
        }else{
            $typePaid = $rowSales['name_type_paids'];
        }
        $bodySales .= "
        <tr>
            <td>".$rowSales['id_sales_header']."</td>
            <td>".$rowSales['name_cus']."</td>
            <td>".$typePaid."</td>
            <td>".$rowSales['total_befor_discount']."</td>
            <td>".$rowSales['discount']."</td>
            <td>".$rowSales['total']."</td>
            <td>".$rowSales['info']."</td>
            <td>".$rowSales['time_add']."</td>
            <td>".$rowSales['name_emp']."</td>
        </tr>
        ";
        
    }

    $total = $totalSales - $totalExp;

    $data['bodyExp'] = $bodyExp;
    $data['bodySales'] = $bodySales;

    $data['totalSales'] = $totalSales;
    $data['totalDiscountSales'] = $totalDiscountSales;
    $data['totalBeforeDiscountSales'] = $totalBeforeDiscountSales;

    $data['totalExp'] = $totalExp;


    $data['total'] = $total;

    echo json_encode($data);
}
if($type == "getTypePaids"){
    $txt = "select * from `type_paids` where `active_type_paids` = '0' and `id_user` = '$idPublicUser'";
    $q = mysqli_query($db,$txt);
    $num = mysqli_num_rows($q);
    if($num == 0){
        $data[] = [

                "id_type_paids"    => "0",
                "name_type_paids"  => "Cash",

        ];
    }else{
        while($rows = mysqli_fetch_assoc($q)){
            $data[] = $rows;
        }
    }
    echo json_encode($data);
}
?>