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

export default {
  namespaced: true,
  state: {
    weatherInfo: {
      'status': '1',
      'count': '1',
      'info': 'OK',
      'infocode': '10000',
      'forecasts': [{
        'city': '南京市',
        'adcode': '320100',
        'province': '江苏',
        'reporttime': '2019-02-21 13:48:01',
        'casts': [{
          'date': '2019-02-21',
          'week': '4',
          'dayweather': '晴间多云',
          'nightweather': '雨夹雪',
          'daytemp': '7',
          'nighttemp': '2',
          'daywind': '东',
          'nightwind': '东',
          'daypower': '≤3',
          'nightpower': '≤3'
        }, {
          'date': '2019-02-22',
          'week': '5',
          'dayweather': '小雨',
          'nightweather': '多云',
          'daytemp': '6',
          'nighttemp': '0',
          'daywind': '北',
          'nightwind': '北',
          'daypower': '≤3',
          'nightpower': '≤3'
        }, {
          'date': '2019-02-23',
          'week': '6',
          'dayweather': '多云',
          'nightweather': '多云',
          'daytemp': '11',
          'nighttemp': '1',
          'daywind': '东北',
          'nightwind': '东北',
          'daypower': '≤3',
          'nightpower': '≤3'
        }, {
          'date': '2019-02-24',
          'week': '7',
          'dayweather': '多云',
          'nightweather': '多云',
          'daytemp': '10',
          'nighttemp': '4',
          'daywind': '东',
          'nightwind': '东',
          'daypower': '≤3',
          'nightpower': '≤3'
        }]
      }]
    }
  },
  mutations: {
    setWeatherInfo (state, data) {
      state.weatherInfo = data
    }
  },
  actions: {
    getWeatherInfo (context, payload) { // 获取天气信息
      return new Promise((resolve, reject) => {
        Ajax(api.getWeatherInfo.url, {
          method: api.getWeatherInfo.method
        }).then(res => {
          context.commit('setWeatherInfo', res)
          resolve(res)
        }).catch((res) => {
          reject(res)
        })
      })
    },
    getPersonB ({ commit }) {
      return Ajax(api.person.url, {
        method: api.person.method
      })
    },
    getPerson ({ commit }) {
      return Ajax(api.person2.url, {
        method: api.person2.method
      })
    }
  }
}
