import { message } from 'antd';
import { queryRole, queryDep, queryDepV2, queryProjectRole } from '@/Project/services/SysAdmin';

import {
  addProject,
  updateProject,
  removeProject,
  // addProjectUser,
  addProjectUserV2,
  queryProjectById,
  queryProject,
  removeProjectUser,
} from '@/Project/services/ProjectAdmin';

function getSelectTreeData(data) {
  data.title = `${data.Name}`;
  data.key = data.ID;
  data.value = data.ID;
  return data;
}

function getDepTreeData(data) {
  data.title = `${data.Name}`;
  data.key = data.ID;
  data.value = data.ID;
  if (data.children) {
    data.children.forEach(item => {
      getDepTreeData(item, false);
    });
  }
  return data;
}

function getDepUserTree(data) {
  data.title = `${data.Name}`;
  data.key = `dep-${data.ID}`;
  data.value = `dep-${data.ID}`;
  data.selectable = false;
  if (!data.children) data.children = new Array();

  if (data.children) {
    data.children.forEach(item => {
      getDepUserTree(item, false);
    });
  }

  if (data.Users && data.Users.length !== 0) {
    data.Users.forEach(item => {
      item.title = item.CName;
      item.key = item.ID;
      item.value = item.ID;
      item.selectable = true;
      data.children.push(item);
    });
  }
  return data;
}

const depDict = {};

function getDepMapData(data) {
  depDict[data.ID] = data;
  if (data.children) {
    data.children.forEach(item => {
      getDepMapData(item, false);
    });
  }
}

export default {
  namespace: 'pro',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    roleList: {},
    projectRoleList: [],
    depTrees: [],
    depDict: {},
    currentProject: {},
    depUserTree: [],
  },

  effects: {
    *fetchById({ payload, callback }, { call, put }) {
      const response = yield call(queryProjectById, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { currentProject: response.data },
        });
        callback?.(response.data);
      }
      
    },

    *fetch({ payload }, { call, put }) {
      const response = yield call(queryProject, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { data: response.data },
        });
      }
    },

    // 查询项目所拥有的角色
    *queryProjectRole({ payload }, { call, put }) {
      const response = yield call(queryProjectRole, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { projectRoleList: response.data.full_roles },
        });
      }
    },
    *fetchRole({ payload }, { call, put }) {
      const response = yield call(queryRole, { pageSize: 999999 });
      if (response) {
        const roleList = response.data.list.map(item => {
          return getSelectTreeData(item);
        });
        yield put({
          type: 'save',
          payload: { roleList },
        });
      }
    },
    *fetchDep({ payload, callback }, { call, put }) {
      const response = yield call(queryDep, { pageSize: 999999 });
      if (response) {
        const depTrees = response.data.list.map(item => {
          getDepMapData(item);
          return getDepTreeData(item);
        });
        callback && callback(depTrees);
        const depUserTree = response.data.list.map(item => {
          return getDepUserTree(item);
        });

        yield put({
          type: 'save',
          payload: { depTrees, depDict, depUserTree },
        });
      }
    },
    *fetchDepV2({ payload, callback }, { call, put }) {
      const response = yield call(queryDepV2, { pageSize: 999999 });
      if (response) {
        const depTrees = response.data.list.map(item => {
          getDepMapData(item);
          return getDepTreeData(item);
        });
        // console.log(depTrees);
        var arr = JSON.parse(JSON.stringify(response.data.list));
        const depUserTree = arr.map(item => {
          return getDepUserTree(item);
        });
        callback && callback(depTrees);
        yield put({
          type: 'save',
          payload: { depTrees, depDict, depUserTree },
        });
      }
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addProject, { ...payload.item });
      if (response) {
        yield put({
          type: 'fetch',
          payload: { ...payload.value },
        });
        if (callback) callback();
      }
    },

    *update({ payload, callback }, { call, put }) {
      const response = yield call(updateProject, payload);
      if (response) {
        yield put({
          type: 'fetch',
          payload: {},
        });
        if (callback) callback();
        message.success('修改成功');
      }
    },

    *remove({ payload, callback }, { call, put }) {
      const response = yield call(removeProject, payload);
      if (response) {
        yield put({
          type: 'fetch',
          payload: {},
        });
        if (callback) callback();
        message.success('删除成功');
      }
    },

    *addProjectUser({ payload, callback }, { call, put }) {
      const response = yield call(addProjectUserV2, payload);
      // const response = yield call(addProjectUser, payload);
      if (response) {
        yield put({
          type: 'fetchById',
          // payload: { ID: payload.ID },
          payload: { ID: payload.project_id },
        });
        if (callback) callback();
        message.success('添加项目用户成功');
      }
    },

    *deleteProjectUser({ payload, callback }, { call, put }) {
      const response = yield call(removeProjectUser, payload);
      if (response) {
        yield put({
          type: 'fetchById',
          payload: { ID: payload.ID },
        });
        if (callback) callback();
        message.success('删除项目用户成功');
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
