import http from '@/utils/request'

export function createICCard (form) {
  let params = {
    cardNo: 'string',
    createTime: '',
    id: 0,
    is_valid: 0,
    last_checkin: '',
    managerId: 0,
    member_id: 0,
    user_name: 'string',
    valid_end_date: '',
    valid_start_date: ''
  }

  return http.post('iccard/createICCard', params)
}

export function getICCardList (form) {
  let params = {
    keyWords1: form.cardId,
    keyWords2: form.userName,
    keyWords3: form.isValid,
    orderByKeyWords: 'string',
    pageNum: 1,
    pageSize: 10,
    startDate: form.startDate,
    endDate: form.endDate
  }

  return http.post('iccard/getICCardList', params)
}

// 明华IC读卡器, type预留对接其他读卡器
export function getICCardNo (type = 'M&H', vue) {
  const path = require('path')

  return new Promise((resolve, reject) => {
    let filePath = path.join(__static, 'iccard', 'index.js')

    let dllPath = path.join(__static, 'iccard', 'mwhrf_bj.dll')

    if (process.env.NODE_ENV === 'production') {
      dllPath = dllPath.replace('app.asar', 'app.asar.unpacked')
      filePath = filePath.replace('app.asar', 'app.asar.unpacked')
    }

    let child = require('child_process').fork(filePath)

    // console.log(path.join(__static, 'iccard', 'index.js'))
    vue.process = child

    child.send(dllPath)

    child.on('message', msg => {
      console.log(msg)
      switch (msg) {
        case 0:
          console.log('读卡结束')
          break
        default:
          if (typeof msg === 'string') {
            resolve(msg)
          } else {
            resolve()
          }
      }
    })

    child.on('exit', () => {
      resolve()
    })

    child.on('uncaughtException', function (err) {
      console.log('捕获到一个未被处理的错误:', err)
      reject(err)
    })
  })
}
// 获取社保卡信息
export function getSSCList (qrCodeNo) {
  return http.post(`order/getIdcardBySheBaoQRcode?code=${qrCodeNo}`)
}
