import ApprovalData from "../../models/approval/ApprovalData";
import { findMyApprovalOptions } from "../../api/Approval";
import QueryDTO from "../../models/QueryDTO";
import { ResultBody, STORAGE_AUTH_USER } from "../../utils/HttpHelper";
import Loadable from "../../utils/LoadingHelper";
import { BasicDataSource } from "@xmky_ohos/ui";
import { Type } from "@kit.ArkUI";
import util from "@ohos.util";
import { DateUtil } from "@xmky_ohos/util";
import { AuthUserInfo } from "../../common/CommonUrl";

@ObservedV2
export class ApprovalViewModel {
  @Trace id?: string;
  @Trace employeeNumber?: string;
  @Trace employeeId?: string;
  @Trace employeeName?: string;
  @Trace departmentName?: string;
  @Trace theme?: string;
  @Trace applicationTime?: Date;
  /**
   * 当前状态(0未提交，1审批中，2已撤销，3已退回，4已完成)
   */
  @Trace currentStatus?: number;
  @Trace remark?: string;
  @Trace createTime?: Date;
  @Trace billId?: string;
  @Trace billType?: string;
  @Trace level?: number;
  @Trace delete?: number;
  @Trace type?: number;
  @Trace startTime?: Date;
  @Trace endTime?: Date;
  @Trace processInstanceId?: string;
  @Trace currentAssignee?: string;
  @Trace currentAssigneeNumber?: string;
  @Trace currentAssigneeName?: string;
  @Trace myResult?: number;
  @Trace myReason?: string;
  @Trace billSealType?: string;

  updateDetail(detail: ApprovalData) {
    this.id = detail.id;
    this.employeeNumber = detail.employeeNumber;
    this.employeeId = detail.employeeId;
    this.employeeName = detail.employeeName;
    this.departmentName = detail.departmentName;
    this.theme = detail.theme;
    this.applicationTime = !detail.applicationTime || new util.types().isDate(detail.applicationTime) ? detail.applicationTime :
    DateUtil.parse(detail.applicationTime + '');
    this.currentStatus = detail.currentStatus;
    this.remark = detail.remark;
    this.createTime =
      !detail.createTime || new util.types().isDate(detail.createTime) ? detail.createTime : DateUtil.parse(detail.createTime + '');
    this.billId = detail.billId;
    this.billType = detail.billType;
    this.level = detail.level;
    this.delete = detail.delete;
    this.type = detail.type;
    this.startTime =
      !detail.startTime || new util.types().isDate(detail.startTime) ? detail.startTime : DateUtil.parse(detail.startTime + '');
    this.endTime = !detail.endTime || new util.types().isDate(detail.endTime) ? detail.endTime : DateUtil.parse(detail.endTime + '');
    this.processInstanceId = detail.processInstanceId;
    this.currentAssignee = detail.currentAssignee;
    this.currentAssigneeNumber = detail.currentAssigneeNumber;
    this.currentAssigneeName = detail.currentAssigneeName;
    this.myResult = detail.myResult;
    this.myReason = detail.myReason;
    this.billSealType = detail.billSealType;
  }

  /**
   * @returns 审批状态
   */
  @Computed
  get currentStatusValue(): string {
    const currentUser = AppStorage.get(STORAGE_AUTH_USER) as AuthUserInfo;
    return (currentUser?.dict['BILL_CONFIRM_MATTERS/APPLICATION_STATUS'][this.currentStatus + ''] ?? '未提交') + '';
  }
}

@ObservedV2
export class ApprovalListViewModel extends BasicDataSource<ApprovalViewModel> {
  @Type(ApprovalViewModel)
  @Trace approvals: ApprovalViewModel[] = [];

  totalCount(): number {
    return this.approvals.length;
  }

  getData(index: number): ApprovalViewModel {
    return this.approvals[index];
  }

  push(approval: ApprovalViewModel) {
    this.approvals.push(approval);
    this.notifyDataAdd(this.approvals.length - 1);
  }
}

@ObservedV2
export default class ListViewModel extends Loadable {
  @Trace loading: number = 0;
  @Trace error?: string;
  @Type(ApprovalListViewModel)
  @Trace approvalListViewModel: ApprovalListViewModel = new ApprovalListViewModel();
  @Trace scroller: Scroller;
  @Trace showSideBar: boolean = false;
  @Trace pageNum: number = 1;
  @Trace approvalOption?: number | null;
  context?: UIContext;
  pageInfos?: NavPathStack;

  @Monitor('pageNum', 'approvalOption')
  async onQueryParamsChange(_monitor: IMonitor) {
    await this.queryApprovalList();
  }

  constructor(approvalOption: number | null = null, scroller: Scroller = new Scroller()) {
    super();
    this.scroller = scroller;
    this.approvalOption = approvalOption;
  }

  async queryApprovalList() {
    const res: ResultBody = await findMyApprovalOptions({
      pageNum: this.pageNum,
      pageSize: 10,
      // stringEndTime: searchData.stringEndTime ? moment(searchData.stringEndTime).add(1, 'days').format("YYYY-MM-DD") : null,
      fromApp: true,
      myResult: this.approvalOption,
    } as QueryDTO);
    if (!res.success) {
      return;
    }
    const approvals = (res.data as ResultBody).data as ApprovalData[];
    const oldCount = this.approvalListViewModel.approvals.length;
    if (1 >= this.pageNum) {
      this.approvalListViewModel.approvals = [];
    }
    this.approvalListViewModel.approvals.concat(approvals.map(approval => {
      const approvalViewModel = new ApprovalViewModel();
      approvalViewModel.updateDetail(approval);
      return approvalViewModel;
    }));
    if (1 >= this.pageNum) {
      this.approvalListViewModel.notifyDataReload();
    } else {
      for (let i = oldCount; i < this.approvalListViewModel.approvals.length; i++) {
        this.approvalListViewModel.notifyDataAdd(i);
      }
    }
  }

  secondaryContentFontSizeVp(resource: Resource) {
    return this.context!.px2vp(this.context!.fp2px(getContext().resourceManager.getNumber(resource)))
  }

  secondaryContentWidthVp(resource: Resource, length: number) {
    return this.secondaryContentFontSizeVp(resource) * length;
  }
}
在鸿蒙中我既想支持刷新又想支持加载下一页，数据都是通过调用queryApprovalList更新，怎么设计才是优雅合理的