// pages/informationEdit/informationEdit.ts
import { nationList } from "../../utils/index";
import { informationAdd, updateWithDataId, businessLinkNameList } from "../../apis/information";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nationList: [],
    source: '',
    value: '',
    // type: 'edit',
    dataName: '',
    key: '',
    keyId: '',
    isAdd: false,
    radioType: 0,
    startDate: '',
    endDate: '',
    marryList: [{ name: '已婚' }, { name: '未婚' }, { name: '离异' }],
    sfqrzOptions: [{ name: '全日制' }, { name: '非全日制' }],
    zyccOptions: [{ name: '专科' }, { name: '本科' }, { name: '硕士研究生' }, { name: '博士研究生' }],
    jyjgOptions: [{ name: '在读' }, { name: '结业' }, { name: '毕业' }, { name: '肄业' }],
    zzmmOptions: [{ name: '中共党员' }, { name: '中共预备党员' }, { name: '共青团员' }, { name: '民革党员' }, { name: '民盟盟员' }, { name: '民建会员' }, { name: '民进会员' }, { name: '农工党党员' }, { name: '致公党党员' }, { name: '九三学社社员' }, { name: '台盟盟员' }, { name: '无党派人士' }, { name: '群众' }],
    xzOptions: [{ name: '二年制' }, { name: '三年制' }, { name: '四年制' }, { name: '五年制' }],
    endDateTyeps: [{ name: '当天', value: 0, checked: false }, { name: '至今', value: 1, checked: false }, { name: '时间选择', value: 2, checked: false }],
    endDateType: '',
    resultList: undefined,
    businessNameAuthenticationTag: undefined
  },
  handleType(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      radioType: e.currentTarget.dataset.value,
      endDate: ''
    })
  },
  bindNationChange(e: WechatMiniprogram.CustomEvent) {
    const { nationList } = this.data
    this.setData({
      value: nationList[e.detail.value].name,
      dataName: wx.getStorageSync('informationEditDataName')
    })
  },
  bindMarryChange(e: WechatMiniprogram.CustomEvent) {
    console.log(e);

    this.setData({
      value: this.data.marryList[e.detail.value].name
    })
  },
  bindSfqrzChange(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      value: this.data.sfqrzOptions[e.detail.value].name
    })
  },
  bindZyccChange(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      value: this.data.zyccOptions[e.detail.value].name
    })
  },
  bindJyjgChange(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      value: this.data.jyjgOptions[e.detail.value].name
    })
  },
  bindXzChange(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      value: this.data.xzOptions[e.detail.value].name
    })
  },
  bindZzmmChange(e: WechatMiniprogram.CustomEvent) {
    this.setData({
      value: this.data.zzmmOptions[e.detail.value].name
    })
  },
  inputChange(e: WechatMiniprogram.CustomEvent) {
    const { key } = e.currentTarget.dataset
    let type = key === '职业经历' ? 'EMPLOYER' : (key === '学历教育' ? 'ACADEMIC_EDUCATION' : 'NON_ACADEMIC_EDUCATION')

    businessLinkNameList({ type, name: e.detail.value || undefined }).then((res: any) => {
      this.setData({
        resultList: res.d,
        businessNameAuthenticationTag: false,
        businessNameAuthenticationName: e.detail.value
      })
    })
  },

  handleEdit() {
    const { dataId } = this.data.source
    const { dataName, startDate, endDate, radioType, businessNameAuthenticationTag } = this.data
    const { value } = this.data

    let payload = {
      updateDataList: [
        {
          dataId,
          dataName,
          dataValue: dataName === '时间' ? startDate + (endDate !== '' ? ',' + endDate : '') : value,
          modifiable: true
        }
      ]
    }
    if (businessNameAuthenticationTag !== undefined) {
      payload.businessNameAuthenticationTag = businessNameAuthenticationTag;
      payload.businessNameAuthenticationName = value
    }

    if (!value) {
      wx.showToast({
        icon: 'none',
        title: `请输入${dataName}`
      })
      return
    }

    if (dataName === '手机号码') {
      if (value.length !== 11) {
        wx.showToast({
          icon: 'none',
          title: `${dataName}格式错误`
        })
        return
      }
    }

    if (dataName === '时间') {
      console.log(dataName);

      if (new Date(startDate).getTime() > new Date(endDate).getTime()) {
        wx.showToast({
          icon: 'none',
          title: `时间范围不正确`
        })
        return
      }
    }

    if (dataName === '电子邮箱') {
      if (!this.checkEmail(value)) return
    }
    if (radioType === 0) {
      if (!startDate) {
        wx.showToast({
          icon: 'none',
          title: `请选择开始时间`
        })
        return
      }
    }

    console.log(payload);

    updateWithDataId(payload).then((res: any) => {
      if (res.c === 200) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  handleSelect(e) {
    console.log(e);

    // var pages = getCurrentPages();
    // var prevPage = pages[pages.length - 2]
    // prevPage.setData({                          //修改上一个页面的变量
    //   jyjg: e.currentTarget.dataset.name,
    //   businessNameAuthenticationTag: e.currentTarget.dataset.authentication
    // })
    // wx.navigateBack({
    //   delta: 1
    // })
    const { dataId } = this.data.source
    const { dataName, startDate, endDate, radioType, businessNameAuthenticationTag } = this.data
    const { value } = this.data

    let payload = {
      businessNameAuthenticationTag: e.currentTarget.dataset.authentication,
      businessNameAuthenticationName: e.currentTarget.dataset.name,
      updateDataList: [
        {
          dataId,
          dataName,
          dataValue: e.currentTarget.dataset.name,
          modifiable: true
        }
      ]
    }
    console.log(payload);

    updateWithDataId(payload).then((res: any) => {
      if (res.c === 200) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  checkEmail: function (email) { //自定义函数，检查输入的邮箱地址是否满足要求
    let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/ //正则表达式
    if (str.test(email)) { //检查正则表达式是否符合邮箱地址要求
      return true
    } else {
      wx.showToast({ //显示消息提示框
        title: '邮箱格式错误',
        icon: 'none'
      })
      return false
    }
  },
  handleCreate() {
    const {
      dataName, keyId, value, key, startDate
      , endDate
    } = this.data
    if (!value) {
      wx.showToast({
        icon: 'none',
        title: `请选择或输入${dataName}`
      })
      return
    }
    if (dataName === '电子邮箱') {
      if (!this.checkEmail(value)) return
    }
    if (dataName === '时间') {
      console.log(dataName);

      if (new Date(startDate).getTime() > new Date(endDate).getTime()) {
        wx.showToast({
          icon: 'none',
          title: `时间范围不正确`
        })
        return
      }
    }

    let filters = ["手机号码", "座机号码", "通信地址", "电子邮箱"];

    let payload = {}
    if (filters.indexOf(dataName) > -1) {
      payload = {
        jsonObject: {
          [keyId]: {
            [dataName]: [{ 值: value }]
          }
        }
      }
    } else if (key === '基础信息') {
      payload = {
        jsonObject: {
          [keyId]: {
            [dataName]: value
          }
        }
      }
    } else {
      payload = {
        jsonObject: {
          [keyId]: [{
            [dataName]: value
          }]
        }
      }
    }

    // console.log(payload);

    informationAdd(payload).then((res: any) => {
      if (res.c === 200) {
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type === 'edit') {
      let { endDateTyeps, endDateType } = this.data
      if (wx.getStorageSync('informationEditInfo').dataValue.split(',')[1] === '当天') {
        endDateTyeps[0].checked = true
        endDateType = 0
      } else if (wx.getStorageSync('informationEditInfo').dataValue.split(',')[1] === '至今') {
        endDateTyeps[1].checked = true
        endDateType = 1
      } else {
        endDateType = 2
        endDateTyeps[2].checked = true
      }
      this.setData({
        endDateTyeps,
        endDateType
      })
      this.setData({
        // @ts-ignore

        nationList: nationList(),
        source: wx.getStorageSync('informationEditInfo'),
        value: wx.getStorageSync('informationEditInfo').dataValue,
        dataName: wx.getStorageSync('informationEditDataName'),
        isAdd: false,
        keyId: options.keyId,
        radioType: wx.getStorageSync('informationEditInfo').dataValue.split(',').length > 1 ? 0 : 1,
        startDate: wx.getStorageSync('informationEditInfo').dataValue.split(',')[0],
        endDate: wx.getStorageSync('informationEditInfo').dataValue.split(',')[1],
        key: wx.getStorageSync('informationName'),
        // endDateTypes:endDateTyeps,
        // endDateType: 0
        // type: options.type
      })

    } else {
      this.setData({
        // @ts-ignore
        nationList: nationList(),
        dataName: wx.getStorageSync('informationEditDataName'),
        isAdd: true,
        keyId: options.keyId,
        key: wx.getStorageSync('informationName')
      })
    }

  },
  radioChange(e) {
    console.log(e);
    const value = Number(e.detail.value)
    switch (value) {
      case 0:
        this.setData({
          endDate: '当天',
          endDateType: value
        })
        break;
      case 1:
        this.setData({
          endDate: '至今',
          endDateType: value
        })
        break;
      case 2:
        this.setData({
          endDate: '',
          endDateType: value
        })
        break;
    }

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})