<?php 

function sfm_student_profile_invoice_link() {
	?>
	<a class="nav-item nav-link" id="custom-nav-contact-tab" data-toggle="tab" href="#s-fees" role="tab" aria-controls="custom-nav-contact" aria-selected="false">School Fees Payment</a>
	<?php
}

function sfm_student_profile_invoice($args) {
	$student_meta = $args['student'];
	?>
	<div class="tab-pane fade" id="s-fees" role="tabpanel" aria-labelledby="custom-nav-home-tab">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
        <?php
        	render_page_content("sfm-pay-school-fees", ['id' => $student_meta['id']]);
         ?>
                </div>
            </div>
        </div>
    </div>
	<?php
}

function sfm_payment_registration_success($args) {
    $invoice = new pm_invoice($args['user_id']);
    $invoice->get_invoice_by_number($args['in_ref']);
    $school_fees = get_option("sfm_school_fees");
    $perc = ($option = get_option('perc_school_fees_partpayment')) ? $option : 50;
    $perc = 100 - $perc;
    if($school_fees && is_array($invoice->items)) {
        if(_count($invoice->items)) {
            $post = new post;
            $user = new user;
            $bal = $invoice->total(true);
            foreach($invoice->items as $item) {
                $fee_meta = $post->get_post_meta($item['feeid']);
                if($fee_meta['description'] == $school_fees) {
                    $check_amount = ($perc/100) * $fee_meta['amount'];
                    if($bal <= 0) {
                        $user->update_user_meta($args['user_id'], ['debt_status' => 'not in debt']);
                    }else if($bal <= $check_amount) {
                        $user->update_user_meta($args['user_id'], ['debt_status' => 'part payment']);
                    }
                }
            }
        }
    }
}

function sfm_settings() {
    $perc_school_fees_partpayment = ($option = get_option('perc_school_fees_partpayment')) ? $option : 50;
    input(['label' => 'Percentage school fees for part payment', 'id' => 'perc_school_fees_partpayment', 'value' => $perc_school_fees_partpayment, 'class' => 'form-control numbers-only']);
}

function save_sfm_settings($args) {
    update_option("perc_school_fees_partpayment", $args["perc_school_fees_partpayment"]);
}

function sfm_users_filter_after() {
    $all_descriptions = get_select_options_from_post(['type' => 'sfm_description', 'status' => 'active'], 'description');
    if(current_user_can("manage_students_in_class")) :
        ?>
        <div class="col-md-3">
            <?php 
                select(['label' => 'Fee Status <small>('.get_term_name(get_option('current_academic_term')).')</small>', 'id' => 'description', 'hint' => '<a href="manage-students-in-class" class="" target="_blank" style="font-size:11px !important; border-radius: 4px;">Check for other terms</a>'], $all_descriptions);
             ?>
        </div>
        <?php
    endif;
}

function sfm_student_profile_more($student_meta) {
    // if(current_user_can("sfm_change_debt_status")) {
    //     $debt_status = [
    //         "Cleared" => "not in debt",
    //         "Partly Cleared" => "part payment",
    //         "In debt" => "in debt"
    //     ];
    //     select(['label' => 'Clearance Status', 'id' => 'debt_status'], $debt_status, $student_meta['debt_status']);
    // }
}

function sfm_fee_description() {
    $all_descriptions = get_select_options_from_post(['type' => 'sfm_description', 'status' => 'active'], 'description');
    ?>
    <div class="col-md-3">
        <?php 
            select(['label' => 'Fee Status', 'id' => 'description'], $all_descriptions);
         ?>
    </div>
    <?php
}

function sfm_upload_fees($args) {
    $cron = new cronjobs;
    $args = [$args['file']];
    $cron->schedule("* * * * *", "sfm_update_fees", ...$args);
}