// pages/information/information.ts
import { informationList, informationDelete, redDotMarkDeal } from '../../apis/information'
// import { codeScanningSuccess } from '../../apis/socket'
import { querystrToObj } from '../../utils/util'
const app = getApp<IAppOption>()

Page({

  /**
   * 页面的初始数据
   */

  data: {
    headeBg: '',
    toggles: <any>{
      '基础信息': false,
      '学历教育': false,
      '非学历教育': false,
      '职业经历': false,
      '社会活动记录': false
    },
    userInfo: {},
    list: <any>{},
    fixed: 'black',
    redDotMakStr: '',
    superiorIdAttachInfoResponseVoMap: [],
    isShowInvalid: true,
    dialogStage: false,
    stages: [{ name: '高等教育', value: 4 },
    { name: '高中', value: 5 },
    { name: '职高', value: 6 },
    { name: '初中', value: 7 },
    { name: '中专', value: 8 },
    { name: '小学', value: 9 }]
  },
  scanCode() {
    wx.scanCode({
      success(res) {
        const { result } = res
        console.log(result)
        // if (/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(result)) {
        //   codeScanningSuccess(res.result).then(su => {
        //     console.log(su)
        //     wx.navigateTo({
        //       url: `/pages/scanLogin/scanLogin?id=${res.result}`
        //     })
        //   })
        //   // https://www.chrivc.com/api/consumer/wechatshare/getUrlLink?b=2&query=source%3D2%26type%3D2%26active%3D3%26id%3D1563042929900044289
        // } else 
        if (/^https:\/\/www.chrivc.com\/api\/consumer\/wechatshare\/getUrlLink\?/.test(result)) {
          const queryStr = result.split('?')[1]
          const { query, b } = querystrToObj(queryStr)
          // const queryObj = querystrToObj(decodeURIComponent(query))
          if (b == 1) {
            wx.reLaunch({
              url: `/pages/apply/apply?${decodeURIComponent(query)}`
            })
          } else if (b == 2) {
            wx.navigateTo({
              url: `/pages/reportPreview/reportPreview?${decodeURIComponent(query)}`
            })
          } else if (b == 3) {
            wx.navigateTo({
              url: `/pages/scanLogin/scanLogin?${decodeURIComponent(query)}`
            })
            // if (/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(queryObj.id)) {
            //   codeScanningSuccess(queryObj.id).then(su => {
            //     console.log(su)
            //     wx.navigateTo({
            //       url: `/pages/scanLogin/scanLogin?id=${queryObj.id}`
            //     })
            //   })
            // }
          } else {
            wx.showToast({
              icon: 'error',
              title: '未知页面'
            })
          }
        } else if (result === 'https://chrivc.com/mobile/#/appdownload') {
          wx.showToast({
            icon: 'none',
            title: '下载APP请使用微信或自带浏览器扫码操作!',
            duration: 2000
          })
        } else {
          wx.navigateTo({
            url: `/pages/scanResult/scanResult?result=${result}`
          })
        }
      }
    })
  },
  handleToggle(e: WechatMiniprogram.CustomEvent) {
    const { toggled, key, show } = e.currentTarget.dataset
    if (show) {
      const { toggles } = this.data
      toggles[key] = !toggled
      this.setData({
        toggles: toggles
      })
    }
  },
  handleCertification(e: WechatMiniprogram.BaseEvent) {
    const { idcard } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/certification/certification?idCard=${idcard}`
    })
  },
  handleDetail(e: WechatMiniprogram.BaseEvent) {
    const { key, index } = e.currentTarget.dataset
    if (key === '社会活动经历') {
      wx.setStorageSync('informationDetail', this.data.list[e.currentTarget.dataset.key][0]['事件/活动记录'][index])
      wx.setStorageSync('informationName', key)
      this.handleRedDot(this.data.list[e.currentTarget.dataset.key][0]['事件/活动记录'][index]['上级Id'])
    } else {
      wx.setStorageSync('informationDetail', this.data.list[e.currentTarget.dataset.key][index])
      wx.setStorageSync('informationName', key)
      this.handleRedDot(this.data.list[e.currentTarget.dataset.key][index]['上级Id'])
    }
    wx.navigateTo({
      url: '/pages/informationDetail/informationDetail?index=' + index
    })
  },
  handleRedDot(id: any) {
    redDotMarkDeal(id).then(res => {
      console.log(res);
    })
  },
  goPersonal() {
    wx.navigateTo({
      url: '/pages/personal/personal'
    })
  },
  handleRecords(e: WechatMiniprogram.BaseEvent) {
    wx.navigateTo({
      url: '/pages/dataRecords/dataRecords?id=' + e.currentTarget.dataset.id
    })
  },
  handleCreate(e: WechatMiniprogram.BaseEvent) {
    // console.log(e);

    const { key } = e.currentTarget.dataset;
    const { list, dialogStage } = this.data
    if (key === '社会活动经历') {
      wx.setStorageSync('informationName', key)
      wx.setStorageSync('eventName', key)
      wx.setStorageSync('eventId', list['社会活动经历'] && list['社会活动经历'][0]['上级Id'] || '社会活动经历')
      // wx.setStorageSync('eventId', (list['社会活动经历'] && list['社会活动经历'][0] && list['社会活动经历'][0]['上级Id']) || '社会活动经历')
      wx.navigateTo({
        url: '/pages/informationEventCreate/informationEventCreate'
      })
    } else {
      wx.setStorageSync('informationName', key)
      if (key === '学历教育') {
        this.setData({
          dialogStage: !dialogStage
        })
      } else {
        wx.navigateTo({
          url: '/pages/informationCreate/informationCreate'
        })
      }
    }
  },
  handleAddEdu(e: WechatMiniprogram.BaseEvent) {
    
    const { stage } = e.currentTarget.dataset
    wx.setStorageSync('educationStage', stage)
    console.log(stage);
    wx.navigateTo({
      url: '/pages/informationCreate/informationCreate'
    })
  },
  getInformation() {
    if (app.certificationVerify()) {
      informationList().then((res: any) => {
        // console.log(res);
        if (res.c === 200) {
          this.setData({
            list: res.d.jsonObject,
            redDotMakStr: res.d.redDotMakStr,
            superiorIdAttachInfoResponseVoMap: res.d.superiorIdAttachInfoResponseVoMap
          })
        }
      })
    }
  },
  handleAdd(e: WechatMiniprogram.BaseEvent) {


    const { name, key, keyid, value } = e.currentTarget.dataset
    console.log('handleAdd', name, key, keyid, value);
    if (name !== '手机号码') {
      console.log('name');

      wx.setStorageSync('informationEditDataName', name)
      wx.setStorageSync('informationName', key)
      wx.removeStorageSync('informationEditInfo')
      wx.navigateTo({
        // url: '/pages/informationEdit/informationEdit?type=' + 'add'
        url: `/pages/informationEdit/informationEdit?type=add&keyId=${keyid}`
      })
    } else {
      wx.navigateTo({
        url: `/pages/addPhone/addPhone?phoneNumber=${value || ''}`
      })
    }

  },
  handleEdit(e: WechatMiniprogram.BaseEvent) {
    console.log(e);

    const { value, name } = e.currentTarget.dataset
    wx.setStorageSync('informationEditInfo', value)
    wx.setStorageSync('informationEditDataName', name)
    wx.navigateTo({
      url: '/pages/informationEdit/informationEdit?type=edit'
    })
  },
  handleDelete(e: WechatMiniprogram.BaseEvent) {
    informationDelete({
      dataId: e.currentTarget.dataset.id,
      deleteAll: false
    }).then((res: any) => {
      if (res.c === 200) {
        this.getInformation()
      }
    })
  },
  handleGenerateReport() {
    wx.navigateTo({
      url: '/pages/generateReport/generateReport'
    })
  },
  toggleAll() {
    this.setData({
      toggles: {
        基础信息: false,
        学历教育: false,
        非学历教育: false,
        职业经历: false,
        社会活动经历: false
      },
    })
  },
  goTop() {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  isShowGuide() {
    // console.log(typeof JSON.parse(wx.getStorageSync('showGuide')));
    if (!wx.getStorageSync('showGuide')) {
      wx.navigateTo({
        url: '/pages/guide/guide'
      })
    }
  },
  goReport() {
    wx.switchTab({
      url: '/pages/report/report'
    })
  },
  handleInvalid() {
    this.setData({
      isShowInvalid: !this.data.isShowInvalid
    })
  },
  stageCencel() {
    this.setData({
      dialogStage: !this.data.dialogStage
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.isShowGuide()
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo,
      isCer: app.certificationVerify(),
      isMember: app.memberVerify()
    })
    this.getInformation()
  },

  onPageScroll(e) {
    const { scrollTop } = e
    if (scrollTop > 150) {
      this.setData({
        headeBg: '#3A61B1'
      })
    } else {
      this.setData({
        headeBg: ''
      })
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})