/**
 * long description for the file
 *
 * @summary short description for the file
 * @author wu <308822989@qq.com>
 *
 * Created at     : 2018-08-27 12:28:00
 * Last modified  : 2019-01-08 17:44:02
 */

import axios from '@/config/axios.js';
import moment from 'moment';
import { proPath } from '@/config/config.js';
import { getCookie } from '@/utils/util.js';

const state = {
  //  列表
  menuTree: [],
  //  选中多列
  items: [],
  //  选中列
  item: [],
  //  默认配置数据对应项
  defaultProps: {
    children: 'children',
    label: 'menuLabel'
  },
  //  默认展开的ID数组
  expandedKeys: [],
  // menu菜单点击的菜单ID
  permissionsMenuId: ''
}

const getters = {
}

const mutations = {
  //  更新list
  menuTree (state, menuTree) {
    state.menuTree = menuTree;
  },
  //  更新
  items (state, items) {
    state.items = items;
  },
  //  更新
  item (state, item) {
    state.item = item;
  },
  //  更新
  expandedKeys (state, expandedKeys) {
    state.expandedKeys = expandedKeys;
  },
  permissionsMenuId (state, permissionsMenuId) {
    state.permissionsMenuId = permissionsMenuId;
  }
}
const actions = {
  //  取菜单
  menuTree ({ commit, state }, obj) {
    let companyId = window.top.localStorage.getItem('companyId') ? window.top.localStorage.getItem('companyId') : JSON.parse(window.top.localStorage.getItem('companys'))[0].id;
    let res = (obj && obj.companyId && obj.appId) ? axios.get(proPath + 'v1/sysmenus/findMenusByUserId?companyId=' + obj.companyId + '&appId=' + obj.appId) : axios.get(proPath + 'v1/sysmenus/findMenusByUserId?companyId=' + companyId);
    return res;
  },
  //  修改菜单
  editMenu ({ commit, state }, obj) {
    let res = axios.put(proPath + 'v1/sysmenus/' + obj.id, obj.form);
    return res;
  },
  //  添加菜单
  addMenu ({ commit, state }, form) {
    delete form.children;
    let res = axios.post(proPath + 'v1/sysmenus/', form);
    return res;
  },
  //  菜单
  delMenu ({ commit, state }, id) {
    let res = axios.delete(proPath + 'v1/sysmenus/' + id);
    return res;
  },
  // 拖动菜单
  dragMenu ({ commit, state }, obj) {
    let res = axios.put(proPath + 'v1/sysmenus/modifyMenuByDrag?type=' + obj.dropType, obj.changeList);
    return res;
  }
}

//
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
