// pages/xiangmuhezuo/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageDone: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    wx.showLoading({
      title: "加载中...",
    });
  },
  formSubmit (e) {
    console.log(e.detail.value);
    let formObj = e.detail.value;
    var myreg = /^[1][3456789][0-9]{9}$/;
    if (formObj.userName == "") {
      wx.showToast({
        title: "请输入称呼",
        icon: "error",
        duration: 2000,
      });
    } else if (formObj.tel == "") {
      wx.showToast({
        title: "请输入电话",
        icon: "error",
        duration: 2000,
      });
    } else if (!myreg.test(formObj.tel)) {
      wx.showToast({
        title: "电话格式不正确",
        icon: "error",
        duration: 2000,
      });
    } else {
      this.submit({ ...formObj, recordType: 3 });
    }
  },
  submit (data) {
    wx.showLoading({
      title: "提交中...",
    });
    wx.request({
      url: "https://a.matripe.com.cn/business/coop/add",
      method: "post",
      data,
      header: app.globalData.headers,
      success: function (res) {
        console.log(res);
        if (res.data.status == 200) {
          wx.showToast({
            title: "提交成功",
            icon: "success",
            duration: 2000,
            success () {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                });
              }, 1000);
            },
          });
        } else if (res.data.status == 2002) {
          app.logout().then(() => {
            wx.reLaunch({
              url: "/pages/login/index",
            });
          });
        } else {
          wx.showToast({
            title: "提交失败,请重试",
            icon: "fail",
            duration: 2000,
          });
        }
        wx.hideLoading();
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    var that = this;
    wx.hideLoading({
      success: (res) => {
        that.setData({
          pageDone: true,
        });
      },
    });

    // const query = wx.createSelectorQuery()
    // query.select('#myCanvas')
    //   .fields({
    //     node: true,
    //     size: true
    //   })
    //   .exec((res) => {
    //     const canvas = res[0].node
    //     const ctx = canvas.getContext('2d')
    //     const dpr = wx.getSystemInfoSync().pixelRatio
    //     canvas.width = res[0].width * dpr // 获取宽
    //     canvas.height = res[0].height * dpr // 获取高
    //     ctx.scale(dpr, dpr)

    //     // 到这里就可以直接绘制
    //     let image = canvas.createImage(); //创建iamge实例

    //     wx.chooseMedia({
    //       success: function (res) {
    //         console.log(res);
    //         image.src = res.tempFiles[0].tempFilePath; // 引入本地图片
    //         image.onload = function () {
    //           ctx.drawImage(image, 0, 0, 100, 100); // 背景图
    //         }
    //       },
    //     });
    //   })
  },

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () { },
});