var app = getApp()
var that;
Page({
  data: {
    detail: {},
    id: '',
    is: true,//是否是发放员进来
    disabled: false,
    sn:'',
    image:''
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "奖品兑换详情" });
    if (e.id) {
      this.setData({ id: e.id })
    }
    if (e.is) {
      this.setData({ is: false })
    }
  },
  showImg(e) {
    var url = e.target.dataset.url
    dd.previewImage({
      current: 0,
      urls:[url]
    });
  },
  onShow() {
    if (app.globalData.userData) {
      that.getData();
    } else {
      app.login(app.globalData.corpId, function (is) {
        if (is) {
          that.getData();
        } else {
          dd.reLaunch({
            url: '../../noJurisdiction/noJurisdiction'
          })
        }
      }, function () { })
    }
  },
  openText() {
    this.setData({ disabled: true });
    app.$post("api/shop/exchange/deal", { id: this.data.id }).then((res) => {
      app.globalData.showToast("已发放");
      setTimeout(() => {
        this.setData({ disabled: false });
        this.getData()
      }, 1000);
    }).catch(err => {
      this.setData({ disabled: false });
    })
  },
  showText() {
    app.$get('api/shop/exchange/qrcode', { id: this.data.id, employee_id: app.globalData.userData.id }).then((res) => {
      this.setData({
        image: app.globalData.baseUrl + res.data.data,
      })
    })
    this.setData({
      isBh: !this.data.isBh,
    })
  },
  showText2() {
    this.setData({
      isBh: !this.data.isBh,
    })
  },
  handleCopy() {
    dd.setClipboard({
      text: this.data.detail.sn,
    })
    app.globalData.showToast("已复制");
  },
  getData() {
    app.$get('api/shop/exchange/detail', { id: this.data.id }).then((res) => {
      let data = res.data.data;
      data.date = this.timestampToTime(data.create_time)
      data.date2 = this.timestampToTime(data.complete_time)
      this.setData({ detail: res.data.data })
    })
  },
  timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    var h = date.getHours() + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ) + ':';
    var s = date.getSeconds();
    return Y + M + D + h + m + s;
  },
});
