public with sharing class TydelikeConController {
	public Account acc {get;set;}
	public Contact con {get;set;}
	public TydelikeContact__c tyc {get;set;}
	public TydelikeAccount__c tya {get;set;}
	public String accName { get; set; }
    public String tycoId { get; set; }
    public String tyacId { get; set; }

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

    public TydelikeConController() {
        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');
        if (rtUrl == null || rtUrl == 'null') {
            rtUrl = '';
        }
    }
    //页面初始化
    public PageReference init() {
        accName = System.currentPageReference().getParameters().get('accName');
        tycoId = System.currentPageReference().getParameters().get('tycoId');
        tyacId = System.currentPageReference().getParameters().get('tyacId');
        //传来的客户的名字查询
        List<Account> accList = [select Id,Name,ProductSegment__c from Account where Name =: accName];
        if (accList.size() > 0) {
            acc = accList[0];
        }
        //传来的联系人的id查询
        List<TydelikeContact__c> tycoList = [select 
        Id,Name,TyAddress__c,TyFaxD__c,TyPhoneD__c,TyPostcode__c,EnglishAddress__c,ContactEnglishName__c 
        from TydelikeContact__c 
        where Id =: tycoId];
        if (tycoList.size() > 0) {
            tyc = tycoList[0];
        }

        //初始化页面的显示
        con = new Contact();
        con.AccountId = acc.Id;
        con.LastName = tyc.Name;
        con.Address1D__c = tyc.TyAddress__c;
        con.FaxD__c = tyc.TyFaxD__c;
        con.StatusD__c = '草案中';
        con.MobilePhoneD__c = tyc.TyPhoneD__c;
        con.PostcodeD__c = tyc.TyPostcode__c;
        con.EnglishAddress__c = tyc.EnglishAddress__c;
        con.ContactEnglishName__c = tyc.ContactEnglishName__c;
        if (acc.ProductSegment__c == 'BS') {
            con.ProductSegmentBS__c = true;
        }else if(acc.ProductSegment__c == 'IE'){
        	con.ProductSegmentIE__c = true;
        }else if(acc.ProductSegment__c == 'NDT'){
        	con.ProductSegmentNDT__c = true;
        }else if(acc.ProductSegment__c == 'ANI'){
        	con.ProductSegmentANI__c = true;
        }else if(acc.ProductSegment__c == 'RVI'){
        	con.ProductSegmentRVI__c = true;
        }

        return null;
    }

    public PageReference save() {
    	tycoId = System.currentPageReference().getParameters().get('tycoId');
        tyacId = System.currentPageReference().getParameters().get('tyacId');
    	List<TydelikeContact__c> tyconList = new List<TydelikeContact__c>();
    	TydelikeContact__c tycontact = new TydelikeContact__c();
    	tycontact.Id = tycoId;
    	tycontact.OfSkrap__c = true;
        tycontact.Ofomsa__c = true;
    	tyconList.add(tycontact);

        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        TydelikeAccount__c tyaccount = new TydelikeAccount__c();
        tyaccount.Id = tyacId;
        tyaccount.Ofomsa__c = true;
        tyacList.add(tyaccount);
        //临时客户与联系人的OfSkrap__c,Ofomsa__c可参与调查。
    	Savepoint sp = Database.setSavepoint();
        try {
        	update tyconList;
            update tyacList;
            insert con;
            
            String url = baseUrl + '/apex/TydelikeAC';
            return new Pagereference(url);
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
        }
        return null;
    }
}