// pages/apply/apply.ts
const app = getApp<IAppOption>()
import { querystrToObj } from '../../utils/util'
import { inspectedList, deal } from '../../apis/inspection'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listQuery: {
      current: 1
    },
    list: [],
    status: {
      WAIT: '待处理',
      AGREE: '已同意',
      REFUSE: '已拒绝',
      AGREE_TIME_OUT: "已失效"
    },
    shadowShow: false,
    selectItem: {},  // 单个处理选中
    selectObj: {},  // 批量选中
    batch: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  getList() {
    inspectedList(this.data.listQuery).then((res:any) => {
      console.log(res)
      const list = res.d.records
      if (list.length) {
        this.setData({
          list: this.data.list.concat(list)
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: '没有更多了!'
        })
      }
    })
  },

  handleClick(e:WechatMiniprogram.BaseEvent){
    const { batch } = this.data
    const { item } = e.currentTarget.dataset
    if (batch) {
      wx.showToast({
        icon: 'none',
        title: '请先退出批量处理!'
      })
      return
    }
    this.setData({
      selectItem: item
    })
    this.showShadow()
  },
  handleAgree() {
    const { selectItem, batch } = this.data
    // @ts-ignore
    let ids = [selectItem.id]
    if (batch) ids = this.getBatchSelectIds()
    
    wx.navigateTo({
      // @ts-ignore
      url: `/pages/applySetting/applySetting?ids=${ids.join(',')}&name=${selectItem.name || ''}&batch=${batch ? 1 : ''}`
    })
    this.hideShadow()
  },
  handleRefuse() {
    const { selectItem, batch } = this.data
    // @ts-ignore
    let ids = [selectItem.id]
    if (batch) ids = this.getBatchSelectIds()
    deal({
      agree: false,
      ids
    }).then(() => {
      wx.showToast({
        icon: 'none',
        title: '操作成功!'
      })
      this.hideShadow()
      app.reload()
    })
  },
  handleBatch() {
    const ids = this.getBatchSelectIds()
    if (!ids.length) {
      wx.showToast({
        icon: 'none',
        title: '请选择申请!'
      })
      return
    }
    console.log(ids)
    this.showShadow()
  },
  changeBatch() {
    const { batch } = this.data
    this.setData({
      selectObj: {},
      batch: !batch
    })
  },
  getBatchSelectIds() {
    const { selectObj } = this.data
    // @ts-ignore
    const ids = Object.keys(selectObj).filter((key) => selectObj[key])
    return ids
  },
  radioChange(e: WechatMiniprogram.BaseEvent) {
    const { id } = e.currentTarget.dataset
    // @ts-ignore
    const val = this.data.selectObj[id]
    this.setData({
      [`selectObj.${id}`]: !val
    })
  },
  jumpSetting(e: WechatMiniprogram.BaseEvent) {
    const { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/applySetting/applySetting?id=${item.id}&name=${item.name}`
    })
  },
  jumpReport(e: WechatMiniprogram.BaseEvent) {
    wx.navigateTo({
      url: `/pages/reportPreview/reportPreview?id=${e.currentTarget.dataset.id}&type=1&active=2`
    })
  },
  jumpInspected(e: WechatMiniprogram.BaseEvent) {
    const { item } = e.currentTarget.dataset
    wx.reLaunch({
      url: `/pages/inspected/inspected?name=${item.name}&phone=${item.phone}`
    })
  },
  jumpAnswer() {
    if (app.memberVerify(true)) {
      wx.navigateTo({
        url: '/pages/answer/answer'
      })
    }
  },
  showShadow() {
    this.setData({
      shadowShow: true
    })
  },
  hideShadow() {
    this.setData({
      shadowShow: false
    })
  },
  

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      list: [],
      ['listQuery.current']: 1,
      batch: false,
      selectObj: {},
      selectItem: {}
    })
    this.getList()
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      ['listQuery.current']: this.data.listQuery.current + 1
    })
    this.getList()
  },

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

  }
})

export {}
