import {
  queryResReport,
  queryUserReport,
  queryDepReport,
  queryProjectReport,
  queryProjectReportDetail,
  queryUserProjectReport,
  queryProject,
  queryDepCompare,
  queryDepCompareUser,
  queryFinanceReport,
  queryUserProject,
  queryProjectReportNew,
} from '@/services/workHours';
import { message } from 'antd';

export default {
  namespace: 'report',
  state: {
    res: {
      list: [],
      pagination: {},
    },
    depUser: {
      list: [],
      pagination: {},
    },
    projectUser: {
      list: [],
      pagination: {},
    },
    dep: {
      list: [],
      pagination: {},
    },
    // project: {
    //   list: [],
    //   pagination: {},
    // },
    project: [],
    finance: {
      list: [],
      pagination: {},
    },
    depCompare: [],
    depUserProject: [],
    projectList: [],
    projectNew: [],
  },

  effects: {
    *queryProject(_, { call, put }) {
      const { data } = yield call(queryProject, {});
      yield put({
        type: 'save',
        payload: { projectList: data.list },
      });
    },
    *queryResReport({ payload }, { call, put }) {
      const { data } = yield call(queryResReport, payload);
      yield put({
        type: 'save',
        payload: { res: data },
      });
    },
    *queryUserReport({ payload }, { call, put }) {
      const { data } = yield call(queryUserReport, payload);
      yield put({
        type: 'save',
        payload: { depUser: data },
      });
    },
    *queryUserProjectReport({ payload }, { call, put }) {
      const { data } = yield call(queryUserProjectReport, payload);
      // try {
      //   data.list.forEach(item => {
      //     let month = {};
      //     item.month_rpt.forEach(mItem => {
      //       let m = moment(mItem.ts).month();
      //       month[m] = mItem;
      //     });
      //     item.month = month;
      //   });
      // } catch (error) {
      //   console.error(error);
      // }
      yield put({
        type: 'save',
        payload: { projectUser: data },
      });
    },
    *queryDepReport({ payload = {}, callback }, { call, put, select }) {
      const { filter = {}, record = {} } = payload;
      const dep = yield select((s) => s.report.dep);
      const { data } = yield call(queryDepReport, {
        ...filter,
        dep_id: record.dep_id,
      });
      data.list.forEach((item) => {
        if (item.sub_dep_num) {
          item.children = [];
          item.isLeaf = false;
        } else {
          item.isLeaf = true;
        }
        item.isLoad = false;
      });

      const find = (data, id, callback) => {
        data.forEach((item) => {
          if (item.dep_id == id) {
            callback(item);
          }
          if (item.children) find(item.children, id, callback);
        });
      };

      // 判断是否为根节点
      if (record.dep_id) {
        let currentItem;
        find(dep.list, record.dep_id, (item) => {
          item.children = data.list.length == 0 ? null : data.list;
          item.isLoad = true;
        });
        yield put({
          type: 'save',
          payload: { dep: { ...dep, list: [...dep.list] } },
        });
      } else {
        yield put({
          type: 'save',
          payload: { dep: data },
        });
      }
      callback?.(data.list);
    },
    *queryProjectReport({ payload }, { call, put }) {
      const { data } = yield call(queryProjectReport, {
        ...payload,
      });
      const idHelper = (item, params) => {
        if (params.flag === undefined) {
          item.child.forEach((childItem) =>
            idHelper(childItem, { flag: item.id }),
          );
          item.key = 'f' + item.id;
        } else {
          item.flag = params.flag;
          if (params.project_id === undefined) {
            item.child?.forEach((childItem) =>
              idHelper(childItem, { flag: item.flag, project_id: item.id }),
            );
            item.key = 'f' + item.flag + 'p' + item.id;
          } else {
            item.project_id = params.project_id;
            item.key = 'f' + item.flag + 'p' + item.project_id + 't' + item.id;
          }
        }
      };
      data.forEach((item) => idHelper(item, {}));
      yield put({
        type: 'save',
        payload: {
          project: data,
        },
      });
    },
    // *queryProjectReport({ payload }, { call, put }) {
    //   const { data } = yield call(queryProjectReport, {
    //     ...payload,
    //     // pageSize: 20,
    //   });
    //   let treeData = {};
    //   try {
    //     data.list.forEach(item => {
    //       let p_type_id =
    //         item.rpt.project_id == 0 ? 't' + item.rpt.p_type_id : 'p' + item.rpt.project_id;
    //       treeData[p_type_id] = {
    //         id: p_type_id,
    //         type_name:
    //           item.rpt.project_id == 0
    //             ? item.rpt.project_name
    //             : `${item.rpt.project_name}(${item.rpt.project_code})`,
    //         project_name: item.rpt.project_name,
    //         project_id: item.rpt.project_id,
    //         project_code: item.rpt.project_code,
    //         p_type_id: item.rpt.p_type_id,
    //         month_rpt: item.rpt.month_rpt,
    //         classification: item.classification,
    //         project_ids: item.project_ids,
    //         type_code: '',
    //         children: [],
    //         month: {},
    //         isLoad: false,
    //         isParent: true,
    //       };
    //       item.rpt.month_rpt.forEach(mItem => {
    //         // let m = moment(mItem.ts).month();
    //         treeData[p_type_id].month[mItem.ts] = mItem;
    //       });
    //     });
    //   } catch (error) {
    //     console.log(error);
    //   }
    //   console.log(treeData);
    //   yield put({
    //     type: 'save',
    //     payload: {
    //       project: {
    //         list: Object.values(treeData),
    //         pagination: data.pagination,
    //       },
    //     },
    //   });
    // },
    *queryProjectReportDetail({ payload = {} }, { call, put, select }) {
      const { record = {} } = payload;
      const { data } = yield call(queryProjectReportDetail, {
        s_time: payload.s_time,
        e_time: payload.e_time,
        classification: record.classification,
        p_type_id: record.p_type_id,
        project_ids: record.project_ids.join(','),
      });
      data.forEach((item, index) => {
        let month = {};
        item.month_rpt.forEach((mItem) => {
          // let m = moment(mItem.ts).month();
          month[mItem.ts] = mItem;
        });

        item.month = month;
        item.id = record.id + '_' + index;
      });
      const project = yield select((s) => s.report.project);
      record.children =
        data.length == 0
          ? null
          : data.map((item) => ({ ...item, project_ids: record.project_ids }));
      record.isLoad = true;
      yield put({
        type: 'save',
        payload: { project: { ...project } },
      });
    },
    *queryFinanceReport({ payload = {} }, { call, put }) {
      const { data } = yield call(queryFinanceReport, payload);
      yield put({
        type: 'save',
        payload: { finance: data },
      });
    },
    *queryDepCompare({ payload }, { call, put }) {
      let { data } = yield call(queryDepCompare, payload);
      if (data) {
        const treeHelper = (item) => {
          item.key = 'd' + item.dep_id;
          if (!item.child && item.total_cnt) item.child = [];
          item.child &&
            item.child.forEach((childItem) => treeHelper(childItem));
        };
        data.forEach((item) => treeHelper(item));
      } else data = [];
      yield put({
        type: 'save',
        payload: { depCompare: data },
      });
    },
    *queryDepCompareUser({ payload }, { call, put, select }) {
      try {
        let record = payload.record;
        payload.dep_id = record.dep_id;
        delete payload.record;
        let { data } = yield call(queryDepCompareUser, payload);
        data.forEach((item) => {
          item.key = 'u' + item.user_id;
          item.dep_id = payload.dep_id;
        });
        record.child = [...record.child, ...data];
        record.isLoad = true;
      } catch (error) {
        console.log(error);
      }
      const depCompare = yield select((s) => s.report.depCompare);
      yield put({
        type: 'save',
        payload: { depCompare: [...depCompare] },
      });
    },
    *queryDepUserProject({ payload }, { call, put }) {
      const { data } = yield call(queryUserProject, payload);
      yield put({
        type: 'save',
        payload: { depUserProject: data },
      });
    },

    *queryProjectReportNew({ payload }, { call, put }) {
      const res = yield call(queryProjectReportNew, payload);
      if (!res) return;
      const initData = (data, parentKey) => {
        let currentKey = parentKey + (data.id || data.name);
        data.key = currentKey;
        if (data.child) {
          data.child.forEach((item) => {
            initData(item, currentKey + '-');
          });
        }
      };
      initData(res.data, '');
      yield put({
        type: 'save',
        payload: { projectNew: res.data.child || [] },
      });
    },
  },

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