import {
  queryAnalysisDict,
  queryProcessSection,
} from '@/services/OperationManagement';
import { queryList, queryListNew } from '@/services/diagnosticTec';
export default {
  namespace: 'smartOps',

  state: {
    list: [],
    processList: [],
  },

  effects: {
    *queryList({ payload, callback }, { call, put, select }) {
      if (!payload.ids) {
        yield put({
          type: 'save',
          payload: {
            list: {
              list: [],
              pagenation: { total: 0 },
            },
          },
        });
        return;
      }
      if (payload.isNewRole) {
        const analysisDict = yield call(queryAnalysisDict, { pageSize: 9999 });
        const response = yield call(queryListNew, payload);
        if (response) {
          response?.data?.list?.map((item,index) => {
            item.index = index;
            var reason = analysisDict?.data?.find(
              (reason) => reason.id == item.Reason,
            );
            if (reason) {
              item.Reason = reason.content;
            }
            var FixPlanArr = item.FixPlan.split(',');
            if (FixPlanArr.length > 0) {
              item.FixPlan = [];
              FixPlanArr.map((fixItem) => {
                var fixPlan = analysisDict?.data?.find(
                  (reason) => reason.id == fixItem,
                );
                if (fixPlan) item.FixPlan.push(fixPlan);
              });
            }
          });
          yield put({
            type: 'save',
            payload: { list: response.data },
          });
          callback && callback?.(response.data);
        }
      } else {
        const response = yield call(queryList, payload);
        if (response) {
          yield put({
            type: 'save',
            payload: { list: response.data },
          });
        }
        callback && callback?.(response.data);
      }
    },
    *queryProcessSection({ payload }, { call, put }) {
      const list = yield call(queryProcessSection, payload);
      if (list) {
        yield put({
          type: 'save',
          payload: { processList: list.data },
        });
      }
    },
  },

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