import storage from 'store'
import { postchecLogin, login, getInfo, postunlock } from '../check/innerCheck/api'
import notification from 'ant-design-vue/es/notification'
function loginfn1(values) {
  let p1 = new Promise((relove, reject) => {
    postunlock(values).then(res => {
      relove(res)

      if (res.success) {
        // this.$message.success(res.message)
        relove(res)
      } else {
        // this.$message.error(res.message)
        // reject(res.success)
      }
    })
  })
  return p1
}
function loginfn2(values) {
  let p2 = new Promise((relove, reject) => {
    login(values).then(res => {
      if (res.success) {
        // this.$message.success(res.message)
        relove(res)
      } else {
        // this.$message.error(res.message)
        reject(res.success)
      }
    })
  })
  return p2
}
function getInfofn(values) {
  let p3 = new Promise((relove, reject) => {
    getInfo(values).then(res => {
        // console.log(res,'获取登录信息11111111111111')
      if (res.data.success) {
        // this.$message.success(res.message)
        relove(res.data)
      } else {
        // this.$message.error(res.message)
        reject(res.data.success)
      }
    })
  })
  return p3
}
const outBound = {
  state: {
    checkPerson: {},
    name: '1',
    stationCode: '',
    businesstype: '',
    worktype: '',
    warehouseId: '',
	isDoubleCheck:'0' 	//是否双人复核，默认0单人，1双人
  },

  mutations: {
    SET_PERSON: (state, checkPerson) => {
      state.checkPerson = checkPerson
    },
    SET_STATIONCODE: (state, stationCode) => {
      state.stationCode = stationCode
    },
    SET_BUSINESSTYPE: (state, businesstype) => {
        state.businesstype = businesstype
    },
    SET_WORKTYPE: (state, worktype) => {
        state.worktype = worktype
    },
    SET_WAREHOUSEID: (state, warehouseId) => {
        state.warehouseId = warehouseId
    }
  },

  actions: {
    //复核登录
    CheckLoginIn({ commit }, values) {
      return new Promise((resolve, reject) => {
        loginfn1(values).then(res => {
          if(res.success) {
            // console.log(res, '校验权限')
            // const params = {
            //   username: values.workercode,
            //   password: values.workpassword
            // }
            // commit('SET_STATIONCODE',values.stationCode)
            // commit('SET_BUSINESSTYPE',values.businesstype)
            // commit('SET_WORKTYPE',values.worktype)
            // commit('SET_WAREHOUSEID',values.warehouseId)
            return resolve(values)
          } else {
            reject(res)
          }

        })
        .then(res => {
            // console.log(res,'账号登录')
            return getInfofn(res.data.token)
        })
        .then(res => {
            // console.log(res,'获取用户信息')
            const userInfo = {
              userId: res.data.userId,
              username: res.data.username
            }
            commit('SET_PERSON', userInfo)
            resolve(res)
            // console.log(userInfo,'THIS111111111111')
        })
        .catch(error => {
            console.log(error,'resresresresresresresres')
            reject(error)
        })
      })
    }
  }
}

export default outBound
