public without sharing class LexInventoryListController {
  /*****************検索用******************/
  public static Consumable_order__c coc { get; set; }
  public static String baseUrl { get; private set; }
  /*****************画面表示Bean******************/
  public static List<ConsumableorderdetailsInfo> consumableorderdetailsRecords {
    get;
    set;
  }
  public static List<ConsumableorderdetailsInfo> consumableorderdetailsRecordsError {
    get;
    set;
  }
  // 盘点明细1
  private static List<Consumable_orderdetails__c> consumableorderdetails1 = new List<Consumable_orderdetails__c>();
  // 盘点明细2
  private static List<Consumable_order_details2__c> consumableorderdetailsSelected = new List<Consumable_order_details2__c>();

  // 盘点单号 ID
  private static String ESetId = '';
  public LexInventoryListController() {
    //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
    baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
    ESetId = ApexPages.currentPage().getParameters().get('esetId');
    consumableorderdetailsRecords = new List<ConsumableorderdetailsInfo>();
    consumableorderdetailsRecordsError = new List<ConsumableorderdetailsInfo>();
  }
  // 画面初始化
  @AuraEnabled
  public static ResponseBodyLWC init(String eSetId) {
    system.debug('eSetId====>' + eSetId);
    ESetId = eSetId;
    ResponseBodyLWC res = new ResponseBodyLWC();
    Map<String, object> data = new Map<String, object>();
    res.entity = data;
    consumableorderdetailsRecordsError = new List<ConsumableorderdetailsInfo>();

    coc = new Consumable_order__c();
    consumableorderdetailsRecords = new List<ConsumableorderdetailsInfo>();
    List<Consumable_order__c> qs = new List<Consumable_order__c>();
    qs = [
      SELECT Name, Inventory_date__c, Order_date__c
      FROM Consumable_order__c
      WHERE Id = :ESetId
    ];
    System.debug('qs===>' + qs);
    if (qs.size() > 0) {
      coc = qs[0];
    }
    // 選択済みの明细を取得
    consumableorderdetailsSelected = [
      SELECT
        Id,
        Name,
        Consumable_Product__c,
        Consumable_Product__r.Name,
        Sterilization_limit__c,
        Deliver_date__c,
        Consumable_Product__r.Name__c,
        Consumable_Product__r.Intra_Trade_List_RMB__c,
        Bar_Code__c,
        Consumable_Product__r.Asset_Model_No__c,
        Lose_Flag__c,
        recordtypeid,
        Lose_reason__c
      FROM Consumable_order_details2__c
      WHERE Consumable_Inventory_order__c = :ESetId
      ORDER BY Name
    ];
    System.debug(
      'consumableorderdetailsSelected==>' + consumableorderdetailsSelected
    );

    for (Integer i = 0; i < consumableorderdetailsSelected.size(); i++) {
      consumableorderdetailsRecordsError.add(
        new ConsumableorderdetailsInfo(consumableorderdetailsSelected[i])
      );
    }
    consumableorderdetails1 = [
      SELECT
        Id,
        Name,
        Diff__c,
        inventory_sum__c,
        Count_Sum__c,
        Consumable_order__c,
        Consumable_Product__c,
        Consumable_Product__r.Name,
        Consumable_Count__c,
        Consumable_Product__r.Intra_Trade_List_RMB__c,
        Consumable_Product__r.Asset_Model_No__c,
        Consumable_Product__r.Name__c,
        Sum_of_money__c,
        recordtypeid,
        Lose_reason__c,
        Product_Refind__c
      FROM Consumable_orderdetails__c
      WHERE Consumable_order__c = :ESetId
      ORDER BY Name
    ];
    for (Integer i = 0; i < consumableorderdetails1.size(); i++) {
      consumableorderdetailsRecords.add(
        new ConsumableorderdetailsInfo(consumableorderdetails1[i])
      );
    }
    data.put('qs', qs);
    System.debug(
      'consumableorderdetailsRecords====>' + consumableorderdetailsRecords
    );
    data.put(
      'consumableorderdetailsRecords',
      JSON.serialize(consumableorderdetailsRecords)
    );
    data.put(
      'ConsumableorderdetailsRecordsError',
      JSON.serialize(ConsumableorderdetailsRecordsError)
    );
    // if(ConsumableorderdetailsRecordsError.size()!=null){
    //     data.put('ConsumableorderdetailsRecordsError',JSON.serialize(ConsumableorderdetailsRecordsError));
    // }
    System.debug(
      'ConsumableorderdetailsRecordsError====>' +
      ConsumableorderdetailsRecordsError
    );
    res.status = 'Success';
    res.code = 200;
    System.debug('res = ' + res);
    return res;
  }

  // Data Bean
  @TestVisible
  class ConsumableorderdetailsInfo implements Comparable {
    public Consumable_orderdetails__c orderdetails1 { get; set; }
    public Consumable_order_details2__c orderdetails2 { get; set; }
    public Product2__c Prod { get; set; }
    // 明细1
    public ConsumableorderdetailsInfo(Consumable_orderdetails__c e) {
      orderdetails1 = e;
      Prod = e.Consumable_Product__r;
    }
    // 明细2
    public ConsumableorderdetailsInfo(Consumable_order_details2__c e) {
      orderdetails2 = e;
      Prod = e.Consumable_Product__r;
    }
    // 排序
    public Integer compareTo(Object compareTo) {
      return null;
    }
  }
}
