var app = getApp();
const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
const Http = require("../../utils/HttpBasics");
const config = require("../../config/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    userName: "",
    userPhone: "",
    // region:"",
    detail: "",
    switch1Checked: false,
    region: ['北京市', '北京市', '东城区'],
    id: "",
    url: "",
    typeLsit: {}
  },

  /**
   * 获取全局样式
   */
  getType() {
    Http.get({
      url: config.api.setType,
      data: {
        mouldType: app.globalData.mouldType
      }
    }).then(res => {
      const {
        code,
        data
      } = res
      if (code == 200) {
        app.globalData.typeLsit = data
        this.setData({
          typeLsit: data,
        })
      }
    })
  },

  go(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  setUserName(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  setUserPhone(e) {
    this.setData({
      userPhone: e.detail.value
    })
  },
  setRegion(e) {
    this.setData({
      region: e.detail.value
    })
  },
  setDetail(e) {
    this.setData({
      detail: e.detail.value
    })
  },
  switch1Change(e) {
    this.setData({
      switch1Checked: e.detail.value
    })
  },
  dleSite() {
    Http.get({
      url: config.api.siteDel,
      data: {
        id: this.data.id
      }
    }).then(res => {
      wx.showToast({
        title: '删除成功',
        icon: 'none',
        duration: 2000,
      })
      wx.navigateBack({
        delta: 1
      })
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
      })
    })
  },
  addSite() {
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (this.data.userName == "") {
      wx.showToast({
        title: '请输入收货人',
        icon: 'none',
        duration: 2000,
      })
      return
    }

    if (!myreg.test(this.data.userPhone)) {
      // this.redata.Phone = '';
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
        duration: 2000,
      })
      return
    }

    if (this.data.detail == "") {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 2000,
      })
      return
    }

    let data = {
      consignee: this.data.userName,
      mobile: this.data.userPhone,
      address: this.data.detail,
      region: JSON.stringify(this.data.region),
      isDefault: this.data.switch1Checked ? 1 : 0,
    }
    if (this.data.id && this.data.id != "") {
      data.id = this.data.id
    }
    Http.post({
      url: config.api.collectSite,
      data: data,
    }).then(res => {
      wx.showToast({
        title: '保存成功！',
        icon: 'none',
        duration: 2000,
      })

      wx.navigateBack({
        delta: 1
      })
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
      })
    })
  },

  bindRegionChange(e) {
    this.setData({
      region: e.detail.value,
      obj: {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getType()
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.siteCheck(options.id)
    }
  },
  siteCheck(id) {
    Http.get({
      url: config.api.siteCheck,
      data: {
        id
      }
    }).then(res => {
      let obj = res.data

      this.setData({
        userName: obj.consignee,
        userPhone: obj.mobile,
        detail: obj.address,
        region: JSON.parse(obj.region),
        switch1Checked: obj.isDefault == 0 ? false : true,
      })
    }).catch(err => {
      wx.showToast({
        title: err.message ? err.message : err.data,
        icon: 'none',
        duration: 2000,
      })
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})