/*
    TestClass
    RentalFixtureManage1Test
    RentalFixtureManage3Test
    RentalFixtureManage9Test
    RentalFixtureManage10Test
 */
public with sharing class EquipmentSetSRList2Controller {
    public List<Rental_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 EquipmentSetSRList2Controller() {
//        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 Pagereference init() {
        resList = new List<Rental_Apply_Equipment_Set__c>();
        initTypeList();

        if (Id != null) {
            resList = [select Rental_Start_Date__c,Rental_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 Rental_Apply_Equipment_Set__c
                    where Rental_Apply__r.Name = :id order by Name
                    //AND Cancel_Select__c = false
                    ];
            if (resList.size() > 0) {
                sr_apply_no = resList[0].Rental_Apply__r.Name;
                sr_id = resList[0].Rental_Apply__r.Name;
            } else {
                List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [select Id from Rental_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 (Rental_Apply_Equipment_Set_Detail__c raesd : raesdList) {
                        raesdSet.add(raesd.Id);
                    }
                }
                resList = [select Rental_Start_Date__c,Id, Rental_Apply__c, Rental_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 Rental_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 Rental_Apply__r.ShelfUp_ng_num__c <> 0
                                )
                            )
                         and Rental_Apply__r.RA_Status__c <> :FixtureUtil.raStatusMap.get(FixtureUtil.RaStatus.Wan_Liao.ordinal())
                         and Rental_Apply__r.RecordType.DeveloperName != 'AgencyRequest'
                       order by Name
                    ];
                if (resList.size() > 0) {
                    sr_apply_no = resList[0].Rental_Apply__r.Name;
                    sr_id = resList[0].Id;
                }
                else {
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '备品不存在'));
                    List<Asset> assList = [SELECT Id FROM Asset WHERE Fixture_QRCode__c=:Id LIMIT 1];
                    if(!assList.isEmpty()) {
                        return new Pagereference('/apex/TransferEquipmentSetSRList?id=' + id);
                    }
                }
            }

        }
        return null;
    }

    private void initTypeList(){
        typeList = new List<SelectOption>();
        typeList.add(new SelectOption('无','无'));
        typeList.add(new SelectOption('下架', '下架'));
        typeList.add(new SelectOption('出库检测', '出库检测'));
        typeList.add(new SelectOption('发货','发货'));
        typeList.add(new 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.RAESD_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.RAESD_Action_Type__c = selectedType;
            FixtureUtil.withoutUpsertObjects(new System_UserSetting__c[]{config});
        }
    }

    //// 出库
 //           //todo エラーか、0件か
 //           if (SR_status == 'Shippment') {
 //               if (es.Shipment_request_time__c == null || es.Arrival_in_wh__c) {
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '备品没有出库指示'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if(Rac.repair__r.Repair_Final_Inspection_Date__c<Date.today()){
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '维修检测已完成，不能出库'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if (es.Shippment_loaner_time__c != null) {
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '备品已经出库'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if (es.cancel_after_loaner_arranged__c == true) {
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '备品借出申请已经取消，不能出库'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if(Ra_c.Cancel_Date__c!=null){
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '分配已取消，不能出库'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if(Rac.Campaign__r.Status == '取消'){
 //                 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '学会状态取消，备品不可操作'));
 //                 saveBtnDisabled = true;
 //                 return null;
 //               }
 //               //***********************20161130本藩上线暂不作对应，禁用********************************//
 //               if(Rac.repair__r.Repair_Final_Inspection_Date__c!=null&&Rac.repair__c!=null){
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '最终检测日不为空，不能出库'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if(Rac.repair__r.Repair_Shipped_Date__c!=null&&Rac.repair__c!=null){
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, 'RC修理品返送日不为空，不能出库'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if(Rac.repair__r.Return_Without_Repair_Date__c!=null&&Rac.repair__c!=null){
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '未修理归还日不为空，不能出库'));
 //                   saveBtnDisabled = true;
 //                   return null;

 //               }
 //               //***********************20161130本藩上线暂不作对应，禁用********************************//
 //               if (es.Return_to_wh_staff__c == null) {
 //                   es.Return_to_wh_staff__c = user.Id;
 //                   es.Return_to_wh_staff__r = user;
 //               }
 //               if (Step_status == null) {
 //                   Step_status = '发货';
 //                   if (es.Inspection_result_notOK_cnt__c > 0) {
 //                       Step_status = '发货前';
 //                   }
 //               }
 //           }
 //           // 入库
 //           if (SR_status == 'Received') {
 //               if (es.Shippment_loaner_time__c == null && es.cancel_after_loaner_arranged__c == false && es.Arrival_wh_time2__c == null) {
 //                   //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '备品没有出库'));
 //                   //saveBtnDisabled = true;
 //                   //return null;
 //                   // 備品センターは下架前であれば分配取消、下架以降であれば上架を行う
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '备品没有出库'));
 //               }
 //               if (es.Arrival_wh_time2__c != null) {
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '备品已回库上架完了'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if (es.Fedex_number__c != null && es.Fedex_number__c != '' && (es.Return_Fedex_number__c == null || es.Return_Fedex_number__c == '')) {
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '备品没有寄回'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //               if(Step_status == '回库'){
 //                   if(es.Wait_for_Repair__c !=0 || es.Wait_for_Abandon__c !=0 ){
 //                       //主机维修/废弃
 //                       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '主机维修/废弃时，不能回库确认操作'));
 //                       //ErrorMessage = '主机维修/废弃时，不能回库确认操作';
 //                       saveBtnDisabled = true;
 //                   }
 //               }
 //               if(es.MissingCntSerial__c !=0){
 //                   //主机欠品
 //                   ReturnRefuse = false;
 //                   ErrorMessage = '主机异常，无法上架';
 //                   saveBtnDisabled = false;
 //               }
 //               /*if(es.CDS_complete__c ==false){
 //                   saveBtnDisabled = false;
 //                   CDSFinished = 'CDS未完成';
 //               }*/
 //               if(es.RepairCntSerial__c !=0){
 //                   //主机维修
 //                   ReturnRefuse = false;
 //                   ErrorMessage = '主机维修，无法上架';
 //                   saveBtnDisabled = false;
 //               }
 //               //if(Rac.Campaign__r.Status == '取消'){
 //               //  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '学会状态取消，备品不可操作'));
 //               //  saveBtnDisabled = true;
 //               //  return null;
 //               //}

 //               //***********************20161130本藩上线暂不作对应，禁用********************************//
 //               //if(Rac.repair__r.Repair_Final_Inspection_Date__c!=null&&Rac.repair__c!=null&&Ra_c.RAES_Status__c!='取消分配'){
 //               //    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '最终检修日不为空，不能回库'));
 //               //    saveBtnDisabled = true;
 //               //    return null;
 //               //}
 //               //if(Rac.repair__r.Repair_Shipped_Date__c!=null&&Rac.repair__c!=null&&Ra_c.RAES_Status__c!='取消分配'){
 //               //    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, 'RC修理品返送日不为空，不能回库'));
 //               //    saveBtnDisabled = true;
 //               //    return null;
 //               //}
 //               //if(Rac.repair__r.Return_Without_Repair_Date__c!=null&&Rac.repair__c!=null&&Ra_c.RAES_Status__c!='取消分配'){
 //               //    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '未修理归还日不为空，不能回库'));
 //               //    saveBtnDisabled = true;
 //               //    return null;

 //               //}
 //               //***********************20161130本藩上线暂不作对应，禁用********************************//
 //              /* if(es.RepairAbandonCntSerial__c!=0){
 //                   //主机等待维修，或者废弃
 //                   ReturnRefuse = false;
 //                   ErrorMessage = '主机等待维修，或者废弃，无法上架';
 //                   saveBtnDisabled = false;
 //               } */
 //               if(es.Wait_for_Repair__c!=0){
 //                   ReturnRefuse = false;
 //                   ErrorMessage = '主机等待维修,无法上架';
 //                   saveBtnDisabled = false;
 //               }
 //               if(es.Wait_for_Abandon__c!=0){
 //                   ReturnRefuse = false;
 //                   ErrorMessage = '主机等待废弃，无法上架';
 //                   saveBtnDisabled = false;
 //               }
 //               //20161114对应，回寄确认者===》》》回库确认者
 //               //更新时机错误
 //               /*
 //               if (es.Received_confirmation_staff__c == null) {

 //                   es.Received_confirmation_staff__c = user.Id;
 //                   es.Received_confirmation_staff__r = user;
 //               }*/
 //               //20161114对应，回寄确认者===》》》回库确认者
 //               if (Step_status == null) {
 //                   if (es.Lost_item_check__c != 'OK') {
 //                       Step_status = '回寄';
 //                   } else {
 //                       Step_status = 'CDS';
 //                   }
 //               }

 //               //if ((es.Lost_item_check_time__c == null || es.MissingCnt__c > 0) && Step_status != '回寄') {
 //               if (es.LostItemCheckCnt__c < es.contents_number__c && es.Fedex_number__c != null && Step_status != '回寄') {
 //                   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info, '请先进行欠品确认'));
 //                   saveBtnDisabled = true;
 //                   return null;
 //               }
 //           }
}