"use strict";
const common_vendor = require("../../common/vendor.js");
const common_libraries_apiLoading = require("../../common/libraries/apiLoading.js");
const common_libraries_naviHelper = require("../../common/libraries/naviHelper.js");
const gen_Apis = require("../../gen/Apis.js");
const BILL_poster_model = require("./model.js");
const getQrCode = (data, fun) => {
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.HouseOrder.HouseOrders.GetPayQrCode, {
    id: data == null ? void 0 : data.order_id
  }).then((res) => {
    var _a;
    const fs = common_vendor.index.getFileSystemManager();
    let times = new Date().getTime();
    let codeimg = common_vendor.wx$1.env.USER_DATA_PATH + "/" + times + ".png";
    fs.writeFile({
      filePath: codeimg,
      data: (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.qr_code.slice(22),
      encoding: "base64",
      success: () => {
        return fun == null ? void 0 : fun(codeimg);
      }
    });
  });
};
const GetAndSetAlipayQrcode = () => {
  console.log("进来:");
  const context = common_vendor.index.createCanvasContext("poster_firstCanvas");
  common_vendor.index.canvasToTempFilePath({
    canvasId: "alipay_qrcode",
    x: 0,
    y: 0,
    width: 170,
    height: 170,
    destWidth: 170,
    destHeight: 170,
    success: (json) => {
      context.clearRect(0, 0, 345, 504);
      common_vendor.index.downloadFile({
        url: "https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01KP2XJ54TN4DVMCCJJ0AVNJK8.png",
        success(res) {
          var _a, _b, _c, _d, _e;
          context.drawImage(res.tempFilePath, 0, 0, 345, 504);
          context.font = "bold 18px Arial";
          context.setTextAlign("center");
          context.fillText(((_a = BILL_poster_model.useModel.orderInfo.value) == null ? void 0 : _a.name) || "-", 345 / 2, 45);
          context.font = "bold 16px Arial";
          context.fillStyle = "#DF3600";
          context.fillText(`缴费金额：¥${(_b = BILL_poster_model.useModel.orderInfo.value) == null ? void 0 : _b.amount}元`, 345 / 2, 78);
          context.font = "14px Arial";
          context.fillStyle = "#333";
          context.fillText("请使用支付宝扫码支付", 345 / 2, 450);
          context.font = "12px Arial";
          context.fillStyle = "#666";
          context.fillText(`生成时间：${(_e = (_d = (_c = BILL_poster_model.useModel) == null ? void 0 : _c.alipayQrCode) == null ? void 0 : _d.value) == null ? void 0 : _e.time} 有效时间30分钟`, 345 / 2, 475);
          context.drawImage(json.tempFilePath, 72.5, 150, 200, 200);
          context.restore();
          context.draw();
        }
      });
      console.log(json, "支付宝二维码路径");
    },
    fail: (err) => {
      console.log(err, "支付宝二维码路径失败");
    }
  });
};
const GetAlipayQrcode = () => {
  var _a, _b, _c, _d;
  if ((_c = (_b = (_a = BILL_poster_model.useModel) == null ? void 0 : _a.alipayQrCode) == null ? void 0 : _b.value) == null ? void 0 : _c.payinfo) {
    GetAndSetAlipayQrcode();
    return;
  }
  common_libraries_apiLoading.getApiLoading(gen_Apis.Apis.HouseOrder.HouseOrders.AlipayQrCode, {
    id: (_d = BILL_poster_model.useModel.orderInfo.value) == null ? void 0 : _d.order_id,
    pay_type: "Alipay"
  }).then((res) => {
    BILL_poster_model.useModel.alipayQrCode.value = { ...res == null ? void 0 : res.data, time: common_vendor.dayjs().format("YYYY-MM-DD HH:mm") };
    SetAlipayQrcode();
  });
};
const SetAlipayQrcode = () => {
  var _a, _b, _c;
  const qr = new common_vendor.UQRCode();
  qr.data = (_c = (_b = (_a = BILL_poster_model.useModel) == null ? void 0 : _a.alipayQrCode) == null ? void 0 : _b.value) == null ? void 0 : _c.payinfo;
  qr.size = 170;
  qr.make();
  const canvasContext = common_vendor.index.createCanvasContext("alipay_qrcode");
  qr.canvasContext = canvasContext;
  qr.drawCanvas();
  setTimeout(() => {
    GetAndSetAlipayQrcode();
  }, 300);
};
const SetCustQrcode = () => {
  const context = common_vendor.index.createCanvasContext("poster_firstCanvas");
  common_vendor.index.downloadFile({
    url: "https://pay-prod-1369486729.cos.ap-guangzhou.myqcloud.com/uploads/cs-test/01KP2XJ54TN4DVMCCJJ0AVNJK8.png",
    success(res) {
      var _a, _b, _c, _d;
      context.clearRect(0, 0, 345, 504);
      context.drawImage(res.tempFilePath, 0, 0, 345, 504);
      context.font = "bold 18px Arial";
      context.setTextAlign("center");
      context.fillText(((_a = BILL_poster_model.useModel.custQrcodeInfo.value) == null ? void 0 : _a.name) || "-", 345 / 2, 45);
      context.font = "bold 16px Arial";
      context.fillStyle = "#DF3600";
      context.fillText(`缴费金额：¥${(_b = BILL_poster_model.useModel.custQrcodeInfo.value) == null ? void 0 : _b.amount}元`, 345 / 2, 78);
      context.font = "14px Arial";
      context.fillStyle = "#333";
      context.fillText("请使用微信扫码支付", 345 / 2, 450);
      context.font = "12px Arial";
      context.fillStyle = "#666";
      context.fillText(`生成时间：${(_c = BILL_poster_model.useModel.custQrcodeInfo.value) == null ? void 0 : _c.time}`, 345 / 2, 475);
      context.drawImage((_d = BILL_poster_model.useModel.custQrcodeInfo.value) == null ? void 0 : _d.url, 72.5, 150, 200, 200);
      context.restore();
      context.draw();
    }
  });
};
const method = {
  //method
  getHouseDetail(id) {
  },
  setCanvas(data) {
    getQrCode(data, (url) => {
      BILL_poster_model.useModel.custQrcodeInfo.value = { ...data, url };
      SetCustQrcode();
      console.log(url, "二维码路径");
    });
  },
  onDownloadPosters() {
    common_vendor.index.showLoading({
      title: "正在保存中..."
    });
    common_vendor.index.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: 345,
      height: 504,
      destWidth: 345 * 4,
      destHeight: 504 * 4,
      canvasId: "poster_firstCanvas",
      success: function(res) {
        common_vendor.index.hideLoading();
        common_vendor.index.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function() {
            console.log("save success");
            common_libraries_naviHelper.showToast("保存成功！");
          },
          fail() {
            common_vendor.index.hideLoading();
          }
        });
        console.log(res.tempFilePath);
      },
      fail() {
        common_vendor.index.hideLoading();
      }
    });
  },
  initActualImages: async (id) => {
  },
  choiceImgPopControl() {
    var _a;
    this.initActualImages((_a = BILL_poster_model.useModel.houseDetail.value) == null ? void 0 : _a.id);
  },
  copyLink() {
  },
  changeTab(idx) {
    BILL_poster_model.useModel.tabCurrent.value = idx;
    if (idx) {
      GetAlipayQrcode();
    } else {
      SetCustQrcode();
    }
  }
};
exports.method = method;
