public with sharing class LexChoiceAssetController {
    public class SSPLine {
        @AuraEnabled
        public Boolean isSelected;
        @AuraEnabled
        public String setProductid ;
        @AuraEnabled
        public String Assetid;
        @AuraEnabled
        public String Accountid;
        @AuraEnabled
        public String Hospital;
         @AuraEnabled
        public String WorkLocation;
        @AuraEnabled
        public Asset theObject;
        @AuraEnabled
        public String Name;      
        @AuraEnabled
        public String SerialNumber;
        @AuraEnabled
        public String HP_Name;
        @AuraEnabled
        public String Department_Name;
        @AuraEnabled
        public Date Posting_Date;
        @AuraEnabled
        public Date Extend_Gurantee_DateTo;
        @AuraEnabled
        public Date CurrentContract_End_Date;


        public SSPLine(Asset psl) {
          isSelected = false;
          setProductid = psl.id;
            setProductid = setProductid.substring(0,15);
            Assetid = psl.id;
            if (psl.Account.id !=null) {
                Accountid = psl.Account.id;
            }
            Name = psl.Name;
            Hospital = psl.Hospital__r.id;
            if (psl.Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c !=null) {
                WorkLocation = psl.Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c;
            }
            SerialNumber= psl.SerialNumber;
            HP_Name = psl.HP_Name__c;
            Department_Name = psl.Department_Name__c;
            Posting_Date = psl.Posting_Date__c;
            Extend_Gurantee_DateTo = psl.Extend_Gurantee_DateTo__c;
            CurrentContract_End_Date = psl.CurrentContract_End_Date__c;
        }
    }
    public class InitData{
        @AuraEnabled
        public String Id;
        @AuraEnabled
        public String StatusC;
        @AuraEnabled
        public String OwnerId;
        @AuraEnabled
        public String Repair_Detail;
        @AuraEnabled
        public Date FaultTime;
        @AuraEnabled
        public String RepairApplicantName;
        @AuraEnabled
        public String HPname;
        @AuraEnabled
        public String DepName;
        @AuraEnabled
        public Date RepairsReportDate;
        @AuraEnabled
        public Date ReceiverTime;
        @AuraEnabled
        public Date PlannedVisitDay;
        @AuraEnabled
        public Date ActualVisitDateFirst;
         @AuraEnabled
        public Date ActualVisitDateSecon;
         @AuraEnabled
        public Date ActualVisitDateThird;
         @AuraEnabled
        public Date ApplicationTime;
     }

     public class AssData{
        @AuraEnabled
        public String canRepair;
     }

     @AuraEnabled
    public static AssData searchAss(String recordId){
        AssData res = new AssData();
        List<Asset> ass = [select id,Product_ID__c from Asset where ID = :recordId];
        List<Product2> p2 = [select id,Name,Can_Repair__c 
                        from Product2 where 
                        ID = :ass[0].Product_ID__c];
        if (p2[0].Can_Repair__c != null) {
            res.canRepair = p2[0].Can_Repair__c;
        }
        return res;
    }
    @AuraEnabled
    public static InitData init(String recordId){
        InitData res = new initData();
        try{
            RepairSubOrder__c report =  [SELECT Id,ApplicationTime__c,ActualVisitDateThird__c,ActualVisitDateSecon__c,ActualVisitDateFirst__c,PlannedVisitDay__c,ReceiverTime__c,RepairsReportDate__c,Status__c,Department__r.Name,Hospital__r.name,RepairApplicantName__c,OwnerId,ResponseResultDesc__c,FaultTime__c FROM RepairSubOrder__c WHERE Id =: recordId LIMIT 1];
            System.debug(LoggingLevel.INFO, '*** opp: ' + report);
            res.Id = report.Id;
            res.StatusC = report.Status__c;
            res.OwnerId = report.OwnerId;
            res.Repair_Detail = report.ResponseResultDesc__c;
            if (report.FaultTime__c!=null) {
                res.FaultTime = report.FaultTime__c.date();
            }
            res.RepairApplicantName = report.RepairApplicantName__c;
            if (report.Hospital__r.name!=null) {
                res.HPname = report.Hospital__r.name;
            }
            if (report.Department__r.Name!=null) {
                res.DepName = report.Department__r.Name;
            }
            res.RepairsReportDate = report.RepairsReportDate__c;
            if (report.ReceiverTime__c!=null) {
                res.ReceiverTime = report.ReceiverTime__c.date();
            }
            if (report.PlannedVisitDay__c!=null) {
                res.PlannedVisitDay = report.PlannedVisitDay__c.date();
            }
            res.ActualVisitDateFirst = report.ActualVisitDateFirst__c;
            res.ActualVisitDateSecon = report.ActualVisitDateSecon__c;
            res.ActualVisitDateThird = report.ActualVisitDateThird__c;
            if (report.ApplicationTime__c!=null) {
                  res.ApplicationTime = report.ApplicationTime__c.date();
            }
            System.debug(LoggingLevel.INFO, '*** res: ' + res);
        }catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
        }
        return res;
    }  

    @AuraEnabled
    public static String getNeedParm1(){
        String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        return baseUrl;
    }

  //   @AuraEnabled
  //   public static String init(){
  //       String reportid;
  //       Report r = new Report();
  //       List<Report> rs = new List<Report>();
  //       rs=[Select id,DeveloperName,Name,NamespacePrefix from Report Where DeveloperName='Set_Product_Detail'];
  //       if (rs.size()>0){
  //           //system.debug('DeveloperName=' + rs[0].DeveloperName);
  //           //system.debug('Name=' + rs[0].Name);
  //           //system.debug('NamespacePrefix=' + rs[0].NamespacePrefix);
  //           reportid = rs[0].id;
  //           reportid = reportid.substring(0,15);
  //       }
  //       return reportid;
  // }

  @AuraEnabled
  public static List<SSPLine> getData(String SearchName , String SearchNo , String SearchSN){
    String searchSql = 'Select id, name,Account.id,Account.Parent.FSE_SP_Main_Leader__r.Work_Location__c,Hospital__r.id, SerialNumber,HP_Name__c,Department_Name__c,Posting_Date__c,Extend_Gurantee_DateTo__c,CurrentContract_End_Date__c from Asset ';
    String whereStr = 'Where  id !=null  ';
    String whereSql = '';

    if(SearchName != null && SearchName != ''){
      whereSql += 'and HP_Name__c Like ' + '\'%' + SearchName + '%\' ';
    }

    if(SearchNo != null && SearchNo != ''){
      whereSql += 'and OwnershipMachine_No__c =\'' + SearchNo + '\'';
    }

    if(SearchSN != null && SearchSN != ''){
      whereSql += 'and SerialNumber like ' + '\'%' + SearchSN + '%\' ';
    }
    searchSql = searchSql + whereStr + whereSql;

    searchSql += ' order by Posting_Date__c limit 100';

    List<Asset> cl = new List<Asset>();

    cl = Database.query(searchSql);
    List<SSPLine> activities = New List<SSPLine>();

    for(Asset psl : cl){
        SSPLine a = new SSPLine(psl);
        activities.add(a);
        
    }
    return activities;
  }


}