/**
 * long description for the file
 *
 * @summary 机构管理
 * @author wu <308822989@qq.com>
 *
 * Created at     : 2018-08-01 18:01:57
 * Last modified  : 2018-08-27 14:54:31
 */

// 机构管理
import axios from '@/config/axios.js';
import { proPath } from '@/config/config.js';
import { getCookie } from '@/utils/util.js';

const state = {
  //  列表
  tree: [],
  //  选中多列
  items: [],
  //  选中列
  item: [],
  //  默认配置数据对应项
  defaultProps: {
    children: 'children',
    label: 'orgName'
  },
  //  默认展开的ID数组
  expandedKeys: [],
  //  机构类别
  formItemCate: [
    {
      label: '机构',
      value: '1'
    },
    {
      label: '部门',
      value: '0'
    }
  ],
  //  是否虚机构
  formItemIsVirtual: [
    {
      label: '实机构',
      value: '0'
    },
    {
      label: '虚机构',
      value: '1'
    }
  ]
}

const getters = {
}

const mutations = {
  //  更新list
  tree (state, tree) {
    state.tree = tree;
  },
  //  更新
  items (state, items) {
    state.items = items;
  },
  //  更新
  item (state, item) {
    state.item = item;
  },
  //  更新
  expandedKeys (state, expandedKeys) {
    state.expandedKeys = expandedKeys;
  }
}
const actions = {
  //  取菜单
  tree ({ commit, state }, id) {
    let companyId = window.top.localStorage.getItem('companyId') ? window.top.localStorage.getItem('companyId') : JSON.parse(window.top.localStorage.getItem('companys'))[0].id;
    let res = id ? axios.get(proPath + 'v1/sysorgs/listtree?companyId=' + id) : axios.get(proPath + 'v1/sysorgs/listtree?companyId=' + companyId);
    return res;
  },
  //  修改
  editOrg ({ commit, state }, obj) {
    let res = axios.put(proPath + 'v1/sysorgs/' + obj.id, obj.form);
    return res;
  },
  //  添加
  addOrg ({ commit, state }, form) {
    delete form.children;
    let res = axios.post(proPath + 'v1/sysorgs/', form);
    return res;
  },
  //  删除
  delOrg ({ commit, state }, id) {
    let res = axios.delete(proPath + 'v1/sysorgs/' + id);
    return res;
  },
  // 拖动机构
  dragOrg ({ commit, state }, obj) {
    let res = axios.put(proPath + 'v1/sysorgs/modifyOrgByDrag?type=' + obj.dropType, obj.changeList);
    return res;
  }
}

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