"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 BILL_outstanding_fee_show_model = require("./model.js");
const auth = common_store_useWeAppAuthStore.useWeAppAuthStore();
const setCalculateTotalPrice = () => {
  var _a, _b, _c;
  let total_amount = 0;
  (_c = (_b = (_a = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a.houseBillsData) == 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);
      }
    });
  });
  BILL_outstanding_fee_show_model.useModel.total_amount.value = parseFloat(total_amount == null ? void 0 : total_amount.toFixed(2));
};
const getHouseBills = (type) => {
  var _a, _b;
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Bill.HouseBills.List, {
    ...(_b = (_a = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value,
    type
  }).then((res) => {
    var _a2;
    console.log(res == null ? void 0 : res.data, "res");
    (_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.map((k) => {
      var _a3;
      k.checked = true;
      (_a3 = k == null ? void 0 : k.bills) == null ? void 0 : _a3.map((i) => {
        i.checked = true;
      });
    });
    BILL_outstanding_fee_show_model.useModel.houseBillsData.value = [...BILL_outstanding_fee_show_model.useModel.houseBillsData.value, ...res == null ? void 0 : res.data];
    BILL_outstanding_fee_show_model.useModel.metaData.value = res == null ? void 0 : res.meta;
    setCalculateTotalPrice();
  });
};
const getHouseWorkOrders = () => {
  var _a, _b;
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.WorkOrder.HouseWorkOrders.GetHouseWorkOrders, (_b = (_a = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value).then(
    (res) => {
      BILL_outstanding_fee_show_model.useModel.listData.value = [...BILL_outstanding_fee_show_model.useModel.listData.value, ...res == null ? void 0 : res.data];
      BILL_outstanding_fee_show_model.useModel.metaData.value = res == null ? void 0 : res.meta;
    }
  );
};
const getBillIds = () => {
  var _a, _b, _c;
  let bill_ids = [];
  (_c = (_b = (_a = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a.houseBillsData) == 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;
  }
  return bill_ids;
};
const getHouseOccupants = () => {
  var _a, _b;
  let ownerArr = [];
  let noOwnerArr = [];
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Archive.HouseOccupants.List, (_b = (_a = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a.formData) == null ? void 0 : _b.value).then((res) => {
    var _a2;
    (_a2 = res.data) == null ? void 0 : _a2.map((i) => {
      if ((i == null ? void 0 : i.house_relation) === "Owner") {
        ownerArr == null ? void 0 : ownerArr.push(i);
      } else {
        noOwnerArr == null ? void 0 : noOwnerArr.push(i);
      }
    });
    BILL_outstanding_fee_show_model.useModel.listData.value = [
      { label: "产权人", list: ownerArr },
      { label: "非产权人", list: noOwnerArr }
    ];
  });
};
const method = {
  getShow: (id) => {
    common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.Bill.HouseBills.HouseArrearageAmount, { asset_houses_id: id }).then((res) => {
      BILL_outstanding_fee_show_model.useModel.showData.value = res == null ? void 0 : res.data;
    });
  },
  getHouseBills,
  loadMore(page) {
    BILL_outstanding_fee_show_model.useModel.formData.value.page = page;
    if (BILL_outstanding_fee_show_model.useModel.tabsCurrent.value === 0) {
      getHouseBills(1);
    }
    if (BILL_outstanding_fee_show_model.useModel.tabsCurrent.value === 1) {
      getHouseWorkOrders();
    }
    if (BILL_outstanding_fee_show_model.useModel.tabsCurrent.value === 2) {
      getHouseOccupants();
    }
  },
  handleTabsChange(idx) {
    BILL_outstanding_fee_show_model.useModel.tabsCurrent.value = idx;
    BILL_outstanding_fee_show_model.useModel.formData.value.page = 1;
    if (idx === 0) {
      BILL_outstanding_fee_show_model.useModel.houseBillsData.value = [];
      getHouseBills(1);
    }
    if (idx === 1) {
      BILL_outstanding_fee_show_model.useModel.listData.value = [];
      getHouseWorkOrders();
    }
    if (idx === 2) {
      BILL_outstanding_fee_show_model.useModel.listData.value = [];
      getHouseOccupants();
    }
  },
  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 = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a.total_amount) == null ? void 0 : _b.value,
        amount: (_d = (_c = BILL_outstanding_fee_show_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, _k;
        common_vendor.index.navigateTo({
          url: `/BILL/poster/index?id=${(_b2 = (_a2 = BILL_outstanding_fee_show_model.useModel.showData) == null ? void 0 : _a2.value) == null ? void 0 : _b2.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 = BILL_outstanding_fee_show_model.useModel.showData) == null ? void 0 : _e.value) == null ? void 0 : _f.full_name}&amount=${((_h = (_g = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _g.total_amount) == null ? void 0 : _h.value) || ((_k = (_j = (_i = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _i.showData) == null ? void 0 : _j.value) == null ? void 0 : _k.total_pending_amount)}`
        });
      });
    }
  },
  // handleSelect(idx: number) {
  //   let isAll = true
  //   let is = useModel.dataList.value[idx].is_select
  //   useModel.dataList.value[idx].is_select = !is
  //   useModel.dataList.value?.map((res: { is_select: boolean }) => {
  //     if (!res.is_select) {
  //       isAll = false
  //     }
  //   })
  //   useModel.allSelectStatus.value = isAll
  // },
  // handleAllSelect() {
  //   useModel.allSelectStatus.value = !useModel?.allSelectStatus?.value
  //   useModel.dataList.value?.map((res: any) => {
  //     res.is_select = useModel.allSelectStatus.value
  //   })
  // },
  handleSetContact(res) {
    var _a, _b, _c;
    common_libraries_apiLoading.getApiLoading((_c = (_b = (_a = gen_Apis.Apis) == null ? void 0 : _a.Archive) == null ? void 0 : _b.HouseOccupants) == null ? void 0 : _c.ChangeContact, {
      house_occupants_id: res == null ? void 0 : res.id,
      is_contact: !(res == null ? void 0 : res.is_contact)
    }).then((res2) => {
      BILL_outstanding_fee_show_model.useModel.listData.value = [];
      getHouseOccupants();
    });
  },
  handleNotice(res) {
    let arr = [
      {
        asset_houses_id: res == null ? void 0 : res.id,
        total_unpaid_amount: parseFloat(res == null ? void 0 : res.total_pending_amount)
      }
    ];
    common_vendor.index.navigateTo({
      url: `/BILL/information_template/index?data=${JSON.stringify(arr)}`
    });
  },
  setCalculateTotalPrice,
  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=${BILL_outstanding_fee_show_model.useModel.formData.value.asset_houses_id}`,
              title: `分享给您${(_c = (_b = (_a2 = BILL_outstanding_fee_show_model.useModel) == null ? void 0 : _a2.showData) == null ? void 0 : _b.value) == null ? void 0 : _c.full_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;
