// pages/applySetting/applySetting.ts
import { list } from '../../apis/report'
import { deal } from '../../apis/inspection'
import { parseTime } from "../../utils/index"

const app = getApp<IAppOption>()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ids: [],
    name: '',
    batch: '',
    reportList: [],
    selectIndex: undefined,
    endDate: undefined,
    startDate: parseTime(new Date().getTime(), '{y}-{m}-{d}'),
    unlimited: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {
    const { ids, name, batch } = options
    this.setData({
      ids: ids.split(','),
      name,
      batch
    })
  },

  getReportList() {
    list({
      size: 999999
    }).then((res:any) => {
      this.setData({
        reportList: res.d.records
      })
    })
  },
  submit() {
    const {reportList, selectIndex, endDate, unlimited, ids} = this.data
    if (!selectIndex) {
      wx.showToast({
        icon: 'none',
        title: '请选择报告'
      })
      return
    }
    if (!unlimited && !endDate) {
      wx.showToast({
        icon: 'none',
        title: '请选择授权截止时间'
      })
      return
    }
    deal({
      ids: ids,
      // @ts-ignore
      endDate: unlimited ? undefined : (new Date(endDate + ' 23:59:59').getTime() / 1000),
      // @ts-ignore
      reportId: reportList[selectIndex].id,
      agree: true,
    }).then(() => {
      if (app.memberVerify()) {
        this.showShadow('shadow1')
      } else {
        this.showShadow('shadow2')
      }
    })
  },
  jumpBuy() {
    wx.redirectTo({
      url: "/pages/member/member"
    })
  },
  jumpSet() {
    wx.redirectTo({
      url: "/pages/answer/answer"
    })
  },
  jumpReport() {
    wx.navigateTo({
      url: '/pages/generateReport/generateReport'
    })
  },
  reportChange(e:WechatMiniprogram.CustomEvent) {
    const { value } = e.detail
    this.setData({
      selectIndex: value
    })
  },
  bindDateChange(e:WechatMiniprogram.CustomEvent) {
    const { value } = e.detail
    this.setData({
      endDate: value
    })
  },
  bindLimitChange(e:WechatMiniprogram.CustomEvent) {
    const { value } = e.detail
    this.setData({
      unlimited: value
    })
  },
  showShadow(name: string) {
    this.selectComponent('#' + name).show()
  },
  back() {
    wx.navigateBack({
      delta: 1
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getReportList()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})

export {}