<?php 

function school_fees_set_up_cb() {
	global $post_meta;
    $post = new post;
    $all_classes = $post->get_post_by_meta_data(['type' => 'class']);
    $category_mapping = get_option("category_mapping");
    $classes = array();
    if(_count($all_classes)) {
        foreach ($all_classes as $class_meta) {
            if(isset($category_mapping[$class_meta['id']]) && _count($category_mapping[$class_meta['id']])) {
                foreach ($category_mapping[$class_meta['id']] as $key => $category_id) {
                    $classes[get_class_name($class_meta['id'], $category_id)] = $class_meta['id'].','.$category_id;
                }
            }
        }
    }
    $term = (isset($_POST['term'])) ? $_POST['term'] : get_option('current_academic_term');
    $all_terms = get_select_options_from_post_q(['type' => 'academic_term'], 'term_name');
    $gender = ["Both" => "Both", "Male" => "Male", "Female" => "Female"];
    $all_descriptions = get_select_options_from_post(['type' => 'sfm_description', 'status' => 'active'], 'description');
    if(current_user_can('manage_fee_description')) :
        if(is_array($all_descriptions)) {
            $all_descriptions['Add More...'] = 'Add More';
        } else {
            $all_descriptions = ['Add More...' => 'Add More'];
        }
    endif;
	?>
	<div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <strong class="card-title">Termly Fee Form</strong>
            </div>
            <div class="card-body">
                <form method="post" action="contents/plugins/school_fees_manager/ajax.php" id="school_fees_set_up_form" data-callbackid="#display" data-callback="sfm_getTermFees">
                    <div class="col-md-4">
                        <?php select(['label' => 'Select Class', 'id' => 'class', 'name' => 'class[]', 'multiple' => 'multiple', 'class' => 'standardSelect', 'required' => 'required'], $classes); ?>
                    </div>
                    <div class="col-md-4">
                        <?php select(['label' => 'Gender', 'id' => 'gender', 'data-form' => 'search-form', 'required' => 'required'], $gender, "Both"); ?>
                    </div>
                    <div class="col-md-4">
                        <?php select(['label' => 'Term/Session', 'id' => 'pay-fee-term', 'name' => 'term', 'data-form' => 'search-form', 'required' => 'required'], $all_terms, $term); ?>
                    </div>
                	<div class="col-md-12">
                		<table class="table" id="items-table">
		                	<thead>
		                		<tr>
			                		<th>QTY</th>
			                		<th>Description</th>
			                		<th>Deadline</th>
			                		<th>Unit Price</th>
			                		<th>Sub-total</th>
			                		<th>Action</th>
			                	</tr>
		                	</thead>
		                	<tbody></tbody>
		                	<tfoot>
		                		<tr>
		                			<tr>
			      						<td><input id="new-qty" style="max-width: 50px;" type="text" name="new[qty]" class="form-control numbers-only" value="1"></td>
			      						<td><?php select(['label' => '', 'id' => 'new-description', 'name'=>'new[description]'], $all_descriptions); ?></td>
			      						<td><input id="new-deadline" type="date" name="new[deadline]" class="form-control"></td>
			      						<td><input id="new-unit_price" type="text" name="new[unit_price]" class="form-control numbers-only"></td>
			      						<td id="new-qty_price"></td>
			      						<td><button class="btn btn-primary" id="add-btn" disabled>Add</button></td>
			      					</tr>
		                		</tr>
		                	</tfoot>
		                </table>
		                <span><?php submit_btn('school_fees_set_up', 'Submit & Save'); ?></span>
                	</div>
                
                </form>
            </div>
        </div>
    </div>
    <div class="col-md-12">
        <div id="display"></div>
    </div>
	<?php
    do_action('after_apply_fees_form');
    add_page_javascript('app/elements/form.js');
    add_page_javascript('app/assets/js/page-scripts/submit.js');
    add_page_javascript('app/assets/js/page-scripts/js-functions.js');
	add_page_javascript('contents/plugins/school_fees_manager/js/school-fees-set-up.js');
    add_page_javascript('app/assets/js/page-scripts/koweb.js');
}