const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
const util = require("../../../utils/util.js");
const Http = require("../../../utils/HttpBasics");
const config = require("../../../config/config");
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navigationBarHeight,
    date: '1988-03-12',
    selectques: { "sex": 0, "name": 0, "birth": 0, "phone": 0, "address": 0 },
    flag: 1,
    answerflag: "",
    flagsex: 0,
    useImg: 0,
    disabled: false,
    showReceiptUrl: false,
    receiptUrl: "",
    items: [{
      name: 1,
      value: '男',
      checked: false
    },
    {
      name: 2,
      value: '女',
      checked: false
    },
    ],
    username: "",
    array: ['上班族', '学生', '企业高管', '个体户', '自由职业', '其他'],
    array1: ['附近住户', '距离2km', '距离3km', '更远'],
    index: 0,
    index1: 0,
    showPage: false
  },
  /**
   * 获得生日
   */
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      flag: 2
    })
  },
  /**
   * 职业
   */
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生活半径
   */
  bindPickerChange1: function (e) {
    this.setData({
      index1: e.detail.value
    })
  },
  address: function () {
    return
    let that = this;
    // wx.chooseLo
    // cation({
    //   success: function (res) {
    //     that.setData({
    //       address: res.name + '(' + res.address + ')',
    //       addressStr: JSON.stringify(res)
    //     })
    //   },
    //   fail: function (error) {
    //     console.log(error)
    //   },
    //   complete: function (data) { }
    // })
  },
  change(value) {
    console.log(value)
  },
  formSubmit: function (e) {

    app.globalData.previewFlag = true
    let that = this;
    /**
     * sex
     * 0 保密
     * 1 男
     * 2 女
     */
    if (that.data.flagsex == 0) {
      var sex = 0;
    } else {
      var sex = that.data.sex;
    }
    if (that.data.addressStr) {
      var address = that.data.addressStr;
    } else {
      var address = null;
    }
    if (e.detail.value.username) {
      var username = e.detail.value.username;
    } else if (that.data.username) {
      var username = that.data.username;
    } else {
      var username = null;
    }
    if (that.data.flag == 2 && that.data.date) {
      var birthday = new Date(that.data.date).getTime();
    } else {
      var birthday = null;
    }

    //获得答案
    delete e.detail.value.username;
    let obj = e.detail.value;
    if (that.data && that.data.question && that.data.question.length > 0) {
      let valArr22 = [];

      Object.keys(obj).map(function (item, index) {
        console.log(item, index)
        if (!obj[item]) {
          wx.showToast({
            title: '问卷调查回答不完整,请补充',
            duration: 2000,
            icon: "none"
          })
          that.setData({
            answerflag: "noanswer"
          })
        } else {
          that.setData({
            answerflag: "answer"
          })

          // return { ques: that.data.question[index].ques, answer: obj[item] } 
        }
      });

      that.data.question.map((item, index) => {

        if (obj['answer' + index] == '') {
          wx.showToast({
            title: '问卷调查回答不完整,请补充',
            duration: 2000,
            icon: "none"
          })
          that.setData({
            answerflag: "noanswer"
          })
        } else {
          that.setData({
            answerflag: "answer"
          })
          valArr22.push({ ques: item.ques, answer: obj['answer' + index] })
        }
      })

      this.setData({
        answer: valArr22
      });
    }

    if ((username == null && this.data.selectques.name == 1) || (sex == 0 && this.data.selectques.sex == 1) || (birthday == null && this.data.selectques.birthday == 1)) {
      wx.showToast({
        title: '请输入完整的用户信息',
        icon: "none"
      })
    }
    else {

      if (this.data.answerflag == 'noanswer') {
      } else {

        that.setData({
          disabled: true
        })
        let data = {};
        data = {
          sex: sex,
          address: address,
          name: username,
          birthday: birthday,
          answer: JSON.stringify(that.data.answer),
          activityId: that.data.activityId
        };

        if (obj.number) {
          data.number = obj.number
        }
        if (obj.age) {
          data.age = obj.age
        }
        if (that.data.receiptUrl) {
          data.imgUrl = "[" + '"' + that.data.receiptUrl + '"' + "]";
        }

        Http.post({
          url: config.api.activityJoin,
          data: data
        })
          .then(res => {
            that.setData({
              disabled: false
            })
            wx.reLaunch({
              url: `/pages/radetail/success/index?activityId=${that.data.activityId}`,
            })
          })
          .catch(err => {
            that.setData({
              disabled: false
            })
            wx.showToast({
              title: err.message,
              icon: 'none',
              duration: 2000,
              mask: false
            })
          })
      }
    }
  },
  radioChange: function (e) {
    this.setData({
      sex: e.detail.value,
      flagsex: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getLocation();
    if (options && options.activityId) {
      that.setData({
        activityId: options.activityId,
        phone: options.phone
      })
      that.getDetail(options.activityId);
    }
  },
  uploadImg() {
    let that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths, 'tempFilePaths');
        wx.uploadFile({
          url: config.url + config.api.fileUpload,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'token': app.globalData.token
          },
          success(res) {
            console.log(res)
            if (res.statusCode == 200) {
              that.setData({
                receiptUrl: JSON.parse(res.data).data.url,
                showReceiptUrl: true
              })
            } else if (res.statusCode == 413) {
              wx.showToast({
                title: '上传的图片太大，请重新上传',
                icon: "none"
              })
              that.setData({
                showReceiptUrl: false
              })
            } else {
              wx.showToast({
                title: '请上传正确的图片',
                icon: "none"
              })
              that.setData({
                showReceiptUrl: false
              })
            }
          },
          fail(error) {
            wx.showToast({
              title: "上传图片失败",
              icon: "none"
            })
            that.setData({
              showReceiptUrl: false
            })
          }
        })
      }
    })
  },
  getDetail(activityId) {
    let that = this;
    Http.get({
      url: config.api.acfindById,
      data: {
        id: activityId
      }
    }).then(res => {
      if (res && res.data && res.data.activity) {
        this.setData({
          useImg: res.data.activity.useImg,
          imgDetail: res.data.activity.imgDetail,
          selectques: JSON.parse(res.data.activity.selectques)
        })
        if (res.data && res.data.activity && res.data.activity.question) {
          that.setData({
            question: JSON.parse(res.data.activity.question)
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000,
        mask: false
      })
    })
  },
  /**
   * 获得经纬度
   */
  getLocation() {
    let that = this;
    // wx.getLocation({
    //   type: "wgs84",
    //   success: function (res) {
    //     console.log(res)
    //     if (res && res.longitude && res.latitude) {
    //       Http.post({
    //         url: config.api.updateLBS,
    //         data: {
    //           latitude: res.latitude,
    //           longitude: res.longitude
    //         }
    //       }).then(res => {
    //         console.log(res)
    //       })
    //     }
    //   },
    //   fail: error => {
    //     wx.showModal({
    //       title: '提醒',
    //       content: '您拒绝了地理位置的授权,将无法参加报名活动,请删除小程序重新登录,才可以继续参加该活动',
    //       showCancel:false,
    //       success:function(res){
    //         if (res.confirm){
    //         }
    //       }
    //     })
    //   }
    // })
  },
})