const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// pages/userCenter.js
const app = getApp();

const wxService = require('../../utils/wxService');

const utils = require('../../utils/util');

const envInfo = require('../../config/index').envInfo;

var wxbarcode = require('../../codeUtils/index.js');

var count = 0;
wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    date: '',
    focus: false,
    memberCardNo: '',
    memberInfo: {},
    memberId: '',
    array: ['男', '女', '保密'],
    objectArray: [{
      id: 0,
      name: '男'
    }, {
      id: 1,
      name: '女'
    }, {
      id: 2,
      name: '保密'
    }],
    index: 0,
    date: ''
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx2my.hideShareMenu();
    console.log('onload');

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('onshow'); // 取会员卡号
    const accreditUserInfo = wx2my.getStorageSync('_accreditUserInfo').data
    const { avatar, nickName } = accreditUserInfo
    const {
      code,
      id
    } = wx2my.getStorageSync('_baseUserInfo').data && wx2my.getStorageSync('_baseUserInfo').data.member;

    if (code && id) {
      this.setData({
        memberCardNo: code,
        memberId: id,
        avatar
      }, () => {
        // 获取会员信息
        this.getMemberFind();
      });
    }
  },

  bindDateChange(e) {
    this.setData({
      date: e.detail.value
    });
  },

  formSubmit(e) {
    // 0 男 1 女
    const {
      sex,
      email
    } = e.detail.value;

    if (sex == 0) {
      e.detail.value.sex = '男';
    } else if (sex == 1) {
      e.detail.value.sex = '女';
    } else {
      e.detail.value.sex = '保密';
    } // 校验邮箱


    if (email) {
      let emailFlag = utils.validateEmail(email);

      if (!emailFlag) {
        wx2my.showToast({
          title: `请输入正确的邮箱地址!`,
          icon: 'none'
        });
        return false;
      }
    }

    const params = e.detail.value; // 保存接口

    this.handelMemberUplate(params);
  },

  // 会员详情
  getMemberFind() {
    wx2my.showLoading({
      title: '加载中'
    });
    const params = {
      cardNoOnline: this.data.memberCardNo
    };
    wxService.post(`/member/find`, params).then(res => {
      if (!res) return false;
      const {
        result,
        data
      } = res && res.data || res && res.result;

      if (result == 0) {
        wx2my.hideLoading();
        data.member.birthday = data.member.birthday && data.member.birthday.substring(0, 10) || ''; // 性别

        const {
          sex
        } = data.member;

        if (sex == '男') {
          data.member.sex = 0;
        } else if (sex == '女') {
          data.member.sex = 1;
        } else {
          data.member.sex = 2;
        }

        this.setData({
          memberInfo: data.member,
          index: data.member.sex,
          date: data.member.birthday
        });
        wxbarcode.barcode('barcode', this.data.memberCardNo, 500, 120);
        setTimeout(() => {
          wxbarcode.barcode('barcode', this.data.memberCardNo, 500, 120);
        }, 200); // this.setInternal();
      }
    });
  },

  //定时刷新 10s
  setInternal() {
    var timeOut = setInterval(() => {
      if (count < 10) {
        count++;
        wxbarcode.barcode('barcode', this.data.memberCardNo, 500, 120);
      } else {
        clearInterval(timeOut);
      }
    }, 500);
  },

  canvasIdErrorCallback(e) {
    console.log('code-me', e.detail.errMsg);
  },

  // 更新
  handelMemberUplate(params) {
    wx2my.showLoading({
      title: '加载中'
    });
    params.id = this.data.memberId;
    wxService.post(`/member/update`, params).then(res => {
      const {
        result,
        data
      } = res.data;

      if (result == 0) {
        wx2my.hideLoading();
        wx2my.showToast({
          title: '保存成功',
          icon: 'success'
        });
      } else {
        wx2my.showToast({
          title: '保存失败',
          icon: 'none'
        });
      }
    });
  },

  bindTextAreaBlur: function(e) {
    console.log(e.detail.value);
  },

  //日期选择
  bindDateChange(e) {
    this.setData({
      date: e.detail.value
    });
  },

  handleGoWxCard() {// wechatCardId wechatCode
    // const { wechatCardId, wechatCode } = wx.getStorageSync('_baseUserInfo')
    // wx.openCard({
    //   cardList: [{
    //     cardId: wechatCardId,
    //     code: wechatCode
    //   }],
    //   success(res) {
    //     console.log('查看微信卡包中的卡券', res)
    //   }
    // })
  }

});