const Http = require('../../utils/http.js')
const Common = require('../../common/common.js')
const app = getApp()

Page({
  data: {
    inputValue:"",
    keyBordList: [{
        id: "1",
        num: '1',
      },
      {
        id: "2",
        num: '2'
      },
      {
        id: "3",
        num: '3'
      },
      {
        id: "4",
        num: '4'
      },
      {
        id: "5",
        num: '5'
      },
      {
        id: "6",
        num: '6'
      },
      {
        id: "7",
        num: '7'
      },
      {
        id: "8",
        num: '8'
      },
      {
        id: "9",
        num: '9'
      },
      {
        id: "10",
        num: '.'
      },
      {
        id: "11",
        num: '0',
      }
    ],
    flag: true,
    val: ''
  },
  hideKey() {
    this.setData({
      flag: false
    })
  },
  onClick(e) {
    let {
      id,
      num
    } = e.currentTarget.dataset.val
    if (this.data.val == '请输入券码') {
      this.setData({
        val: ''
      })
    }
    switch (num) {
      case '1':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '2':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '3':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '4':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '5':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '6':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '7':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '8':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '9':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '.':
        this.setData({
          val: this.data.val += num
        })
        break;
      case '0':
        this.setData({
          val: this.data.val += num
        })
        break;
    }
  },
  search() {
    this.setData({
      flag: true
    })
  },
  // delVal() {
  //   let {
  //     val
  //   } = this.data
  //   val = val.substr(0, val.length - 1)
  //   this.setData({
  //     val
  //   })
  // },

  /**
   * 获取当前网络状态
   */
  getNetWorkType: function() {
    tt.getNetworkType({
      success: function(res) {
        console.log(res)
      }
    })
  },
  /**
   *  获取系统信息
   */
  getSystemInfo: function() {
    tt.getSystemInfo({
      success: function(res) {
        console.log(res)
      }
    })
  },
  setVale(e){
    console.log(e.detail.value);
    this.setData({
      inputValue:e.detail.value
    })
  },
  toWriteoffPage() {
    let val = this.data.inputValue;
    console.log(val);

    if (val != '请输入券码' && val != '') {
      console.log(val)
      Common.getCouponOrderDetail(val)
        .then(res => {
          console.log(res);
          if (res.code != 200) {
            tt.reLaunch({
              url: `/pages/errorPage/errorPage?errTitle=${res.message}`,
            })
          } else {
            tt.setStorageSync("couponOrderId", val)
            tt.navigateTo({
              url: `/pages/writeoffPage/writeoffPage`
            })
          }
        })
    } else if (val == '') {
      tt.showToast({
        title: '券码不能为空',
        image: '../../static/images/fail.png'
      })
    } else {
      tt.showToast({
        title: '券码不能为空',
        image: '../../static/images/fail.png'
      })
    }
  }
})