public with sharing class CM_SearchContactServiceController {
    public Contact newCon { get; set; }
    public Contact searchCon { get; set; }
    public List<LineInfo> lineInfoList { get; set; }
    public String conId { get; set; }

    public String openLine { get; set; }
    public String accountIdV2{ get; set; }      //zhj 2022-02-04 新方案改造
    private String accountId;
    private String nowValue;

    //deloitte-zhj 20231115 PIPL还原
    // public String staticResource {get; set;}
    // public String contactAWSIds {set;get;}
    // public String contactsInfo {set;get;}
    // public String awsDataIdArray {set;get;}

    public String sfContactId{set;get;} //zhj 2022-12-02 sfId
    //public String staticResourceContactV2 {get; set;}   //zhj 2022-02-04 新方案改造  //deloitte-zhj 20231115 PIPL还原

    public CM_SearchContactServiceController() {
        openLine = Apexpages.currentPage().getParameters().get('line');
        accountId = Apexpages.currentPage().getParameters().get('acc');
        //accountIdV2 = accountId;   //deloitte-zhj 20231115 PIPL还原
        nowValue = Apexpages.currentPage().getParameters().get('now');
    }

    public void init() {
        searchCon = new Contact();
        if (nowValue != null && nowValue != '') {
            searchCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,
                                FirstName, LastName
                           from Contact where Id = :nowValue];
            searchCon.Search_LastName__c = searchCon.LastName;
            searchCon.Search_FirstName__c = searchCon.FirstName;
        }

        searchContact();

        newCon = new Contact();
        newCon.AccountId = accountId;
        sfContactId = '';

        return;
    }


    public PageReference searchContact() {
        //deloitte-zhj 20231115 PIPL还原 去除AWS_Data_Id__c
        String searchStr = 'select Id, Name, Department__c, Type__c, AccountName__c, Supplement__c, Phone,UnifiedI_Contact_ID__c,MobilePhone ';  //deloitte-zhj 20231020 加上UnifiedI_Contact_ID__c  //deloitte-zhj 20231031 加上MobilePhone
         searchStr += ', City__c , State__c '; //2018/11/19 HWAG-B399RW 读取省和市
        searchStr += '        from Contact ';
        searchStr += '       where Isactive__c = \'有效\' ';
        searchStr += '         and AccountId = :accountId ';

        //deloitte-zhj 20231115 PIPL还原 start
        //2022/02/15 张华建 检索 start
        if (searchCon.Search_LastName__c != null && searchCon.Search_LastName__c != '') {
            searchStr += '     and LastName like \'%' + searchCon.Search_LastName__c + '%\'';
        }
        if (searchCon.Search_FirstName__c != null && searchCon.Search_FirstName__c != '') {
            searchStr += '     and FirstName like \'%' + searchCon.Search_FirstName__c + '%\'';
        }

        // System.debug('awsDataIdArray = ' + awsDataIdArray);
        // if (awsDataIdArray != null && awsDataIdArray != '') {
        //     String[] arr = awsDataIdArray.split(',');
        //     System.debug('arr = ' + arr);
        //     String awsDataIdSql = '';
        //     for(String s : arr){
        //         System.debug('s = ' + s);
        //         awsDataIdSql = awsDataIdSql + ',' + '\'' + s+'\'';
        //     }
        //     awsDataIdSql = awsDataIdSql.substring(1);
        //     System.debug('awsDataIdSql = ' + awsDataIdSql);
        //     searchStr += ' and AWS_Data_Id__c in (' + awsDataIdSql + ')';
        // }
        //2022/02/15 张华建 检索 end
        //deloitte-zhj 20231115 PIPL还原 end

        system.debug('=====searchStr:' + searchStr);

        searchStr += ' order by lastmodifieddate desc limit 500';

        List<Contact> searchResult = new List<Contact>();
        if(!Test.isRunningTest()){
            //searchResult = Database.query(searchStr);   //deloitte-zhj 20231024 with改造
            searchResult = LexWithOutSharingSQLDao.searchContactServiceResult(searchStr,accountId);
        }else{
            // searchResult = [SELECT Id FROM Contact];
            //20231103 李文涛
            searchResult = [SELECT Id,UnifiedI_Contact_ID__c FROM Contact];
        }

        lineInfoList = new List<LineInfo>();
        Integer line = 0;
        for (Contact con : searchResult) {
            line += 1;
            LineInfo li = new LineInfo(line, con);
            //deloitte-zhj 20231024 with改造
            if(li.con.UnifiedI_Contact_ID__c != null &&li.con.UnifiedI_Contact_ID__c != '' ){
                li.unifiedId = li.con.UnifiedI_Contact_ID__c;
            }else{
                li.unifiedId = '';
            }
            lineInfoList.add(li);
        }
        System.debug('lineInfoList  = '  + lineInfoList);
        //deloitte-zhj 20231024 with改造
        editClear();

        return null;
    }

    public PageReference editContact() {
        if (conId != null && conId != '') {
            newCon = [select Id, Name, Department__c, Type__c, Search_LastName__c, Search_FirstName__c, Phone, Supplement__c,
                      FirstName, LastName    //deloitte-zhj 20231115 PIPL还原
                        from Contact where Id = :conId];    //zhj 新方案改造 去除Encrypted__c 2022-12-05
            newCon.Search_LastName__c = newCon.LastName;
            newCon.Search_FirstName__c = newCon.FirstName;
            sfContactId = newCon.Id; // zhj 2022-12-02 得到sfid给aws
        }

        return null;
    }

    public PageReference saveNew()  {
        if (newCon.Search_LastName__c == null || newCon.Search_LastName__c == '') {
            newCon.Search_LastName__c.addError('必须填写。');
            return null;
        }
        newCon.LastName = newCon.Search_LastName__c;
        newCon.FirstName = newCon.Search_FirstName__c;
        //deloitte-zhj 20240103 报错优化
        //upsert newCon;
        try {
            upsert newCon;
        }catch (DmlException e) {
            // 捕获 DML 异常
            String errorMessage = 'Error occurred while upserting records: ' + e.getMessage();
            // 返回错误消息给前端
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage);
            ApexPages.addMessage(myMsg);
        } catch (Exception e) {
            // 捕获其他异常
            String errorMessage = 'An unexpected error occurred: ' + e.getMessage();
            // 返回错误消息给前端
            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage);
            ApexPages.addMessage(myMsg);
        }
        System.debug('newCon.Id = ' + newCon.Id); 
        sfContactId = newCon.Id; // zhj 2022-12-02 得到sfid给aws

        searchCon.Search_LastName__c = newCon.LastName;
        searchCon.Search_FirstName__c = newCon.FirstName;
        searchContact();

        return null;
    }

    public PageReference editClear()  {
        newCon = new Contact();
        newCon.AccountId = accountId;
        return null;
    }

    public PageReference clearLineInfoList()  {
        lineInfoList = new List<LineInfo>();
        return null;
    }

    class LineInfo {
        public Integer lineNo { get; set; }
        public Contact con { get; set; }
        public String unifiedId{ get; set; } //Add By Li Jun 20231024
        public LineInfo(Integer in_line) {
            lineNo = in_line;
            con = new Contact();
        }

        public LineInfo(Integer in_line, Contact in_con) {
            lineNo = in_line;
            con = in_con;
        }
    }
}