/*
 * @Author: your name
 * @Date: 2022-01-04 15:14:05
 * @LastEditTime: 2022-01-19 15:30:51
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \goldenPig\src\store\index.js
 */
import Vue from 'vue'
import Vuex from 'vuex'
import {
  getMyButton,
  // getFarm
} from '../utils/api'
import { getEarFarmList } from "../utils/apis/eartag-data/eartagData";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    color: '#31C3A6',
    mode: true,
    // 按钮权限
    buttons: [],
    ip: 'http://120.27.234.126:8010',
    // 所选择的farmId
    farmId: '',
    farmList: [],
    userName: '',
    stageList: [ // 阶段
      {
        value: 1,
        label: '配怀'
      },
      {
        value: 2,
        label: '分娩'
      },
      {
        value: 3,
        label: '保育'
      },
      {
        value: 4,
        label: '育成育肥'
      },
      {
        value: 5,
        label: '空怀'
      },
      {
        value: 6,
        label: '后备母猪'
      },
      {
        value: 7,
        label: '公猪'
      },
      {
        value: 8,
        label: '病死猪场内收集'
      },
      {
        value: 9,
        label: '病死猪无害化'
      }
    ],
    // 判断用户类型
    userType: 0,
  },
  mutations: {
    setColor(state, data) {
      state.color = data
    },
    setMode(state, data){
      state.mode = data
    },
    SET_BUTTONS(state, data) {
      state.buttons = data
    },
    SET_FARMID(state, data) {
      state.farmId = data;
    },
    SET_FARMLIST(state, data) {
      state.farmList = data;
    },
    SET_USERNAME(state, data) {
      state.userName = data;
    },
    SET_USERTYPE(state, data) {
      state.userType = data
    }
  },
  actions: {
    setModeAsync(context, data) {
      context.commit('setMode' ,data)
    },
    setColorAsync(context, data) {
      context.commit('setColor', data)
    },
    setFarmIdAsync(context,data) {
      context.commit('SET_FARMID', data)
    },
    setUserNameAsync(context, data) {
      context.commit('SET_USERNAME', data)
    },
    setUserTypeAsync(context, data) {
      context.commit('SET_USERTYPE', data)
    },
    // 获取用户按钮权限
    GetButtons({ commit }) {
      return new Promise((resolve, reject) => {
        getMyButton({userId: localStorage.getItem('gold_UserId')}).then(res => {
          if(res.code === 10000) {
            commit('SET_BUTTONS', res.data)
            resolve(res.data)
          } else {
            reject(new Error(res.message))
          }
        }).catch(error => {
            reject(error)
          })
      })
    },
    // 获取牧场列表
    GetFarm({ commit }){
      return new Promise((resolve, reject) => {
        getEarFarmList({type: 2}).then(res => {
          if (res && res.code === 10000) {
            commit('SET_FARMLIST', res.data);
            resolve(res.data);
          } else {
            res && reject(new Error(res.message))
          }
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  modules: {
  },
  getters: {
    buttons: state => state.buttons
  }
})
