<?php

function display_vehicle_selection_parameters() {
    start_table(TABLESTYLE_NOBORDER);
        tool_list_cells(_("Select a Vehicle:"), 'asset_id', null, _('New vehicle'), true, AT_VEHICLE, check_value('show_inactive'));
        submit_cells('Refresh', 'Refresh');
    end_table(1);
}

function display_vehicle_control($asset_id) {
    $asset_description = '-';
    
    $row = get_vehicle_with_asset($asset_id);
        if (isset($row['parent_asset_id']) && $row['parent_asset_id'] > 0) {
            $_POST['id'] = $row["id"];
            $asset_description = $_POST['description'] = $row["asset_description"];
            $_POST['hired'] = $row["hired"];
            $_POST['plate_number'] = $row["plate_number"];
            $_POST['chassis'] = $row["chassis_no"];
            $_POST['owner_details'] = $row["owner_details"];
            $_POST['model'] = $row["model_no"];
            $_POST['engine_no'] = $row["engine_no"];
            $_POST['vehicle_type'] = $row["vehicle_type"];
            $_POST['purchased_status'] = $row["purchased_status"];
            $_POST['cylinder_capacity'] = $row["cylinder_capacity"];
            $_POST['fuel_type'] = $row["fuel_type"];
            $_POST['manufacturer'] = $row["manufacturer"];
            $_POST['country_of_origin'] = $row["country_of_origin"];
            $_POST['year_of_manufacture'] = $row["year_of_manufacture"];
            $_POST['inactive'] = $row["inactive"];
        }
    
    start_outer_table(TABLESTYLE2);
        table_section(1);
            label_row("Description:", $asset_description);
            yesno_list_row("Hired:", 'hired', null, "Yes", "No", true);

            text_row("Plate Number:", 'plate_number', null, 20, 30);
            text_row("Chassis No:", 'chassis', null, 20, 20);
            textarea_row("Owner Details :", 'owner_details', null, 20, 10);
            text_row("Model:", 'model', null, 20, 20);
            text_row("Engine Number:", 'engine_no', null, 20, 20);
            value_list_row("Vehicle Type:", 'vehicle_type', 'VEHICLE_TYPES', null, 'Select', false);

        table_section(2);
            if($_POST['hired'] == '0')
            {
                value_list_row("Purchased Status:", 'purchased_status', 'VEHICLE_PURCHASED_STATUS', null, 'Select', false);
            }
            else
            {
                $_POST['purchased_status'] = 0;
            }

            text_row("Cylinder Capacity:", 'cylinder_capacity', null, 20, 20);
            value_list_row("Fuel Type:", 'fuel_type', 'VEHICLE_FUEL_TYPES', null, 'Select', false);
            value_list_row("Manufacturer:", 'manufacturer', 'VEHICLE_MANUFACTURERS', null, 'Select', false);
            value_list_row("Country of Origin:", 'country', 'COUNTRIES', null, 'Select', false);
            value_list_row("Year of Manufacture:", 'year_manufacture', 'YEAR', null, 'Select', false);
            record_status_list_row(_("status:"), 'inactive');
    end_outer_table(1);

    if (isset($row['asset_id']) && $row['asset_id'] > 0) {
        submit_center_first('Update_V', _("Update Vehicle"), 'Update Vehicle', 'default');   
    } else {        
        submit_center_first('Add_V', _("Add Vehicle"), 'Add Vehicle', 'default');   
    }
    
        submit_center_last('cancel', _("Cancel"), _("Cancel Edition"), 'cancel');
}

function can_process_vehicle($new_rec)
{
    if (! ($_POST["asset_id"] > 0))
    { 
        set_focus('asset_id');
        display_error("Please select a vehicle");
        return false;    
    }  
    
    if ($new_rec === true) {
        $ca_masg = check_selected_vehicle_already_in_system($_POST['asset_id']);
        if($ca_masg['success'] !== true)
        { 
            set_focus('asset_id');
            display_error($ca_masg["message"]);
            return false;    
        } 
    }
    
    if(strlen($_POST['plate_number']) == "")
    { 
        set_focus('plate_number');
        display_error("Plate Number can not be empty");
        return false;    
    }  
    
    if(!($_POST['vehicle_type'] > 0))
    { 
        set_focus('vehicle_type');
        display_error("Please select a vehicle type.");
        return false;    
    } 

    return true;    
} 

function clear_data()
{
    unset($_POST['hired']);
    unset($_POST['plate_number']);
    unset($_POST["chassis"]);
    unset($_POST["owner_details"]);
    unset($_POST["model"]);
    unset($_POST["engine_no"]);
    unset($_POST["vehicle_type"]);
    unset($_POST["purchased_status"]);
    unset($_POST["cylinder_capacity"]);
    unset($_POST["fuel_type"]);
    unset($_POST["manufacturer"]);
    unset($_POST["country"]);
    unset($_POST["year_manufacture"]);
}
    
function create_vehicle(&$Mode)
{         
    if(!can_process_vehicle(true)){
        return;
    }

    $ast_row = get_asset($_POST["asset_id"]);
    
    $num_af = add_vehicle($_POST["asset_id"], $ast_row['description'], $_POST['hired'], $_POST['plate_number'],$_POST["chassis"],$_POST["owner_details"],
            $_POST["model"],$_POST["engine_no"],$_POST["vehicle_type"],$_POST["purchased_status"],$_POST["cylinder_capacity"],$_POST["fuel_type"],
            $_POST["manufacturer"],$_POST["country"],$_POST["year_manufacture"],$_POST["inactive"]);                                
                                  
    if($num_af == 1){
        $Mode = "RESET";
        display_notification("New Vehicle has been added");
        clear_data();
    }
    else{
        display_error("New Vehicle could not be added" );
    }
}

function edit_vehicle(&$Mode)
{       
    if(!can_process_vehicle(false)){
        return;
    }
    
    if($_POST['hired'] == '1')
    {
        $_POST['purchased_status'] = 0;
    }
    
    $ast_row = get_asset($_POST["asset_id"]);
    
    $recs = update_vehicle($_POST["asset_id"], $ast_row['description'],  $_POST['hired'], $_POST['plate_number'],$_POST["chassis"],$_POST["owner_details"],
            $_POST["model"],$_POST["engine_no"],$_POST["vehicle_type"],$_POST["purchased_status"],$_POST["cylinder_capacity"],$_POST["fuel_type"],
            $_POST["manufacturer"],$_POST["country"],$_POST["year_manufacture"],$_POST["inactive"]);                             
                           
    if($recs == 1){
        $Mode = "RESET";
        display_notification("Selected Vehicle has been updated");
    }
    else{
        display_error("Selected Vehicle could not be updated");
    }
}

?>