// pages/card/card.ts
// import { card } from '../../utils/painter'
import { info } from "../../apis/phone";
import { businessCard } from "../../apis/card";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardPainter: {},
    isEdit: false,
    companyName: "",
    consumerId: "",
    email: "",
    introduction: "",
    name: "",
    office: "",
    phone: "",
    businessCard: null,
    labelOptions: [],
    dialogExplain: false,
    type: 0,
    info: {},
    dialogDetail: false,
    detailInfo: '',
    detailTitle: ''
  },
  // saveImage() {
  //   let screenWidth = wx.getSystemInfoSync().screenWidth;
  //   let screenHeight = wx.getSystemInfoSync().screenHeight;
  //   let canvasContext = wx.createCanvasContext('myCanvas');
  //   wx.createSelectorQuery().select('#saveArea').boundingClientRect(function (rect) {
  //     let top = rect.top;
  //     let left = rect.left;
  //     let width = rect.width;
  //     let height = rect.height;
  //     console.log(top, left, width, height);

  //     canvasContext.drawImage('../../assets/images/card/card_bg.png', 0, 100, screenWidth, screenHeight);

  //     // 将第二个绘制操作放在canvasContext.draw()方法中
  //     canvasContext.draw(false, () => {
  //       // 在回调函数中调用wx.canvasToTempFilePath()方法进行保存图片
  //       wx.canvasToTempFilePath({
  //         canvasId: 'myCanvas',
  //         x: left,
  //         y: top,
  //         width: width,
  //         height: height,
  //         destWidth: screenWidth * 2,
  //         destHeight: screenHeight * 2,
  //         success: function (res) {
  //           wx.saveImageToPhotosAlbum({
  //             filePath: res.tempFilePath,
  //             success: function () {
  //               wx.showToast({
  //                 title: '保存成功'
  //               });
  //             },
  //             fail: function () {
  //               wx.showToast({
  //                 title: '保存失败'
  //               });
  //             }
  //           });
  //         }
  //       });
  //     });
  //   }).exec();
  // },
  handleEdit() {
    console.log('handleEdit');
    const { isEdit } = this.data
    this.setData({
      isEdit: !isEdit
    })
    // if (isEdit) {

    //   const { companyName, email, introduction, office, phone } = this.data
    //   const payload = {
    //     companyName, email, introduction, office, phone
    //   }
    //   businessCard(payload).then(res => {
    //     console.log(res);
    //     this.getInfo(phone)
    //   })
    // }
    // },
  },
  handleCancelEdit() {
    console.log('handleCancelEdit');

    this.setData({
      isEdit: false
    })
  },
  handleSearch(){
    wx.navigateTo({
      url:`/pages/searchResult/searchResult?phone=${this.data.phone}`
    })
  },

  handleSave() {
    const { companyName, email, introduction, office, phone } = this.data
    const payload = {
      companyName, email, introduction, office, phone
    }
    businessCard(payload).then((res: any) => {
      console.log(res);
      if (res.c === 200) {
        wx.showToast({
          icon: 'none',
          title: '保存成功'
        })
        this.getInfo(phone)
        this.setData({
          isEdit: false
        })
      }
    })
  },
  handleSaveContract() {
    const info = this.data.info.businessCard
    wx.addPhoneContact({
      firstName: info.name,
      email: info.email,
      organization: info.companyName,
      title: info.office,
      mobilePhoneNumber:this.data.phone
    })

  },
  handleDetail() {
    const { phone } = this.data
    wx.navigateTo({
      url: `/pages/searchResult/searchResult?phone=${phone}`
    })
  },
  closeDetail() {
    this.setData({
      dialogDetail: false
    })
  },
  showExplain() {
    this.setData({
      dialogExplain: true
    })
  },
  closeExplain() {
    this.setData({
      dialogExplain: false
    })
  },
  // onImgOK(e: WechatMiniprogram.CustomEvent) {
  //   const { path } = e.detail
  //   this.setData({
  //     shareImg: path
  //   })
  //   // wx.showShareImageMenu({  //分享给朋友
  //   //   path,
  //   //   success: (res) => {
  //   //     console.log("分享成功：", res);
  //   //   },
  //   //   fail: (err) => {
  //   //     console.log("分享失败：", err);
  //   //     wx.showToast({
  //   //       title: "分享失败",
  //   //       duration: 2000
  //   //     })
  //   //   },
  //   // })
  // },
  handleDialogDetail(e: WechatMiniprogram.BaseEvent) {
    console.log(e);
    const { title, info } = e.currentTarget.dataset
    if (info) {
      this.setData({
        dialogDetail: true,
        detailInfo: info,
        detailTitle: title
      })
    }
  },
  getInfo(phone: string) {
    const { type } = this.data
    info({ phone, type, businessCard: true }).then((res: any) => {
      let { labelVoList } = res.d
      if (res.d.businessCard) {
        const { companyName, email, introduction, office } = res.d.businessCard
        if(labelVoList){
          labelVoList = labelVoList.slice(0, 1)
        }
        
        this.setData({
          businessCard: res.d.businessCard,
          companyName,
          email,
          introduction,
          office,
          labelOptions: labelVoList,
          info: res.d
        })
      } else {
        this.setData({
          info: res.d,
          labelOptions: labelVoList,
        })
      }
      // this.setData({
      //   businessCard: res.d.businessCard,
      //   companyName,
      //   email,
      //   introduction,
      //   office,
      //   labelOptions: res.d.labelVoList.slice(0, 2),
      //   info: res.d
      // })
    })
    // this.downLoadCard()

  },
  phoneCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone,
    })
  },
  // async downLoadCard() {
  //   console.log('downLoadCard');
  //   const cardPainter = await card()
  //   this.setData({
  //     cardPainter
  //   })
  // },
  handleCopy(e) {
    const { email } = e.currentTarget.dataset
    wx.setClipboardData({
      data: email,
      success(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        })
        // wx.getClipboardData({
        //   success(res) {
        //     console.log(res.data) // data
        //   }
        // })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    const { type, phone } = options
    this.setData({
      type: Number(type),
      phone
    })
    this.getInfo(phone)
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '您好，这是我的口碑名片，请收下',
          imageUrl: this.data.shareImg,
          path: `/pages/card/card?phone=${this.data.phone}&type=1`,
        })
      })
    })
    return {
      title: '您好，这是我的口碑名片，请收下',
      path: '/pages/card/card',
      promise
    }
  }
})