import Ajax from '../../util/ajax'
import api from '../../util/api'

export default {
  namespaced: true,
  state: {
    types: {},
    recommends: {
      list: []
    },
    books: {},
    minebooks: {},
  },
  mutations: {
    updateTypeList (state, payload) {
      state.types = payload
    },
    updateRecommendBooks (state, payload) {
      payload.list ? state.recommends.list = state.recommends.list.concat(payload.list) : null
    },
    updateBookList (state, payload) {
      state.books = payload
    },
    updateMineBook (state, payload) {
      state.minebooks = payload
    },
    updateRecommendBookLeftNum(state, bookid) {
      state.recommends = {...state.recommends, list: state.recommends.list.map(x => {
        if(x.BookId == bookid){
          x.LeftNum = x.LeftNum - 1
          return x
        }
        return x
      })}
      // let list = state.recommends.list
      // list = list.map(x => {
      //   if(x.BookId == bookid){
      //     x.LeftNum = x.LeftNum - 1
      //     return x
      //   }
      //   return x
      // })
      // list.splice(list.findIndex(item => item.LeftNum < 1), 1)
      // state.recommends = {...state.recommends, list}
    },
    updateBookLeftNum(state, bookid) {
      state.books = {...state.books, list: state.books.list.map(x => {
        if(x.BookId == bookid){
          x.LeftNum = x.LeftNum - 1
          return x
        }
        return x
      })}
    },
  },
  actions: {
    getBookType ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.library.getNavList,
          queryData: {
            ...payload
          }
        }).then(res => {
          commit('updateTypeList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getRecommendBook ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.library.getRecommendBookList,
          queryData: {
            ...payload
          }
        }).then(res => {
          commit('updateRecommendBooks', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getBookList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.library.getBookList,
          queryData: {
            ...payload,
            showzero: '0',
          },
        }).then(res => {
          commit('updateBookList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getMineBook ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.library.getMineRecord,
          queryData: {
            ...payload
          },
        }).then(res => {
          commit('updateMineBook', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    reserveBook (c, { bookid }) {
      return new Promise((resolve, reject) => {
        Ajax({
          ...api.library.reserve,
          urlData: {
            bookid,
          },
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      }) 
    },
    updateBookLeftNum({ commit }, bookid) {
      commit('updateBookLeftNum', bookid)
    },
    updateRecommendBookLeftNum({ commit }, bookid) {
      commit('updateRecommendBookLeftNum', bookid)
    },
  }
}