public with sharing class ConsumApplyEquipmentSetSRListController {
    public List<Consum_Apply_Equipment_Set__c> resList { get; set; }
    public String sr_open { get; set; }
    public String sr_apply_no { get; set; }
    public String sr_id { get; set; }
    public String sr_type { get; set; }
    public List<SelectOption> typeList {get; set;}
    public String selectedType {get; set;}
    private String Id;
    public Integer getResListSize() {
        return resList.size();
    }
    
    public ConsumApplyEquipmentSetSRListController() {
//        Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        Id = ApexPages.currentPage().getParameters().get('id');
        sr_type = ApexPages.currentPage().getParameters().get('type');
        sr_open = ApexPages.currentPage().getParameters().get('open');
    }

    public void init() {
        resList = new List<Consum_Apply_Equipment_Set__c>();
        initTypeList();

        if (Id != null) {
            resList = [select Model_No__c
                            , Consum_Apply__r.Name
                            , Yi_Assigned_Cnt__c
                            , Yi_Shipment_request__c
                            //, Yi_Return__c
                            , RetalFSetDetail_Cnt__c
                            , First_RAESD_Model_No_F__c
                            , First_RAESD__r.SerialNumber_F__c
                            , First_RAESD__r.Fixture_QRCode_F__c
                            , RAES_Status__c
                    from Consum_Apply_Equipment_Set__c
                    where Consum_Apply__r.Name = :id and Cancel_Select__c = false order by Name
                    //AND Cancel_Select__c = false
                    ];
            if (resList.size() > 0) {
                sr_apply_no = resList[0].Consum_Apply__r.Name;
                sr_id = resList[0].Consum_Apply__r.Name;
            } else {
                List<Consum_Apply_Equipment_Set_Detail__c> raesdList = [select Id from Consum_Apply_Equipment_Set_Detail__c where Fixture_QRCode_F__c = :id
                              //and Cancel_Select__c = False
                            ];
                Set<Id> raesdSet = new Set<Id>();
                if (raesdList.size() > 0) {
                    for (Consum_Apply_Equipment_Set_Detail__c raesd : raesdList) {
                        raesdSet.add(raesd.Id);
                    }
                }
                resList = [select Id
                                , Model_No__c
                                , Consum_Apply__c
                                , Yi_Assigned_Cnt__c
                                , Yi_Shipment_request__c
                                //, Yi_Return__c
                                , RetalFSetDetail_Cnt__c
                                , Consum_Apply__r.Name
                                , First_RAESD_Model_No_F__c
                                , First_RAESD__r.SerialNumber_F__c
                                , First_RAESD__r.Fixture_QRCode_F__c
                                , RAES_Status__c
                        from Consum_Apply_Equipment_Set__c
                       where (First_RAESD__c in :raesdSet
                                or ((First_RAESD__r.Canceled__c in :raesdSet or First_RAESD__r.Canceled__r.Canceled__c in :raesdSet)
                                        and Consum_Apply__r.ShelfUp_ng_num__c <> 0
                                )
                            ) 
                         and Consum_Apply__r.RA_Status__c <> :FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Wan_Liao.ordinal())
                         and Cancel_Select__c = false
                       order by Name
                    ];
                if (resList.size() > 0) {
                    sr_apply_no = resList[0].Consum_Apply__r.Name;
                    sr_id = resList[0].Id;
                } else {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '备品不存在'));
                }
            }

        }
        return;
    }
    
    private void initTypeList(){
        typeList = new List<SelectOption>();
        typeList.add(new SelectOption('无','无'));
        typeList.add(new SelectOption('发货', '发货'));
        typeList.add(new SelectOption('收货', '收货'));
        System_UserSetting__c config = System_UserSetting__c.getInstance(UserInfo.getUserId());
        selectedType = config.CAESD_Action_Type__c;
        if (String.isBlank(selectedType)){
            selectedType = '无';
        }

    }
    public void setType(){
        if (selectedType != '无' && selectedType != null ){
            System_UserSetting__c config = System_UserSetting__c.getInstance(UserInfo.getUserId());
            config.CAESD_Action_Type__c = selectedType;
            FixtureUtil.withoutUpsertObjects(new System_UserSetting__c[]{config});
        }

    }
}