public with sharing class NewDiagnosisPartController {

    public Diagnosis_Part__c diagnosisPart {get; set;}
    public String swoId {get; set;}
    public String diagnosisPartId {get; set;}

    public String baseUrl { get; set; }
    public String rtUrl { get; set; }

    public Boolean pageClose{ get; set; }
    public Boolean DELIVERED_isCheck{get;set;}
    public Boolean RETURNED_isCheck{get;set;}

    public NewDiagnosisPartController() {
        swoId = System.currentPageReference().getParameters().get('swoId');
        diagnosisPartId = System.currentPageReference().getParameters().get('id');
    }
    public NewDiagnosisPartController(ApexPages.StandardController controller) {
        swoId = System.currentPageReference().getParameters().get('swoId');
        diagnosisPartId = System.currentPageReference().getParameters().get('id');

        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0, path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
    }

    public void init() {
        DELIVERED_isCheck = false;
        RETURNED_isCheck = false;
        if (String.isNotBlank(diagnosisPartId)) {
            Schema.DescribeSobjectResult repairPartType = Diagnosis_Part__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 Diagnosis_Part__c where Id = \'' + diagnosisPartId + '\'';
            diagnosisPart = Database.query(soql_repair);
            DELIVERED_isCheck = diagnosisPart.DELIVERED__c;
            RETURNED_isCheck = diagnosisPart.RETURNED__c;
        } else {
            diagnosisPart = new Diagnosis_Part__c();
        }
        if (String.isNotBlank(swoId)) {
            diagnosisPart.SWO__c = swoId;
        }
    }

    public void save() {
        Boolean isSave = true;
        if(diagnosisPart.DELIVERED__c == false && DELIVERED_isCheck == true){
            diagnosisPart.DELIVERED__c.addError('DELIVERED 不允许取消勾选');
            isSave = false;
        }
        if(diagnosisPart.RETURNED__c == false && RETURNED_isCheck == true){
            diagnosisPart.RETURNED__c.addError('RETURNED 不允许取消勾选');
            isSave = false;
        }
        if(!isSave){
            return;
        }
        String productId = diagnosisPart.Product__c;
        if (String.isNotBlank(productId)) {
            Product2 product = [SELECT Id, Name, Product_ECCode__c, Description FROM Product2 WHERE Id = :productId limit 1];
            diagnosisPart.PART_NUMBER__c = product.Product_ECCode__c;
            diagnosisPart.DESCRIPTION__c = product.Description;
        }
        pageClose = false;

        if (diagnosisPart.DELIVERED__c == false  && String.isBlank(diagnosisPart.Id)) {
            Boolean isUpdateSwo = false;
            List<SWO__c> swoList = [select Id,Diagnose_part_not_delievered_1__c,Diagnose_part_not_delievered_2__c,Diagnose_part_not_delievered_3__c from SWO__c where Id=:diagnosisPart.SWO__c];
            System.debug('swoList.size():'+swoList.size());
            if (swoList!=null && swoList.size()!=0) {
                SWO__c swo = swoList[0];
                if(String.isBlank(swo.Diagnose_part_not_delievered_1__c)){
                    isUpdateSwo = true;
                    swo.Diagnose_part_not_delievered_1__c = diagnosisPart.PART_NUMBER__c;
                }else if(String.isBlank(swo.Diagnose_part_not_delievered_2__c)){ 
                    isUpdateSwo = true;
                    swo.Diagnose_part_not_delievered_2__c = diagnosisPart.PART_NUMBER__c;
                }else if(String.isBlank(swo.Diagnose_part_not_delievered_3__c)){
                    isUpdateSwo = true;
                    swo.Diagnose_part_not_delievered_3__c = diagnosisPart.PART_NUMBER__c;
                }
                System.debug('swo.Id:'+swo.Id);
                if(isUpdateSwo) update swo;
            }
        }

        upsert diagnosisPart;
        pageClose = true;
        System.debug('id:' + diagnosisPart.Id);
        diagnosisPartId = diagnosisPart.Id;
        return;
    }

    public PageReference deleteAction() {
        if (String.isNotBlank(diagnosisPart.Id)) {
            delete diagnosisPart;
        }
        return ReturnSWO();
    }

    public PageReference ReturnSWO() {
        String swoId = diagnosisPart.SWO__c;
        String url = '/apex/SWOPage?id=' + swoId + '&type=Diagnosis';
        PageReference  ref = new Pagereference(url);
        ref.setRedirect(true);
         return ref;
    }
}