<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-01-24
 * Time: 19:40
 */

session_start();

require_once getcwd() . "/../functions/db.class.php";
require_once getcwd() . "/../functions/form.data.php";
require_once getcwd() . "/../functions/robot.php";
require_once getcwd() . "/../functions/config.php";


if (isset($_POST["patientName"])) {

    $query = new db();
    /*
     * adding other information to the array
     */
    $_POST["patientUId"] = genPatientUId();
    $_POST["userId"] = $_SESSION["YUZA_ID"];

    //selecting the service type id
    $service_type =
        $conn->query("select * from udut_services_types where typeName = '".$_POST["typeId"]."' and serviceId = '".$_POST["serviceId"]."' ")
        ->fetch_assoc() or die("Error fetching services type ".$conn->error);


    /*
     * assigning value for adding service
     */
    $_POST['typeId'] = $service_type["typeId"];
    $serviceId = $_POST['serviceId'];
    $typeId = $_POST['typeId'];
    $paymentTypeId = $_POST['paymentTypeId'];
    $paymentStatusId = $_POST['paymentStatusId'];

    /*
     * unset unrelated array content
     */
    unset($_POST['typeId']);
    unset($_POST['serviceId']);
    unset($_POST['paymentTypeId']);
    unset($_POST['paymentStatusId']);

    //creating SQL statement from file
    $SQLPatients = SQLStatement("udut_patients", $_POST);

    /*
     * Saving the patient details
     */
    $conn->query($SQLPatients) or die("Problem saving patient details ".$conn->error);

    /*
     * detect the recent patient details
     */
    $patient = $conn->query("select * from udut_patients where patientUId = '" . $_POST["patientUId"] . "' ")
        ->fetch_assoc() or die("Problem selecting patients details ".$conn->error);




    //generating array for payments and visits
    $payments = array(
        "typeId" => $typeId,
        "serviceId" => $serviceId,
        "paymentTypeId" => $paymentTypeId,
        "paymentStatusId" => $paymentStatusId,
        "userId" => $_SESSION["YUZA_ID"],
        "receiptNo" => genReceiptNo(),
        "patientId" => $patient['patientId']
    );

    $SQLPayments = SQLStatement("udut_payments", $payments);

    // Saving the payment details
    $conn->query($SQLPayments) or die("Problem saving payment details ".$conn->error);

    //selecting payment details
    $payment = $conn->query("SELECT * FROM udut_payments ORDER BY paymentId DESC LIMIT 1")->fetch_assoc();
    $paymentId = $payment['paymentId'];

    $visit = array(
        "paymentId" => $paymentId,
        "patientId" => $patient["patientId"]
    );

    $SQLVisits = SQLStatement("udut_visits", $visit);
    /*
    * Saving the visit details
    */
    $conn->query($SQLVisits) or die("Problem saving visit details ".$conn->error);


    //moving referral letter to referral folder
    if(empty($_FILES)){
        $PatientReferrals = move_files($_FILES, "patientReferralLetter", "../referral/");

        /*
         * setting students array
         * normalize the array data and fetch sql statement
         */

        $Referral = array("patientId" => $patient['patientId'], "referralDoc" => $PatientReferrals);
        $SQLPatientReferrals = SQLStatement("udut_patients_referral_docs", $Referral);

        if (!$conn->query($SQLPatientReferrals)) {
            echo "Problem Saving referral Paper <br>";
        }
    }


    header("location: ../about-patient/" . $patient['patientId']);

} else {
    header("location: ../add-patient");
}