<template>
  <!-- <button  class="btn btn-success width-60" @click='readCard()' :disabled="hasCard.cardState === -1"> -->
  <button  class="button_search" @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, param) {
    let hasCard = yield self.$resetpost('http://127.0.0.1:8003/HasCard', {}, {resolveMsg: null, rejectMsg: null}).catch((error) => {
      if (error.status === 612) {
        self.$showAlert('请检查读卡器是否启动！！', 'danger', 2000)
      }
    })
    if (JSON.parse(hasCard.data).state !== 1) {
      self.$showAlert('请检查卡是否插好!!', 'danger', 2000)
    } else {
      let data = {}
      if (param) {
        data.Factory = param
      }
      yield self.$resetpost('http://127.0.0.1:8003/ReadCard', data, {resolveMsg: null, rejectMsg: null}).then((res) => {
        console.log('返回卡信息')
        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)
        }
        // 验证卡上信息和用户信息是否一致
        if (param) {
          console.log('读完卡之后核对', res.data, self.isSingle)
          if (res.data.CardID != self.isSingle[0].f_card_id || res.data.Factory != self.isSingle[0].f_alias) {
            self.$showAlert('此时的卡片信息和用户信息不匹配，请核实后在进行读卡', 'warning', 5000)
            return
          }
        }

        self.$dispatch('read-info', res)
      }).catch((res) => {
        self.$showAlert(res.data, 'danger', 2000)
      })
    }
  }

  export default {
    title: '读卡',
    // data () {
    //   return {
    //     hasCard: cardListen
    //   }
    // },
    props: {
      cardInfo: {
        twoWay: true
      },
      isSingle: []
    },
    methods: {
      readCard () {
        console.log('查看是否只有单户', this.isSingle)
        // if (this.isSingle) {
        //   let gen = readGen(this, this.isSingle.length == 1 ? this.isSingle[0].f_alias : null)
        //   return co(gen)
        // } else {
        let gen = readGen(this)
        return co(gen)
        // }
      },
      async readCard2 (param) {
        // 这是二次掉卡服务，所以这里不做判卡处理
        let data = {}
        if (param) {
          data.kmm = param
        }
        let res = await this.$resetpost('http://127.0.0.1:8003/ReadCard', data, {resolveMsg: null, rejectMsg: '获取卡信息出错！！'})
        return res.data
      }
    }
  }
</script>
