<?php

function job_order_sub_jobs($jo_id) {
    global $path_to_root, $Ajax;
    
    $jo_hd_row = get_job_order_record($jo_id);
        if ($jo_hd_row['parent_jo_id'] > 0) {
            display_note('Sub job cannot create any sub job.', 2, 2, 'style=color:red');
            
            return;
        }
    
    $Ajax->activate('job_order_sub_jobs');
    div_start('job_order_sub_jobs');
        display_job_order_sub_jobs($jo_id);

        start_table();
            hyperlink_params_separate($path_to_root . "/services/sub_job_order_entry.php", 'Create a New Sub Job', "p_jo_id=$jo_id");
        end_table();
    div_end();
}

function display_job_order_sub_jobs($jo_id, $width=99, $from_jos_view=false) {
    global $path_to_root, $approval_statuses;
    
    if ($from_jos_view === false) {
        display_heading("Sub Job Card Details");    
    } else {
        display_heading2("Sub Job Card Details");        br();  
    }
    
	start_table(TABLESTYLE, "width=$width%");
        
            if ($from_jos_view === false) {
                $th = array(_("Sub Job #"), _("Reference"), _("Date"), _("Department"), _("Section"), _("Current Owner"), _("Sectional Engineer"), _("Technical Officer"), 
                            _("Job Aacceptance"), _("Work Complete"), _("Approval Status"), _(""));
            } else {
                $th = array(_("Sub Job #"), _("Reference"), _("Date"), _("Department"), _("Section"), _("Current Owner"), _("Sectional Engineer"), _("Technical Officer"), 
                            _("Job Aacceptance"), _("Work Complete"), _("Approval Status"));
            }
            table_header($th);
            
            $k = 0;  
            
            $result = get_job_order_details_by_filters(parent_jo_id: $jo_id);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);

                        $sub_jo_id = $row['id'];
                        label_cell(get_service_trans_view_str(ST_JOB_ORDER, $sub_jo_id), 'align=center');
			label_cell($row['jo_ref']);
			label_cell(sql2date($row['date_']), 'align=center');
			label_cell($row['department_name']);
			label_cell($row['section_name']);
			label_cell(get_employee_formatted_long_name($row['next_job_owner_id']));
			label_cell(get_employee_formatted_long_name($row['engineer_code']));
			label_cell(get_employee_formatted_long_name($row['tech_officer_code']));
			label_cell($row['job_accepted'] == 1 ? 'Yes' : 'No', 'align=center');
			label_cell($row['job_completed'] == 1 ? 'Yes' : 'No', 'align=center');
			label_cell($approval_statuses[$row['approval_status']], 'align=center');
                        
                        if ($from_jos_view === false) {
                            $link = "<a target='_blank' href='$path_to_root/services/sub_job_order_entry.php?trans_no=$sub_jo_id'>Edit</a>";
                            label_cell($link, 'align=center');
                        }
                        
                    end_row();
                }

    end_table(2);
}

?>