public with sharing class AssetMaintainDetailController extends CreateRelationListPagingCtrlBase {
    public override Integer getSearchNumMax() {
        //各ページに制御あれば、最大件数を指定する
        pagesize = '20';
        return searchNumMax;
    }
    public override String getSelectedDataSql() {
        // オブジェクトAPI名
        selectedDataSql = ' From AssetMaintainDetail__c';
        selectedDataSql += ' where AssetMaintainHeader__c = \'' + String.escapeSingleQuotes(parentId) + '\'' ;
        if (myComponentController.columnLeftRW != null) {
            for (String key : myComponentController.columnLeftRW.keySet()) {
                myComponentController.columnLeftRW.put(key, 'r');
            }
        }
        if (myComponentController.columnRightRW != null) {
            for (String key : myComponentController.columnRightRW.keySet()) {
                myComponentController.columnRightRW.put(key, 'r');
            }
        }

        return selectedDataSql;
    }
    public override String getOriginObjName() {
        // オブジェクトAPI名
        originObjName = 'AssetMaintainDetail__c';
        return originObjName;
    }
    public override String getObjName() {
        // オブジェクトAPI名
        objName = 'AssetMaintainDetail__c';
        return objName;
    }
    public override String getColumnLeftFieldSetName() {
        // 左の項目セット
        columnLeftFieldSetName = 'AssetMaintainDetail_L';
        return columnLeftFieldSetName;
    }
    public override String getColumnRightFieldSetName() {
        // 右の項目セット
        switch on parentObj.MaintainType__c {
            when '耗材已消耗数' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_XH';
            }
            when '断念找回(断念取消)' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_DNQX';
            }
            when '断念找回(重新登录)' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_DNDL';
            }
            when '冻结', '解冻' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_DJJD';
            }
            when '实物报废' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_BFSW';
            }
            when '废弃(丢失)', '废弃(盘亏)' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_FQ';
            }
            when '盘盈(数量管理)' {
                columnRightFieldSetName = 'AssetMaintainDetail_R_PY';
            }
        }
        return columnRightFieldSetName;
    }
    public override String getOriginObjColumns() {
        // 項目セット
        originObjColumns = String.join(myComponentController.columus,',');
        return originObjColumns;
    }
    public override String getSqlWhereStr() {
        sqlWhereStr = '';
        if (getIsNeedRunSearch()) {
            sqlWhereStr = this.makeSoql();
        }
        return sqlWhereStr;
    }
    public override Boolean getIsNeedRunSearch() {
        return true;
    }
    public override List<String> getColumnFieldList() {
        return new List<String>{'Id'};
    }
    public override String getFKColumnField() {
        return '';
    }
    public override String getRecordTypeId() {
        //ページレイアウトを収得するのレコードタイプ
        recordTypeId = '';
        return recordTypeId;
    }
    public AssetMaintainHeader__c parentObj { get; set; }
    public String keyword{get;set;}
    public String model{get;set;}
    public Asset ass{get;set;}
    public String mainOrAccessory{get;set;}
    public static final String NONE = system.label.StartTrading_None;  // 「なし」ラベル [Chinese:--无--]
    public List<SelectOption> mainOrAccessoryOptions {
        get {
            List<SelectOption> selectOptions = new List<SelectOption> { new SelectOption('', NONE) };
            //主体
            selectOptions.add(new SelectOption('主体', '主体'));
            //附属品
            selectOptions.add(new SelectOption('附属品', '附属品'));

            return selectOptions;
        }
    }
    public String isInternalAsset{get;set;}
    public List<SelectOption> isInternalAssetOptions {
        get {
            List<SelectOption> selectOptions = new List<SelectOption> { new SelectOption('', NONE) };
            selectOptions.add(new SelectOption('是', '是'));
            selectOptions.add(new SelectOption('否', '否'));
            return selectOptions;
        }
    }
    public AssetMaintainDetailController() {
        this.ass = new Asset();

    }
    public void init(){
        parentId = ApexPages.currentPage().getParameters().get('id');
        if (!String.isBlank(this.parentId)) {
            List<AssetMaintainHeader__c> parentObjs = [
                    SELECT Id
                        , Name
                        , MaintainType__c
                        , Submit_Time__c
                        , Status__c
                        , Submit_Person__c
                        , Date__c
                      FROM AssetMaintainHeader__c
                     WHERE Id = :parentId
                    ];
            if(parentObjs.size()>0){
                parentObj = parentObjs.get(0);
            }
            else{
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '无效维护单Id'));
                return;
            }
        }
        else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '无效维护单Id'));
            return;
        }
        isNeedSearchFirst = true;
        isNeedCheckEvent = true;
        getSqlWhereStr();
    }
    public PageReference searchOpp() {

        if (!getIsNeedRunSearch()) {
            return null;
        }

        // 選択済みの製品を取得
        myComponentController.getSelectedDataInfo();

        getSqlWhereStr();
        // コンポーネントにSoqlを発行して、ページングする
        myComponentController.searchAndPaging();
        return null;
    }
    public override void setViewList(List<sObject> queryList) {
        viewList = new List<WrapperInfo>();
        for (Integer i = 0; i < queryList.size(); i++) {
            // 501を超えた場合前500のみを出す
            if (i == getSearchNumMax()) { break; }
            AssetMaintainDetail__c mf = (AssetMaintainDetail__c)queryList[i];
            viewList.add(new WrapperInfo(mf, myComponentController));
            viewList[viewList.size() - 1].lineNo = viewList.size() - 1;
        }
    }
    private String makeSoql() {
        String soql = ' where AssetMaintainHeader__c = \'' + String.escapeSingleQuotes(parentId) + '\'' ;

        if (!String.isBlank(keyword)) {
            String[] vals = keyword.split(' ');
            soql += ' and (';
            String fmodelno = '';
            for (String v : vals) {
                v = String.escapeSingleQuotes(v.replace('%', '\\%').replace('*', '%'));
                fmodelno += ' Asset__r.Product2.Fixture_Model_No_T__c like \'' + v + '\' ';
                fmodelno += ' or Asset__r.Product2.Name like \'' + v + '\' ';
                fmodelno += 'or';
            }
            fmodelno = fmodelno.removeEnd('or');
            soql += fmodelno + ' )';
        }
        if(String.isNotBlank(model)){
            soql += ' AND Asset__r.Fixture_Model_No_F__c = \'' + String.escapeSingleQuotes(model) + '\'';
        }
        if(String.isNotBlank(mainOrAccessory)){
            if(mainOrAccessory == '主体'){
                soql += ' AND Asset__r.Loaner_accsessary__c  = false';
            }
            else{
                soql += ' AND Asset__r.Loaner_accsessary__c = true';
            }
        }
        if(String.isNotBlank(isInternalAsset)){
            if(isInternalAsset == '是'){
                soql += ' AND Asset__r.Asset_loaner_category__c = \'固定资产\' ';
            }
            else{
                soql += ' AND Asset__r.Asset_loaner_category__c != \'固定资产\' ';
            }
        }
        if(String.isNotBlank(ass.Internal_Asset_number__c)){
            soql += ' AND Asset__r.Internal_Asset_number__c = \'' + String.escapeSingleQuotes(ass.Internal_Asset_number__c) + '\'';
        }
        if(String.isNotBlank(ass.SerialNumber)){
            soql += ' AND Asset__r.SerialNumber = \'' + String.escapeSingleQuotes(ass.SerialNumber) + '\'';
        }
        if(String.isNotBlank(ass.WH_location__c)){
            soql += ' AND Asset__r.WH_location__c = \'' + String.escapeSingleQuotes(ass.WH_location__c) + '\'';
        }
        if(String.isNotBlank(ass.Internal_asset_location__c)){
            soql += ' AND Asset__r.Internal_asset_location__c = \'' + String.escapeSingleQuotes(ass.Internal_asset_location__c) + '\'';
        }
        if(String.isNotBlank(ass.Product_category__c)){
            soql += ' AND Asset__r.Product_category__c = \'' + String.escapeSingleQuotes(ass.Product_category__c) + '\'';
        }
        if(String.isNotBlank(ass.Equipment_Type__c)){
            soql += ' AND Asset__r.Equipment_Type__c = \'' + String.escapeSingleQuotes(ass.Equipment_Type__c) + '\'';
        }
        if(String.isNotBlank(ass.Manage_type__c)){
            soql += ' AND Asset__r.Manage_type__c = \'' + String.escapeSingleQuotes(ass.Manage_type__c) + '\'';
        }
        return soql;
    }
}