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

var booksData = [],
  recordData = [],
  appointmentListData = [],
  aTarget = {
    BookImg: '',
    BookName: '书名',
    id: '',
    Status: '状态',
    Author: '作者',
    Publisher: '出版社',
    LeftNum: '10',
    BookDescription: '描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述描述'
  },
  bTarget = {
    BookImg: '',
    BookName: '书名',
    id: '',
    Status: '状态',
    ReturnDate: '2018-08-02',
    CaseName: '案场名称'
  },
  cTarget = {
    BookImg: '',
    BookName: '书名',
    id: '',
    appointmentDate: '2018-08-02 12:00:00',
    CaseName: '案场名称'
  }
for (var n = 0; n < 10; n++) {
  booksData.push(aTarget)
  recordData.push(bTarget)
  appointmentListData.push(cTarget)
}

export default {
  namespaced: true,
  state: {
    indexSearchKey: '', // 图书首页搜索关键词
    navList: [{ // 图书馆首页nav
      icon: '',
      name: '名著',
      id: ''
    }, {
      icon: '',
      name: '文学',
      id: ''
    }, {
      icon: '',
      name: '历史',
      id: ''
    }, {
      icon: '',
      name: '学术',
      id: ''
    }, {
      icon: '',
      name: '童话',
      id: ''
    }, {
      icon: '',
      name: '天文',
      id: ''
    }, {
      icon: '',
      name: '期刊',
      id: ''
    }, {
      icon: '',
      name: '更多',
      id: 'more'
    }],
    bookList: booksData, // 图书馆首页推荐书籍
    bookTypeList: booksData, // 分类书籍列表
    searchBooksList: booksData, // 搜索书籍列表
    borrowedRecordList: recordData, // 借阅记录列表
    appointmentList: appointmentListData // 线上预约列表
  },
  mutations: {
    setIndexSearchKey (state, data) { // 赋值图书首页搜索关键词
      state.indexSearchKey = data || ''
    },
    setNavInfo (state, data) { // 赋值nav
      (data || []).map((item) => {
        state.navList.push({
          icon: '',
          name: '历史',
          id: ''
        })
      })
    },
    emptyNavInfo (state, data) { // 清空nav
      state.navList = []
    },
    setRecommendBookList (state, data) { // 赋值精选推荐书籍列表
      (data || []).map((item) => {
        state.bookList.push({
          BookImg: '',
          BookName: '书名',
          id: '',
          Status: '状态',
          Author: '作者',
          Publisher: '出版社',
          LeftNum: '10',
          BookDescription: '描述描述描述描述描述描述描述描述描述'
        })
      })
    },
    emptyRecommendBookList (state, data) { // 清空精选推荐书籍列表
      state.bookList = []
    },
    setBookTypeList (state, data) { // 赋值分类书籍列表
      (data || []).map((item) => {
        state.bookList.push({
          BookImg: '',
          BookName: '书名',
          id: '',
          Status: '状态',
          Author: '作者',
          Publisher: '出版社',
          LeftNum: '10',
          BookDescription: '描述描述描述描述描述描述描述描述描述'
        })
      })
    },
  },
  actions: {
    getBookListByType (context, data) { // 获取分类书籍列表
      return new Promise((resolve, reject) => {
        Ajax(api.library.getBookListByType.url, {
          method: api.library.getBookListByType.method
        }).then(res => {
          context.commit('setBookTypeList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getNavList (context, data) { // 获取nav数据
      return new Promise((resolve, reject) => {
        Ajax(api.library.getNavList.url, {
          method: api.library.getNavList.method
        }).then(res => {
          context.commit('setNavInfo', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
    getRecommendBookList (context, data) { // 获取精选推荐书籍列表
      return new Promise((resolve, reject) => {
        Ajax(api.library.getRecommendBookList.url, {
          method: api.library.getRecommendBookList.method
        }).then(res => {
          context.commit('setRecommendBookList', res)
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    }
  }
}
