<?php

if (isset($Mode) && $Mode == 'Delete') {
    $msg = handle_delete_job_order_repair($jo_id, $selected_id);
        if ($msg['success'] === false) {
            display_error($msg['message']);
        } else {
            clear_job_order_inputs($selected_id);
            display_notification($msg['message']);
        }
}

function job_order_repairs(&$Mode, &$selected_id, &$jo_id, $state=true) {
    global $Ajax;
    
    if (! ($jo_id > 0)) {
        return;
    }
    
    $Ajax->activate('job_order_repairs');
    div_start('job_order_repairs');
    
        display_job_order_repairs($jo_id, $selected_id, $Mode, $state);
        
    div_end();
}

function display_job_order_repairs($jo_id, $selected_id, &$Mode, $state=true) {
    display_heading("Job Card Repair Details");    

	start_table(TABLESTYLE, "width=80%");
        
            $th = array(_("Id"), _("Repair Description"), _("Created By"), _("Created On"), _("Updated By"), _("Updated On"), _(""), _(""));
            table_header($th);
            
            $k = 0;  
            
            $result = get_job_order_repairs_details($jo_id);
                while ($row = db_fetch($result)) {
                    $id = $row['id'];
                    
                    if ($selected_id != $id || $Mode == 'Delete') {
                        alt_table_row_color($k);

                            label_cell($id, 'align=center');
                            label_cell(nl2br($row['description']));
                            label_cell($row['created_by_name']);
                            label_cell($row['created_on'], 'align=center');
                            label_cell($row['updated_by_name']);
                            if ($row['updated_by'] > 0) {
                                label_cell($row['updated_on'], 'align=center');
                            } else {
                                label_cell('');
                            }
                            edit_button_cell("Edit".$row['id'], _("Edit"));
                            delete_button_cell("Delete".$row['id'], _("Delete"));

                        end_row();
                    } else {
                        display_job_order_repair_controls($Mode, $selected_id, $jo_id, $state);
                    }
            }
            
            if (! ($selected_id > 0) || $Mode == 'Delete') {
                display_job_order_repair_controls($Mode, $selected_id, $jo_id, $state);
            }

    end_table(2);
}

function display_job_order_repair_controls(&$Mode, &$selected_id, $jo_id, $state=true) {
    echo "<td colspan=6>";
        start_outer_table(TABLESTYLE2);
            if ($jo_id > 0 && $Mode != 'Delete' && $selected_id > 0 && $state === true) {
                $jo_repair_row = get_job_order_repair_record($selected_id);
                    if (is_array($jo_repair_row)) {
                        $_POST['description'] = $jo_repair_row['description'];
                    }
            }

            table_section(1);
                if ($selected_id > 0) {
                    label_row('Line #:', $selected_id);
                }
            
            table_section(2);
                textarea_row('Description:', 'description', null, 50, 4);
        end_outer_table(1);
    echo "</td>";
    
        if ($jo_id > 0 && $Mode != 'Delete' && $selected_id > 0) {
            button_cell('UPDATE_JO_REPAIR', _("Update Job Card Repair"), _('Confirm changes'), ICON_UPDATE);
            hidden('selected_id', $selected_id);

        } elseif ($jo_id > 0) {        
            button_cell('ADD_JO_REPAIR', _("Add Job Card Repair"),  _('Confirm changes'), ICON_ADD);
        }

        button_cell("CLEAR_DATA", 'Clear', 'Clear entered data', ICON_CANCEL);
}

function can_save_job_order_repair(&$selected_id) {  
    if ($selected_id > 0) {
        $row = get_job_order_repair_record($selected_id);
            if (! is_array($row)) {
                return array('success' => false, 'message' => "Another user has deleted job card repair record #$selected_id.");
            }
            
        hidden('selected_id', $selected_id);
    }
      
    if (! isset($_POST['description']) || ! (strlen($_POST['description']) > 2)) {
        return array("success" => false, "focus_to" => 'description', "message" => "Repair description cannot empty.");        
    }
        
    return array("success" => true, "message" => "", "focus_to" => '');    
}

function handle_save_job_order_repair(&$jo_id, &$selected_id) {
    $msg = can_save_job_order_repair($selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
        
    begin_transaction();   
        $row_state = 0;
        $hd_msg = handle_save_job_order_header($jo_id, $row_state);   
            if ($hd_msg['success'] === false) {
                return $hd_msg;
            }

        if ($selected_id > 0) {
            $msg = update_job_order_repair($jo_id, $selected_id, $row_state, $_POST['description']);
                if ($msg['success'] === false) {
                    cancel_transaction();
                    
                    return $msg;
                } else {
                    commit_transaction();
                    
                    return array('success' => true, 'message' => "Job card repair record #$selected_id has been updated.");
                }
        } else {
            $msg = add_job_order_repair($jo_id, $row_state, $_POST['description']);
                if ($msg['success'] === false) {
                    cancel_transaction();
                    
                    return $msg;
                } else {
                    commit_transaction();
                    
                    return array('success' => true, 'message' => "New job card repair record #".$msg['id']." has been added.");
                }
        }
}

function can_delete_job_order_repair(&$jo_id, &$selected_id) { 
    $jo_hd_row = get_job_order_record($jo_id);
        if (is_array($jo_hd_row)) {
            if ($jo_hd_row['closed'] == 1) {
                return array('success' => false, 'message' => "Job card #$jo_id is already closed. Not allow to further update.");
            }
            
            $user_id = $_SESSION['wa_current_user']->user;
            // current job owner permission check before saving
            if (strlen($jo_hd_row['next_job_owner_id']) > 0) {
                $user_row = get_user($user_id);
                    if ($user_row['employee_code'] == '') {
                        $system_user = $user_row['real_name'];
                        return array('success' => false, 
                                     'message' => "Loged in system user $system_user is not assigned to any employee in the company. Please contact system administrator.");
                    }
                $owner_row = get_users_by_filters($user_id, 0, $jo_hd_row['next_job_owner_id'], true);
                    if (! is_array($owner_row)) {
                        return array('success' => false, 'message' => "Not allow to update job repair details #$jo_id because you are not the current owner of the job.");
                    }
            }
        } else {
            return array("success" => false, "message" => "Could not find Job Card #$jo_id.");
        }
        
    if ($selected_id > 0) {
        $row = get_job_order_repair_record($selected_id);
            if (! is_array($row)) {
                return array('success' => false, 'message' => "Another user has deleted job card repair record #$selected_id.");
            }
            
        hidden('selected_id', $selected_id);
    }

    return array("success" => true, "message" => "", "focus_to" => '');    
}

function handle_delete_job_order_repair(&$jo_id, &$selected_id) {
    $msg = can_delete_job_order_repair($jo_id, $selected_id);   
        if ($msg['success'] === false) {
            return $msg;
        }
        
        $del_msg = delete_job_order_repair($selected_id);
            if ($del_msg != 1) {
                return array('success' => true, 'message' => "Could not delete job card repair record #$selected_id.");
            } else {
                return array('success' => true, 'message' => "Job card repair record #$selected_id has been deleted.");
            }
}

function view_job_order_repairs($jo_id, $width=80) {
    display_heading2("Job Card Repair Details");    br();

	start_table(TABLESTYLE, "width=$width%");
        
            $th = array(_("Id"), _("Repair Description"), _("Created By"), _("Created On"), _("Updated By"), _("Updated On"));
            table_header($th);
            
            $k = 0;  
            
            $result = get_job_order_repairs_details($jo_id);
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);
			label_cell($row['id'], 'align=center');
			label_cell(nl2br($row['description']));
                        label_cell($row['created_by_name']);
                        label_cell($row['created_on'], 'align=center');
                        label_cell($row['updated_by_name']);
                        if ($row['updated_on'] != INFINITE_START_DATE." 00:00:00") {
                            label_cell($row['updated_on'], 'align=center');
                        } else {
                            label_cell('');
                        }
                    end_row();
            }

    end_table(2);
}

?>