// pages/reportDetail/reportDetail.ts
// import { certificateList, certificateDiscard, reportDelete } from '../../apis/report'
import { etcChoose } from "../../apis/phone";
import { reportCreateDate } from "../../apis/report";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // info: {
    //   id: ''
    // },
    // list: [],
    // status: true,
    // statusObj: {
    //   IN_EFFECT: '生效中',
    //   TIMEOUT: '已超时',
    //   DISCARD: '已作废',
    //   RUN_OUT_OF_TIMES: '次数已失效'
    // },
    // statusOptions: [
    //   {
    //     label: '生效中',
    //     value: true
    //   },
    //   {
    //     label: '已失效',
    //     value: false
    //   },
    // ]
    report: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    this.setData({
      info: options
    })
  },
  getList() {
    this.setData({
      report: wx.getStorageSync('etcList')
    })
  },
  handleEtc(e) {
    etcChoose({
      electronicCertificateId: e.currentTarget.dataset.id,
      phone: this.data.report.phone
    }).then(res => {
      console.log(res);
      if (res.c === 200) {
        wx.showToast({
          icon: 'none',
          title: '添加成功!'
        })
        wx.navigateBack({ delta: 1 })
      }
    })
  },
  goReport(){
    wx.navigateToMiniProgram({
      appId: 'wx1da91ccb94c17c99',
      path: 'pages/report/report',
      success(res) {
        // 打开成功
      }
    })
  },
  getData() {
    certificateList({
      reportId: this.data.info.id,
      status: this.data.status
    }).then((res: any) => {
      console.log(res)
      this.setData({
        list: res.d
      })
    })
  },
  handleDisCard(e: WechatMiniprogram.BaseEvent) {
    const { item } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: `是否作废[${item.name}]证书?`,
      success: (res) => {
        if (res.confirm) {
          certificateDiscard({
            id: item.id
          }).then(() => {
            wx.showToast({
              icon: 'none',
              title: '作废成功!'
            })
            this.getData()
          })
        }
      }
    })
  },
  del() {
    wx.showModal({
      title: '提示',
      content: '是否删除该报告?',
      success: (res) => {
        if (res.confirm) {
          reportDelete({
            id: this.data.info.id
          }).then(() => {
            wx.showToast({
              icon: 'none',
              title: '删除成功!'
            })
            wx.navigateBack()
          })
        }
      }
    })
  },

  changeStatus(e: WechatMiniprogram.BaseEvent) {
    const { value } = e.currentTarget.dataset
    this.setData({
      status: value
    })
    this.getData()
  },
  jumpWhiteList(e: WechatMiniprogram.BaseEvent) {
    const { item } = e.currentTarget.dataset
    console.log(item);
    wx.setStorageSync('whiteList', item.whiteList)
    wx.navigateTo({
      url: `/pages/whiteList/whiteList`
    })
  },

  jumpVisitor(e: WechatMiniprogram.BaseEvent) {
    const { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/visitor/visitor?id=${item.id}&name=${item.name}`
    })
  },
  jumpCreate() {
    wx.navigateTo({
      url: `/pages/certificateCreate/certificateCreate?id=${this.data.info.id}`
    })
  },
  checkReport() {
    // wx.setStorageSync('reportCreateDate',this.data.info.createDate)

    wx.navigateTo({
      url: `/pages/reportPreview/reportPreview?id=${this.data.info.id}&type=1&active=2`
    })
  },
  jumpCertificate(e: WechatMiniprogram.BaseEvent) {
    const { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/reportPreview/reportPreview?id=${item.id}&type=2&active=1`
    })
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})