"use strict";
const common_vendor = require("../../common/vendor.js");
require("../../common/libraries/request.js");
require("../../common/store/useWatermarkStore.js");
const common_store_useWeAppAuthStore = require("../../common/store/useWeAppAuthStore.js");
require("../../common/store/useWorkStore.js");
const common_libraries_apiLoading = require("../../common/libraries/apiLoading.js");
const common_libraries_naviHelper = require("../../common/libraries/naviHelper.js");
const common_libraries_sendWxWork = require("../../common/libraries/sendWxWork.js");
const gen_Apis = require("../../gen/Apis.js");
const CUSTOMER_list_bill_model = require("./model.js");
const auth = common_store_useWeAppAuthStore.useWeAppAuthStore();
const handleGetList = () => {
  var _a, _b;
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Bill.HouseBills.List, { ...(_b = (_a = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value, type: 1 }).then((res) => {
    var _a2;
    (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.map((i) => {
      var _a3;
      i.checked = true;
      (_a3 = i == null ? void 0 : i.bills) == null ? void 0 : _a3.map((k) => {
        k.checked = true;
      });
    });
    CUSTOMER_list_bill_model.useModel.items.value = [...CUSTOMER_list_bill_model.useModel.items.value, ...res == null ? void 0 : res.data];
    CUSTOMER_list_bill_model.useModel.metaData.value = res == null ? void 0 : res.meta;
  });
};
const getBillIds = () => {
  var _a, _b, _c;
  let bill_ids = [];
  (_c = (_b = (_a = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value) == null ? void 0 : _c.map((res) => {
    var _a2, _b2;
    if ((_a2 = res == null ? void 0 : res.bills) == null ? void 0 : _a2.length) {
      (_b2 = res == null ? void 0 : res.bills) == null ? void 0 : _b2.map((i) => {
        if (i == null ? void 0 : i.checked) {
          bill_ids == null ? void 0 : bill_ids.push(i == null ? void 0 : i.id);
        }
      });
    }
  });
  if (!(bill_ids == null ? void 0 : bill_ids.length)) {
    common_libraries_naviHelper.showToast("请至少选择一个月账单！");
    return false;
  }
  setCalculateTotalPrice();
  return bill_ids;
};
const setCalculateTotalPrice = () => {
  var _a, _b, _c;
  let total_amount = 0;
  (_c = (_b = (_a = CUSTOMER_list_bill_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) => {
      if (k == null ? void 0 : k.checked) {
        total_amount += parseFloat(k.amount || 0);
      }
    });
  });
  CUSTOMER_list_bill_model.useModel.total_amount.value = total_amount;
};
const method = {
  handleGetList,
  handleCollapseClick: (index, res) => {
    var _a, _b, _c;
    let show = (_c = (_b = (_a = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value[index]) == null ? void 0 : _c.show;
    CUSTOMER_list_bill_model.useModel.items.value[index].show = !show;
  },
  hanldeSearch: (res) => {
    var _a, _b;
    CUSTOMER_list_bill_model.useModel.items.value = [];
    CUSTOMER_list_bill_model.useModel.formData.value = { ...(_b = (_a = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value, ...res, page: 1 };
    handleGetList();
  },
  handleLoadMore(params) {
    CUSTOMER_list_bill_model.useModel.formData.value.page = params == null ? void 0 : params.page;
    handleGetList();
  },
  handleCalculatePrice(index, idx, res) {
    var _a;
    let checked = res == null ? void 0 : res.checked;
    CUSTOMER_list_bill_model.useModel.items.value[index].bills[idx].checked = !checked;
    if (checked) {
      CUSTOMER_list_bill_model.useModel.items.value[index].checked = false;
    } else {
      let allChecked = (_a = CUSTOMER_list_bill_model.useModel.items.value[index].bills) == null ? void 0 : _a.every(
        (item) => item == null ? void 0 : item.checked
      );
      CUSTOMER_list_bill_model.useModel.items.value[index].checked = allChecked;
      console.log(allChecked);
    }
  },
  handleCollapseAllCheckedBtn: async (index, res) => {
    var _a, _b, _c, _d;
    let checked = res == null ? void 0 : res.checked;
    CUSTOMER_list_bill_model.useModel.items.value[index].checked = !checked;
    (_d = (_c = (_b = (_a = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a.items) == null ? void 0 : _b.value[index]) == null ? void 0 : _c.bills) == null ? void 0 : _d.map((i) => {
      i.checked = !checked;
    });
  },
  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 = CUSTOMER_list_bill_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 = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _d.total_amount) == null ? void 0 : _e.value,
      total_payable_amount: (_g = (_f = CUSTOMER_list_bill_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}`
      });
    });
  },
  handleQRCode() {
    var _a, _b, _c, _d;
    let bill_ids = getBillIds() || [];
    if (bill_ids == null ? void 0 : bill_ids.length) {
      common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.HouseOrder.HouseOrders.Store, {
        bill_ids: bill_ids || [],
        total_payable_amount: (_b = (_a = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a.total_amount) == null ? void 0 : _b.value,
        amount: (_d = (_c = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _c.total_amount) == null ? void 0 : _d.value
      }).then((res) => {
        var _a2, _b2, _c2, _d2, _e, _f, _g, _h, _i, _j;
        common_vendor.index.navigateTo({
          url: `/BILL/poster/index?id=${(_b2 = (_a2 = CUSTOMER_list_bill_model.useModel.formData) == null ? void 0 : _a2.value) == null ? void 0 : _b2.asset_houses_id}&order_id=${(_c2 = res == null ? void 0 : res.data) == null ? void 0 : _c2.id}&time=${(_d2 = res == null ? void 0 : res.data) == null ? void 0 : _d2.created_at}&name=${(_f = (_e = CUSTOMER_list_bill_model.useModel.formData) == null ? void 0 : _e.value) == null ? void 0 : _f.name}&amount=${((_h = (_g = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _g.total_amount) == null ? void 0 : _h.value) || ((_j = (_i = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _i.total_amount) == null ? void 0 : _j.value)}`
        });
      });
    }
  },
  handleSelectExternalContact() {
    var _a;
    let bill_ids = getBillIds() || [];
    console.log(bill_ids, "bill_ids");
    if (!(bill_ids == null ? void 0 : bill_ids.length)) {
      return false;
    }
    if (((_a = auth == null ? void 0 : auth.data) == null ? void 0 : _a.environment) === "wxwork") {
      common_vendor.index.qy.getContext({
        success: function(res) {
          var _a2, _b, _c;
          let entry = res.entry;
          if (entry === "single_chat_tools") {
            common_libraries_sendWxWork.sendChatMessage({
              page: `/INDEX/bill_order/index.html?bill_ids=${JSON.stringify(bill_ids)}&house_id=${CUSTOMER_list_bill_model.useModel.formData.value.asset_houses_id}`,
              title: `分享给您${(_c = (_b = (_a2 = CUSTOMER_list_bill_model.useModel) == null ? void 0 : _a2.formData) == null ? void 0 : _b.value) == null ? void 0 : _c.name}物业账单`,
              imgUrl: "https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01K5B5RY2TQQAJ1GT2RZSK426V.jpg"
            });
          } else {
            common_libraries_sendWxWork.navigateToMiniProgram({
              path: "/pages/forward/index?source=OutstandingFeeList",
              envVersion: "develop",
              extraData: { source: "OutstandingFeeList" }
            });
          }
        }
      });
    } else {
      common_libraries_sendWxWork.navigateToMiniProgram({
        path: "/pages/forward/index?source=OutstandingFeeList",
        envVersion: "develop",
        extraData: { source: "OutstandingFeeList" }
      });
    }
  }
};
exports.method = method;
