<template>
  <!-- <button  class="btn btn-success width-60" @click='readCard()' :disabled="hasCard.cardState === -1"> -->
  <button  class="btn btn-success width-60" @click='readCard()'>
    <!-- <span class="glyphicon glyphicon-credit-card"></span> -->
    读卡
  </button>
</template>
<script>
/**
*卡服务
*读卡
*/
import co from 'co'
// import cardListen from '../../plugins/CardListen'
// import CardService from '../../../plugins/CardService'

let readGen = function * (self) {
  let hasCard = yield self.$resetpost('http://127.0.0.1:8003/HasCard', {}, {resolveMsg: null, rejectMsg: null})
  console.log('hascard返回，，，', hasCard)
  if (JSON.parse(hasCard.data).state === 0) {
    self.$showAlert('请检查卡是否插好!!', 'danger', 2000)
  } else {
    yield self.$resetpost('http://127.0.0.1:8003/ReadCard', {data: {}}, {resolveMsg: null, rejectMsg: null}).then((res) => {
      self.cardInfo = res
      res.data.Err ? self.$showAlert(res.data.Err, 'danger', 2000) : ''
      if (res.data.Err === null && res.data.Exception) {
        self.$showAlert(res.data.Exception, 'danger', 2000)
      }
      self.$dispatch('read-info', res)
    }).catch((res) => {
      self.$showAlert(res.data, 'danger', 2000)
    })
  }
  // self.$dispatch('read-info', {data: {CardID: '10000000000024', Factory: 'XianFengPTKS', Money: 0, Gas: 0}})
}

export default {
  title: '读卡',
  // data () {
  //   return {
  //     hasCard: cardListen
  //   }
  // },
  props: {
    cardInfo: {
      twoWay: true
    }
  },
  // watch: {
  //   'hasCard.cardState' (val){
  //     if (val === -1) {
  //       this.$dispatch('refresh')
  //     }
  //   }
  // },
  methods: {
    readCard () {
      let gen = readGen(this)
      return co(gen)
      // CardService.readCard().then((res) => {
      //   this.cardInfo = res
      //   res.data.Err ? this.$showAlert(res.data.Err, 'danger', 2000) : ''
      //   if (res.data.Err === null && res.data.Exception) {
      //     this.$showAlert(res.data.Exception, 'danger', 2000)
      //   }
      //   this.$dispatch('read-info', res)
      // }).catch((res) => {
      //   console.log('读卡', res.data)
      //   this.$showAlert(res.data, 'danger', 2000)
      // })
    }
  }
}
</script>
