@isTest
private class TradeCustomerManagerControllerTest {
    static testMethod void testMethod1() {
        
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account accIE = new Account(
            Name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer IE',
            FacilityName__c='abc',
            PostCode__c='000000'
        );
        insert accIE;

        Contact con = new Contact();
        con.LastName = 'test';
        con.AccountId = accIE.Id;
        con.MobilePhone = 'MobilePhoneD__c';
        con.OtherPhone = 'OtherPhoneD__c';
        con.Fax = 'FaxD__c';
        con.Email = 'EmailD__c@test.com';
        con.Phone = 'PhoneD__c';
        con.Title = 'TitleD__c';
        con.Address1__c = 'Address1D__c';
        con.Address2__c = 'Address2D__c';
        con.Address3__c = 'Address3D__c';
        con.Postcode__c = '100111';
        con.ContactStatus__c = 'ContactStatusD__c';
        con.CancelReason__c = 'CancelReasonD__c';
        con.StatusD__c = 'Pass';

        insert con;

        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE'
        );
        insert pricebook;

        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = accIE.Id,
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'NDT',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            TradeType__c = 'Taxation'
        );
        insert opp;
        
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

        OrderItemTriggerHandler.tess();

        List<RecordType> rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'IESalesContract'];
        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accIE.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            RecordTypeId = rectOdr[0].Id,
            SpecialDeliveryAccount__c = accIE.Id,
            SpecialDeliveryAccount_D__c = accIE.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert odr; 
        PageReference page = new PageReference('/apex/TradeCustomerManager?orderId=' + odr.Id);
        System.Test.setCurrentPage(page);
        TradeCustomerManagerController controller = new TradeCustomerManagerController();
        controller.init();
        controller.deleteForm();
        controller.send();
        controller.trade.isAloneCustomer__c = '是';
        controller.send();
        controller.trade.isHaveLink__c = '是';
        controller.send();
        controller.trade.militaryLink__c = 'militaryLink';
        controller.send();
        controller.trade.useYes__c = true;
        controller.send();
        controller.trade.isHaveLink__c = '否';
        controller.trade.companyLink__c = null;
        controller.send();
        controller.trade.isAlone__c = '是';
        controller.trade.model__c = null;
        controller.send();
        controller.returnOrder();
        TradeCustomerManager__c tra = new TradeCustomerManager__c();
        tra.OrderIdSave__c = odr.Id;
        tra.order__c =  odr.Id;
        insert tra;

        Attachment att = new Attachment();
        att.Name = 'test';
        att.Body = EncodingUtil.base64Decode('test');
        att.ParentId = tra.Id;
        insert att; 
        Attachment att1 = new Attachment();
        att1.Name = '个人信息';
        att1.Body = EncodingUtil.base64Decode('test');
        att1.ParentId = tra.Id;
        insert att1; 
        Attachment att2 = new Attachment();
        att2.Name = '军事';
        att2.Body = EncodingUtil.base64Decode('test');
        att2.ParentId = tra.Id;
        insert att2;

        page = new PageReference('/apex/TradeCustomerManager?id=' + tra.Id);
        System.Test.setCurrentPage(page);
        controller = new TradeCustomerManagerController();
        controller.init();
        controller.trade.militaryLink__c = 'militaryLink';
        controller.send();
        /*controller.upload();*/
        page = new PageReference('/apex/TradeCustomerManager?orderId=' + odr.Id);
        System.Test.setCurrentPage(page);
        controller = new TradeCustomerManagerController();
        controller.init();
        controller.uploadFile();
        controller.init();
        controller.fileIdSee = att.Id;
        controller.seeFile();
		controller.fileIdDelete = att.Id;
        controller.deleteFile();
        controller.deleteForm();
    }
}