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

export default {
  namespaced: true,

  state: {
    page: 0,
    pagenum: 0,
    list: [
      {
        BookId: '1',
        BookTypeId: '1',
        BookName: '遮天',
        BookBarcode: '123456',
        BookImg: 'http://jingcheng-resourceplat.oss-cn-shanghai.aliyuncs.com/upload/135230460466.png',
        Author: '辰东',
        Publisher: '南京出版社',
        Price: 90.00,
        InStock: 10,
        LeftNum: 2,
        BorrowNum: 5,
        BorrowDays: 7,
        BookDescription: '《遮天》是一部古典仙侠类型的网络小说',
        CaseId: '1',
        OrgId: '1',
        CreateDate: new Date(),
        IsRecommend: 1,
        Status: 1,
      },
    ],
    detail: {},
    type: {
      page: 0,
      pagenum: 0,
      list: [
        {
          BookTypeId: '1',
          BookTypeName: '测试',
          BookTypeImg: 'http://jingcheng-resourceplat.oss-cn-shanghai.aliyuncs.com/upload/135230460466.png',
          CaseId: '1',
          OrgId: '1',
          Nums: 100,
          Status: 1,
          CreateDate: new Date()
        }
      ],
      detail: {
        BookTypeId: '',
        BookTypeName: '',
        BookTypeImg: '',
        CaseId: '',
        OrgId: '1',
        Status: 1,
        CreateDate: new Date()
      },
    },
    stock: {
      page: 0,
      pagenum: 0,
      list: [
        {
          RecordId: '1',
          BookId: '1',
          InStockTotal: 20,
          InStockBefore: 8,
          InStockAfter: 10,
          ChangeReason: '补库存',
          ChangeDate: new Date(),
          ChangeUser: '1',
          ChangeUserName: 'admin',
          CaseId: '1',
          OrgId: '1',
        }
      ],
      detail: {},
    },
    borrow: {
      list: [],
      history: {},
    },
    bookReturn: {
      list: [],
    },
  },
  mutations: {
    mergeTypeDetail (state, payload) {
      state.type.detail = payload || {}

      if (payload && payload.BookTypeId) {
        state.type.list = (state.type.list || []).map(x => x.BookTypeId === payload.BookTypeId ? payload : x)
      }
    },
    mergeBookDetail (state, payload) {
      state.detail = payload || {}

      if (payload && payload.BookId) {
        state.list = (state.list || []).map(x => x.BookId === payload.BookId ? payload : x)
      }
    },
    mergeHistory (state, payload) {
      state.borrow.history = payload
    },
    setBorrowList (state, payload) { // 更新借阅记录
      state.borrow.list = payload
    },
    setTypeList (state, payload) {
      const { list, pagenum } = payload || {}
      if (!!list) { // eslint-disable-line
        state.type.list = list
        state.type.pagenum = pagenum
      } else {
        state.type.list = []
        state.type.pagenum = 0
      }
    },
    deleteTypeList (state, payload = {}) {
      state.type.list = state.type.list.filter(x => x.BookTypeId !== payload.BookTypeId)
    },
    setBookList (state, payload) {
      const { list, pagenum } = payload || {}
      if (!!list) { // eslint-disable-line
        state.list = list
        state.pagenum = pagenum
      } else {
        state.list = []
        state.pagenum = 0
      }
    },
    deleteBookList (state, payload = {}) {
      state.list = state.list.filter(x => x.BookId !== payload.BookId)
    },
    setUserReturn (state, payload = {}) {
      state.bookReturn = payload
    }
  },
  actions: {
    cancelBorrow ({ commit }, payload) { // 取消预约
      return new Promise((resolve, reject) => {
        ajax(api.borrow.cancelBorrow.url, {
          method: api.borrow.cancelBorrow.method,
          urlData: { id: payload }
        }).then(res => {
          resolve(res)
        }).catch(({ message }) => reject(message))
      })
    },
    getBorrowList ({ commit }, payload) { // 获取借阅记录
      return new Promise((resolve, reject) => {
        ajax(api.borrow.getBorrowList.url, {
          method: api.borrow.getBorrowList.method,
          queryData: {
            ...payload,
          }
        }).then(res => {
          commit('setBorrowList', res.list)
          resolve(res)
        }).catch(({ message }) => reject(message))
      })
    },
    getTypeList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.bookType.list,
          queryData: payload,
        }).then((x) => {
          commit('setTypeList', x)
          resolve(x)
        })
          .catch(({ message }) => reject(message))
      })
    },
    getTypeDetail ({ commit }, payload = {}) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.bookType.detail,
          urlData: { ...payload },
        }).then((x) => {
          commit('mergeTypeDetail', x)
          resolve(x)
        })
          .catch(({ message }) => reject(message))
      })
    },
    updateTypeDetail ({ commit }, payload = {}) {
      return new Promise((resolve, reject) => {
        const id = payload.BookTypeId
        payload.CreateDate = ''

        const apiConf = !id ? api.bookType.save : api.bookType.update
        ajax({
          ...apiConf,
          data: payload,
        }).then((x) => {
          commit('mergeTypeDetail', x)
          resolve(x)
        })
          .catch(({ message }) => reject(message))
      })
    },
    deleteType ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.bookType.delete,
          urlData: { id: payload.BookTypeId },
        }).then(() => {
          commit('deleteTypeList', payload)
          resolve()
        })
          .catch(({ message }) => reject(message))
      })
    },
    getBookList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.list,
          queryData: payload,
        }).then((x) => {
          commit('setBookList', x)
          resolve(x)
        })
          .catch((err) => console.log(err) && reject(err.message))
      })
    },
    getBookDetail ({ commit }, payload = {}) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.detail,
          urlData: { ...payload },
        }).then((x) => {
          commit('mergeBookDetail', x)
          resolve(x)
        })
          .catch(({ message }) => reject(message))
      })
    },
    updateBookDetail ({ commit }, payload = {}) {
      return new Promise((resolve, reject) => {
        const id = payload.BookId

        const apiConf = !id ? api.book.save : api.book.update
        ajax({
          ...apiConf,
          data: payload,
        }).then((x) => {
          commit('mergeBookDetail', x)
          resolve(x)
        })
          .catch(({ message }) => reject(message))
      })
    },
    deleteBook ({ commit }, payload = {}) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.delete,
          urlData: { id: payload.BookId },
        }).then((x) => {
          commit('deleteBookList', payload)
          resolve(x)
        })
          .catch(({ message }) => reject(message))
      })
    },
    getStockList ({ commit }, payload) {
      return new Promise((resolve, reject) => {

      })
    },
    batchImport(_, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.import,
          data: payload,
        }).then(() => {
          resolve()
        })
          .catch(({ message }) => reject(message))
      })
    },
    getBorrowHistory({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.history,
          urlData: payload,
        }).then((res) => {
          if (!!res) { // eslint-disable-line
            commit('mergeHistory', res)
            resolve(res)
          } else {
            reject('未检索到数据') // eslint-disable-line
          }
        })
          .catch(({ message }) => reject(message))
      })
    },
    borrowBooks (_, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.borrow,
          urlData: { ...payload },
        }).then(() => {
          resolve()
        })
          .catch(({ message }) => reject(message))
      })
    },
    getUserReturn ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.getUserReturn,
          urlData: { ...payload },
          queryData: payload,
        }).then((res) => {
          commit('setUserReturn', res)
          resolve(res)
        })
          .catch(({ message }) => reject(message))
      })
    },
    returnBook ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax({
          ...api.book.returnBook,
          urlData: { ...payload },
        }).then((res) => {
          resolve(res)
        })
          .catch(({ message }) => reject(message))
      })
    }
  },
}
