// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
let iconFrom = {};
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show1: false,
    cardData: {},
    cardMessage: {},
    // 图片
    loadingImg: true,
    card_id: "",
    cardDataInfo: {
      card_statistics: {},
    },
    area_name: "",
    role_name: {},
    drawing: [],
    savebtnText: "保存名片图片",
    // 画图
    avatar: "",
    // 本地存储的数据
    card_mobile: "/image/card_img_b.png",
    card_wechat: "/image/card_img_c.png",
    area_nameIcon: "/image/card_img_d.png",
    visit: "/image/card_img_a.png",
    canvasWidth: "",
    canvasHeight: "",
    showBtn: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log(option, "option");
    // this.getCardDate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCardDate();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 网络图标存本地
  saveImg() {
    wx.getImageInfo({
      src: this.data.visit,
      success: (res) => {
        console.log(res);
        this.setData({
          visit: res.path,
        });
        iconFrom.visit = this.data.visit;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
        console.log(res, "错误的回调1");
      },
    });
    wx.getImageInfo({
      src: this.data.card_mobile,
      success: (res) => {
        console.log(res);
        this.setData({
          card_mobile: res.path,
        });
        iconFrom.card_mobile = this.data.card_mobile;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
        console.log(res, "错误的回调2");
      },
    });
    wx.getImageInfo({
      src: this.data.card_wechat,
      success: (res) => {
        this.setData({
          card_wechat: res.path,
        });
        iconFrom.card_wechat = this.data.card_wechat;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
        console.log(res, "错误的回调3");
      },
    });
    wx.getImageInfo({
      src: this.data.area_nameIcon,
      success: (res) => {
        this.setData({
          area_nameIcon: res.path,
        });
        iconFrom.area_nameIcon = this.data.area_nameIcon;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
        console.log(res, "错误的回调4");
      },
    });
    wx.downloadFile({
      url: this.data.cardDataInfo.background, // 服务器返回的图片地址
      success: (res) => {
        // res.tempFilePath是网络图片的本地地址
        let Path = res.tempFilePath;
        this.setData({
          "cardDataInfo.background": Path,
        });
        iconFrom.background = res.tempFilePath;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
      },
    });
    wx.downloadFile({
      url: this.data.cardDataInfo.card_avatar, // 服务器返回的图片地址
      success: (res) => {
        // res.tempFilePath是网络图片的本地地址
        this.setData({
          "cardDataInfo.card_avatar": res.tempFilePath,
        });
        iconFrom.card_avatar = res.tempFilePath;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
        console.log(res, "失败");
      },
    });

    wx.downloadFile({
      url: this.data.cardDataInfo.min_code, // 服务器返回的图片地址
      success: (res) => {
        // res.tempFilePath是网络图片的本地地址
        this.setData({
          "cardDataInfo.min_code": res.tempFilePath,
        });
        iconFrom.min_code = res.tempFilePath;
        wx.setStorage({
          key: "iconFrom",
          data: iconFrom,
        });
      },
      fail: function (res) {
        // 失败回调
        console.log(res, "错误的回调");
      },
    });
  },
  getCardDate() {
    let urlStr = app.getNetAddresss("plugin.business-card.frontend.controllers.center.card.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          let area_name = null;
          if (response.data.area_name.length != 0) {
            if (response.data.area_name[3] == null) {
              response.data.area_name[3] = "";
            }
            if (response.data.has_one_card.address == null) {
              response.data.has_one_card.address = "";
            }
            area_name = response.data.area_name[0] + response.data.area_name[1] + response.data.area_name[2] + response.data.area_name[3] + response.data.has_one_card.address;
          }

          this.setData({
            cardData: response.data,
            cardMessage: response.data.has_one_card,
            role_name: response.data.role_name,
            area_name: area_name,
            card_id: response.data.has_one_card.id,
          });
          this.getData();
        } else {
          wx.showToast({
            title: response.msg,
            icon: "none",
          });
          setTimeout(() => {
            wx.reLaunch({
              url: "/packageG/index/index",
            });
          }, 2000);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.business-card.frontend.controllers.center.card-pic.index");
    urlStr += "&card_id=" + this.data.card_id + "&card_type=1";
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            cardDataInfo: response.data,
            role_name: response.data.role_name,
          });
          this.saveImg();
          let area_name = "";
          if (this.data.cardData.area_name[0]) {
            area_name = this.data.cardData.area_name[0] + this.data.cardData.area_name[1] + this.data.cardData.area_name[2] + this.data.cardData.area_name[3] + this.data.cardData.has_one_card.address;
          }
          let info = {};
          if (response.data.role_name.level_name == null) {
            response.data.role_name.level_name = "";
          }
          info.card_name = response.data.card_name;
          info.card_mobile = response.data.card_mobile;
          info.card_wechat = response.data.card_wechat;
          info.area_name = area_name;
          info.visit = "人气" + response.data.card_statistics.visit;
          info.card_name = response.data.card_name + "名片码";
          info.shop_name = `名片由【${response.data.shop_name}】生成`;
          info.role_name = response.data.role_name.role_name + "|" + response.data.role_name.level_name;
          wx.setStorage({
            key: "info",
            data: info,
          });
        } else {
          // wx.showToast({
          //   title: response.msg,
          //   icon: 'none'
          // })
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  ctiuShow(e) {
    let index = e.target.dataset.index || e.currentTarget.dataset.index;
    if (index == "1") {
      this.setData({
        show1: true,
      });
    } else if (index == "2") {
      this.setData({
        show1: false,
      });
    }
  },
  toPage(val) {
    console.log(val);
    let url = val.target.dataset.url || val.currentTarget.dataset.url;
    let type = val.target.dataset.type || val.currentTarget.dataset.type;
    wx.getStorage({
      key: "iconFrom",
      success: (res) => {
        if (res.data) {
          wx.navigateTo({
            url: "/packageB/member/business_card/" + url + "/" + url + "?card_id=" + this.data.cardMessage.id + "&type=" + type,
          });
        }
      },
    });
    this.setData({
      show1: false,
    });
  },
  callMobile() {
    wx.makePhoneCall({
      phoneNumber: this.data.cardMessage.card_mobile, // 仅为示例，并非真实的电话号码
      success: (res) => {
        console.log(res, "成功");
      },
      fail: (error) => {
        console.log(error, "失败");
      },
    });
  },
  // 跳转到名片
  goToCard() {
    wx.navigateTo({
      url: "/packageB/member/business_card/BusinessCard/BusinessCard" + "?card_id=" + this.data.card_id + "&mark_id=" + this.data.cardMessage.id + "&mark=card",
    });
  },
  closeOperateListPopup() {
    this.setData({
      show1: false,
    });
  },
});
