"use strict";
const common_vendor = require("../../../common/vendor.js");
const INDEX_bill_list_model = require("./model.js");
const common_libraries_apiLoading = require("../../../common/libraries/apiLoading.js");
const gen_Apis = require("../../../gen/Apis.js");
const common_libraries_naviHelper = require("../../../common/libraries/naviHelper.js");
const handleGetList = () => {
  var _a, _b;
  INDEX_bill_list_model.useModel.billsLength.value = 0;
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Bill.HouseBills.MyBillYearList, (_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value).then((res) => {
    var _a2;
    (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.map((i) => {
      var _a3, _b2;
      INDEX_bill_list_model.useModel.billsLength.value += ((_a3 = i == null ? void 0 : i.bills) == null ? void 0 : _a3.length) || 0;
      (_b2 = i == null ? void 0 : i.bills) == null ? void 0 : _b2.map((k) => {
        var _a4;
        k.checked = true;
        (_a4 = k == null ? void 0 : k.details) == null ? void 0 : _a4.map((m) => {
          m.checked = true;
        });
      });
    });
    INDEX_bill_list_model.useModel.items.value = [...INDEX_bill_list_model.useModel.items.value, ...res == null ? void 0 : res.data];
    setCalculateTotalPrice();
  });
};
const setCalculateTotalPrice = () => {
  var _a, _b, _c;
  let total_amount = 0;
  (_c = (_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value) == null ? void 0 : _c.map((res) => {
    var _a2;
    (_a2 = res == null ? void 0 : res.bills) == null ? void 0 : _a2.map((k) => {
      total_amount += parseFloat(k.amount || 0);
    });
  });
  INDEX_bill_list_model.useModel.total_amount.value = parseFloat(total_amount.toFixed(2));
};
const method = {
  handleGetList,
  handleCollapseClick: (index, idx, res) => {
    var _a, _b, _c, _d;
    let show = (_d = (_c = (_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value[index]) == null ? void 0 : _c.bills[idx]) == null ? void 0 : _d.show;
    INDEX_bill_list_model.useModel.items.value[index].bills[idx].show = !show;
  },
  hanldeSearch: (res) => {
    var _a, _b;
    INDEX_bill_list_model.useModel.items.value = [];
    INDEX_bill_list_model.useModel.formData.value = { ...(_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value, ...res, page: 1 };
    handleGetList();
  },
  handleCalculatePrice(index, idx, idxs, res) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
    if (INDEX_bill_list_model.useModel.currentTabs.value) {
      return;
    }
    let checked = res == null ? void 0 : res.checked;
    let total_amount = parseFloat((_d = (_c = (_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value[index]) == null ? void 0 : _c.bills[idx]) == null ? void 0 : _d.amount);
    let amount = parseFloat(
      (_i = (_h = (_g = (_f = (_e = INDEX_bill_list_model.useModel) == null ? void 0 : _e.items) == null ? void 0 : _f.value[index]) == null ? void 0 : _g.bills[idx]) == null ? void 0 : _h.details[idxs]) == null ? void 0 : _i.total_payable_amount
    );
    INDEX_bill_list_model.useModel.items.value[index].bills[idx].details[idxs].checked = !checked;
    INDEX_bill_list_model.useModel.items.value[index].bills[idx].amount = !checked ? total_amount + amount : total_amount - amount;
    if (INDEX_bill_list_model.useModel.items.value[index].bills[idx].amount <= 0) {
      INDEX_bill_list_model.useModel.items.value[index].bills[idx].checked = false;
    } else {
      if (!((_m = (_l = (_k = (_j = INDEX_bill_list_model.useModel) == null ? void 0 : _j.items) == null ? void 0 : _k.value[index]) == null ? void 0 : _l.bills[idx]) == null ? void 0 : _m.checked)) {
        INDEX_bill_list_model.useModel.items.value[index].bills[idx].checked = true;
      }
    }
    setCalculateTotalPrice();
  },
  handleCollapseAllCheckedBtn: async (index, idx, res) => {
    var _a, _b, _c, _d, _e;
    let checked = res == null ? void 0 : res.checked;
    let total_amount = 0;
    INDEX_bill_list_model.useModel.items.value[index].bills[idx].checked = !checked;
    if (checked) {
      INDEX_bill_list_model.useModel.items.value[index].bills[idx].amount = 0;
    }
    (_e = (_d = (_c = (_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value[index]) == null ? void 0 : _c.bills[idx]) == null ? void 0 : _d.details) == null ? void 0 : _e.map((i) => {
      i.checked = !checked;
      if (!checked) {
        total_amount += parseFloat(i.total_payable_amount);
      }
      INDEX_bill_list_model.useModel.items.value[index].bills[idx].amount = total_amount;
    });
    setCalculateTotalPrice();
    console.log(index, idx);
  },
  handleQuery(res) {
    common_vendor.index.navigateTo({
      url: `/INDEX/bill/show/index?asset_houses_id=${res == null ? void 0 : res.asset_houses_id}&month=${res == null ? void 0 : res.month}`
    });
    console.log(res, "res");
  },
  handleSubmitOrder() {
    var _a, _b, _c, _d, _e, _f, _g;
    let bill_ids = [];
    (_c = (_b = (_a = INDEX_bill_list_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value) == null ? void 0 : _c.map((res) => {
      var _a2;
      (_a2 = res == null ? void 0 : res.bills) == null ? void 0 : _a2.map((k) => {
        var _a3;
        (_a3 = k == null ? void 0 : k.details) == null ? void 0 : _a3.map((i) => {
          if (k == null ? void 0 : k.checked) {
            bill_ids.push(i == null ? void 0 : i.id);
          }
        });
      });
    });
    if (!(bill_ids == null ? void 0 : bill_ids.length)) {
      common_libraries_naviHelper.showToast("至少选择一个账单！");
      return;
    }
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.HouseOrder.HouseOrders.Store, {
      bill_ids,
      amount: (_e = (_d = INDEX_bill_list_model.useModel) == null ? void 0 : _d.total_amount) == null ? void 0 : _e.value,
      total_payable_amount: (_g = (_f = INDEX_bill_list_model.useModel) == null ? void 0 : _f.total_amount) == null ? void 0 : _g.value
    }).then((res) => {
      var _a2;
      common_vendor.index.navigateTo({
        url: `/INDEX/pay/index?id=${(_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.id}`
      });
    });
  }
};
exports.method = method;
