public with sharing class AllAssetController {
	
	public String assECcode { get; set; }
	public String assName { get; set; }
	public Asset rec { get; set; }
	public String setId { get; set; }

	public List<EquipmentSetInfo> equipmentSetRecords { get; set; }
	public List<EquipmentSetInfo> loanerSetRecords { get; set; }

    private Integer dayCount = 0;


	private Integer Select_Limit = 200;



	public AllAssetController() {
		 equipmentSetRecords = new List<EquipmentSetInfo>();
	}

	public Integer loanerSetNo {
        get {
            return loanerSetRecords == null ? 0 : loanerSetRecords.size();
        }
    }

    public Integer pageRecordNo { 
        get {
            return equipmentSetRecords == null ? 0 : equipmentSetRecords.size();
        }
    }

	public void init() {
        List<Asset> assList = [select Id,Name, SerialNumber, Internal_Asset_number__c, OT_Code__c,EC_Code__c, Name__c,ProductName__c,
        					AssetType__c,status,Count_can_allocate_F__c,RecordTypeId,loaner_place__c,Remarks2__c,
        					Rental_Customer__r.Name,Rental_End_Date__c,Description,Rental_Start_Date__c 
        					from Asset 
        					WHERE Id != null AND Equipment_Type__c = 'BS'];
   		
       	for(Integer i = 0 ;i<assList.size();i++){
       		if (i == Select_Limit ){
       			break;
       		}

       		equipmentSetRecords.add(new EquipmentSetInfo(assList[i]));
       	}
       	this.getLoanerSet();
   		rec = new Asset();
   		rec.Equipment_Type__c = 'BS';
    }

    public void getLoanerSet() {
        // 所有样机Set
        loanerSetRecords = new List<EquipmentSetInfo>();
        List<loaner_Set__c> loanerSetSearch = [select Id, Name, Equipment_Type__c,Remarks__c,SerialNumber__c,Internal_Asset_number__c,Status__c,loaner_place__c,Reservation_information__c
                                                 from  Loaner_Set__c
                                                 WHERE Equipment_Type__c = 'BS'
                                                 order by Name];
        Integer line = 0;
        for(loaner_Set__c ls : loanerSetSearch){
            EquipmentSetInfo li = new EquipmentSetInfo(line, ls);
            loanerSetRecords.add(li);
            line += 1;
        }
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, 'loanerSetRecords' + loanerSetRecords));
    }

     public void getLoanerSetdet() {
        
       
        // 样机set中所有样机
        List<String> loanerSetDetIDList = new List<String>();
        equipmentSetRecords = new List<EquipmentSetInfo>();
        for(Loaner_Set_Detail__c lSD : [SELECT Id,Asset__c FROM Loaner_Set_Detail__c WHERE Loaner_Set__c = :setId]){
            loanerSetDetIDList.add(lSD.Asset__c);
        }

        List<Asset> queryList = [select Id,Name, SerialNumber, Internal_Asset_number__c,Name__c,AssetType__c,
                                        OT_Code__c, EC_Code__c, status,Count_can_allocate_F__c,
                                        RecordTypeId,loaner_place__c,Remarks2__c,Rental_Customer__r.Name,Rental_End_Date__c,ProductName__c
                                        ,Description,Rental_Start_Date__c 
                                    from Asset
                                    where Id in :loanerSetDetIDList];
                                   
        // 新明细行做成  
        for (Integer i = 0;  i < queryList.size(); i++) {
            if (i == Select_Limit) {
            	break;
            }
                equipmentSetRecords.add(new EquipmentSetInfo(queryList[i]));
             
        }
       
    }

      public void searchLoanerApp() {
        //已选择个体管理样机
        List<String> loanerindividualList = new List<String>();
        equipmentSetRecords = new List<EquipmentSetInfo>();
       
        String soql = 'select Id,Name, SerialNumber, Loaner__c,Internal_Asset_number__c, OT_Code__c,EC_Code__c, Name__c,ProductName__c,AssetType__c,';
              soql += ' status,Count_can_allocate_F__c,RecordTypeId,loaner_place__c,Remarks2__c,Rental_Customer__r.Name,Rental_End_Date__c ,Description,Rental_Start_Date__c from Asset WHERE Id != null ';


        if (!String.isBlank(assName)) {
            soql += ' and Name like \'%' + String.escapeSingleQuotes(assName.trim().replaceAll('%', '\\%')) + '%\'';
        }
        if (!String.isBlank(rec.OT_Code__c)) {
            soql += ' and OT_Code__c like \'%' + String.escapeSingleQuotes(rec.OT_Code__c.trim().replaceAll('%', '\\%')) + '%\'';
        }
        if (!String.isBlank(assECcode)) {
            soql += ' and EC_Code__c like \'%' + String.escapeSingleQuotes(assECcode.trim().replaceAll('%', '\\%')) + '%\'';
        }
       
            soql += ' and Equipment_Type__c = \'BS\'';
       
        if (!String.isBlank(rec.loaner_place__c)) {
            soql += ' and loaner_place__c = \'' + String.escapeSingleQuotes(rec.loaner_place__c) + '\'';
        }
        if (!String.isBlank(rec.Status)) {
            soql += ' and status = \'' + String.escapeSingleQuotes(rec.Status) + '\'';
        }
         if (!String.isBlank(rec.SerialNumber)) {
            soql += ' and SerialNumber like \'%' + String.escapeSingleQuotes(rec.SerialNumber.trim().replaceAll('%', '\\%')) + '%\'';
        }
        if (!String.isBlank(rec.Internal_Asset_number__c)) {
            soql += ' and Internal_Asset_number__c like \'%' + String.escapeSingleQuotes(rec.Internal_Asset_number__c.trim().replaceAll('%', '\\%')) + '%\'';
        }




        List<Asset> queryList = Database.query(soql);
        System.debug(soql);
        System.debug(queryList);
        // 新明细行做成
        for (Integer i = 0;  i < queryList.size(); i++) {
            if (i == Select_Limit) {
                break;
            }
                equipmentSetRecords.add(new EquipmentSetInfo(queryList[i]));
             
        }
        System.debug(equipmentSetRecords);

    }


    /* private void makeMessage() {
        if (Over_Limit) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '数据超过' + Select_Limit + '件，只显示前' + Select_Limit + '件'));
            Over_Limit = false;
        }
    }*/


    class EquipmentSetInfo implements Comparable {
     	public Boolean check { get; set; }
        public Boolean oldcheck { get; set; }
        public loaner_application_detail__c lad { get; set; }
        public Asset aset { get; set; }
        public Loaner_Set__c lSet { get; set; }
        public Date Rental_Start_Date { get; set; }
        public Date Rental_End_Date { get; set; }
        //最早可借出时间
        public Date earliest_Lend_Date { get; set; }
        public Integer SEdayCount { get; set; }
        public Integer deatilNo { get; set; }
        public Boolean canInput { get; set; }
        public Boolean showNo { get; set; }
        public String remark { get; set; }
        //public Date endDate {get; set; }
        //转借元申请明细标记
        public Boolean firstDet { get; set; }

        // 已存产品明细
        public EquipmentSetInfo(loaner_application_detail__c e) {
            check = true;
            oldcheck= true;
            lad = e;
            aset = e.LOANER__r;
            //endDate = aset.Rental_End_Date__c;
            if(String.isNotBlank(lad.Remark__c)){
                remark = lad.Remark__c;
            }else{
                remark = aset.Remarks2__c;
            }
            deatilNo = 1;
            canInput = false;
            firstDet = false;
            if(String.valueOf(aset.RecordTypeId).subString(0,15) == System.label.Asset_Record_Type2){
                showNo = true;
            }else{
                showNo = false;
            }
        }

        public EquipmentSetInfo(Integer in_line, Loaner_Set__c e) {
            deatilNo = in_line;
            lSet = e;

        }

        public EquipmentSetInfo(Asset e) {
            check = false;
            oldcheck = false;
            lad = new loaner_application_detail__c();
            aset = e;
            remark = aset.Remarks2__c;
            firstDet = false;
            if(String.valueOf(aset.RecordTypeId).subString(0,15) == System.label.Asset_Record_Type2){
                canInput = true;
                showNo = true;
            }else{
                //lad.loaner_ApplyNo__c = 1;
                deatilNo = 1;
                canInput = false;
                showNo = false;
            }
        }
        // 排序
        public Integer compareTo(Object compareTo) {
            return null;
        }
    }
}