//@ts-nocheck
import { observable, action } from 'mobx';
import { Api } from '../../services/api';
import { myLog } from '../../utils/log';
import { Alert } from 'react-native';

class ListLoanStore {
  @observable isLoading = false;
  @observable isError = false;
  @observable isLoadMore = false;
  @observable isErrorMore = false;
  @observable canLoadMore = true;
  @observable Page = 1;
  @observable LoanBriefId = 0;
  @observable SearchName = '';
  @observable PageSize = 20;
  @observable Product = {
    id: 1,
    value: 0,
    name: 'Tất cả sản phẩm',
    label: 'Tất cả sản phẩm',
  };

  @observable Status = {
    id: 1,
    value: 0,
    name: 'Tất cả',
    label: 'Tất cả',
  };

  @observable data = [];

  @observable listStatus = [];
  @observable products = [
    {
      id: 1,
      value: 0,
      name: 'Tất cả sản phẩm',
      label: 'Tất cả sản phẩm',
    },
  ];

  @observable openStatus = false;

  @observable statusLoading = false;
  @observable statusError = false;

  @action
  loadmore() {
    myLog('loadmore');
    if (!this.isLoading && !this.isLoadMore && this.canLoadMore) {
      this.Page += 1;
      this.getListLoan();
    }
  }

  @action
  reload() {
    this.Page = 1;
    this.getListLoan();
  }

  @action
  async getListLoan(onSuccess?, onFailure?) {
    if (this.Page === 1) {
      this.isLoading = true;
    } else {
      this.isLoadMore = true;
    }
    const response = await Api.getInstance().ListLoan({
      LoanBriefId: Number(this.LoanBriefId),
      SearchName: this.SearchName,
      Status: this.Status.value,
      ProductId: this.Product.value,
      Page: this.Page,
      PageSize: this.PageSize,
    });
    if (response.kind === 'ok') {
      if (this.Page === 1) {
        this.isLoading = false;
        this.isError = false;
      } else {
        this.isLoadMore = false;
        this.isErrorMore = false;
        this.canLoadMore = response.data.data.length > 0;
      }
      this.data =
        this.Page === 1
          ? response.data.data
          : [...this.data, ...response.data.data];
      if (onSuccess) {
        onSuccess(response.data.data);
      }
    } else {
      this.isLoading = false;
      this.isError = false;
      Alert.alert('Lỗi', response.Messages);
    }
  }

  @action
  async getListProduct(onSuccess?, onFailure?) {
    this.isLoading = true;
    const response = await Api.getInstance().ListProduct({});
    myLog(response);
    if (response.kind === 'ok') {
      this.isLoading = false;
      this.isError = false;
      this.products = [
        { loanProductId: 0, label: 'Tất cả sản phẩm', value: 0 },
        ...response.data.data.map((item) => ({
          ...item,
          ...{ label: item.name, value: item.loanProductId },
        })),
      ];
      if (onSuccess) {
        onSuccess(response.data.data);
      }
    } else {
      this.isLoading = false;
      this.isError = false;
      Alert.alert('Lỗi', response.Messages);
    }
  }

  @action
  async getListStatus(params, onSuccess?, onFailure?) {
    this.statusLoading = true;
    const response = await Api.getInstance().ListStatus(params);
    this.statusLoading = false;
    myLog(response);
    if (response.kind === 'ok') {
      this.statusError = false;
      this.listStatus = response.data.data.map((item) => ({
        ...item,
        ...{ label: item.name, value: item.id },
      }));
      if (onSuccess) {
        onSuccess(response.data.data);
      }
    } else {
      this.statusError = false;
      Alert.alert('Lỗi', response.Messages);
    }
  }
}

const listLoanStore = new ListLoanStore();

export default listLoanStore;
