public with sharing class NewRepairPartsController {

	public RepairPart__c repairPart{get;set;}
	public String swoId{get;set;}
	public String repairId{get;set;}
    public Boolean pageClose{get;set;}

    /*public NewRepairPartsController(ApexPages.StandardController stdController) {
        swoId = System.currentPageReference().getParameters().get('swoId');
    }*/

    public NewRepairPartsController() {
        swoId = System.currentPageReference().getParameters().get('swoId');
        repairId = System.currentPageReference().getParameters().get('id');
    }

    public void init(){
    	if(String.isNotBlank(repairId)){
			Schema.DescribeSobjectResult repairPartType = RepairPart__c.sObjectType.getDescribe();
			Map<String, Schema.SObjectField> repairPart_fields = repairPartType.fields.getMap();
			String soql_repair = 'select ';
			String fields = '';
			for (String field : repairPart_fields.keySet()) {
				if (fields.length() > 0) {
					fields += ', ';
				}
				fields += field;
			}
			soql_repair += fields;
			soql_repair += ' from RepairPart__c where Id = \'' + repairId + '\'';
			repairPart = Database.query(soql_repair);
    	}else{
			repairPart = new RepairPart__c();

            if(String.isNotBlank(swoId)){
                repairPart.SWO_ID__c = swoId;
            }
    	}
    }

    public void save(){
        pageClose = false;
    	upsert repairPart;
        pageClose = true;
        System.debug('id:'+repairPart.Id);
    	repairId = repairPart.Id;
        return;
    }

    public Boolean mastData(){
        Boolean temp = true;
        if(repairPart.Product__c == null){
            repairPart.Product__c.addError('ITEM can not be empty');
            temp = false;
        }
        if(repairPart.QUANTITY__c == null){
            repairPart.QUANTITY__c.addError('QUANTITY can not be empty');
            temp = false;
        }
        if(repairPart.LOCATION__c == null){
            repairPart.LOCATION__c.addError('LOCATION can not be empty');
            temp = false;
        }
        System.debug('temp:'+temp);
        return temp;
    }

    public void deleteAction(){
        pageClose = true;
        if(String.isNotBlank(repairPart.Id)){
            delete repairPart;
            return;
        }else{
            return;
        }
    }
}