import {
  getRouteList,
  queryPatrol,
  queryPatrolRecord,
} from '@/services/eqSelfInspection';
import { queryProcessSection } from '@/services/SmartOps';
export default {
  namespace: 'patrolArtificialRecord',
  state: {
    list: [],
    routeInfoList: [],
    processList: [],
  },
  effects: {
    *queryPatrol({ payload, callback }, { call, put }) {
      let response = yield call(queryPatrol, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { list: response?.data?.list },
        });
        callback?.(response?.data);
      }
    },
    *queryRouteInfoList({ payload }, { call, put }) {
      let response = yield call(getRouteList, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: { routeInfoList: response?.data },
        });
      }
    },
    *queryPatrolRecord({ payload, callback }, { call }) {
      let { data } = yield call(queryPatrolRecord, payload);
      if (data) {
        const creatorName = data.CreatorUser && data.CreatorUser.CName;
        var status = {};
        let Items = [],
          sensor = [];
        const getItems = (item, patrolType) => {
          let key;
          if (patrolType == 1) {
            key = item.PatrolName;
          } else {
            key = item.DeviceCode + '-' + item.DeviceName;
          }
          item.TemplateItem = item.TemplateItem || {};
          item.patrolType = patrolType;
          item.ThresholdEnum = item.TemplateItem.ThresholdEnum;
          item.Type = item.TemplateItem.Type;
          if (!status[key]) status[key] = { normal: 0, error: 0 };
          if (item.Status == 1) {
            status[key].error++;
          } else {
            status[key].normal++;
          }
          return item;
        };
        data.ItemsExtend.forEach((item) => {
          console.log(Items);
          if (item.PatrolCardRecordItemAssocThreshold) {
            var arr = item.PatrolCardRecordItemAssocThreshold.map((i) =>
              getItems(i, 1),
            );
            Items = Items.concat(arr);
          } else {
            Items.push(getItems(item, 0));
          }
        });
        data.ItemsSensor.forEach((item) => {
          if (item.PatrolCardRecordItemAssocThreshold) {
            var arr = item.PatrolCardRecordItemAssocThreshold.map((i) =>
              getItems(i, 1),
            );
            sensor = sensor.concat(arr);
          } else {
            sensor.push(getItems(item, 0));
          }
        });

        data.Items = Items;
        data.sensor = sensor;

        data.Points.forEach((item) => {
          let key;
          if (item.PatrolType == 1) {
            key = item.DeviceName;
          } else {
            key = item.DeviceCode + '-' + item.DeviceName;
          }
          item.creatorName = creatorName;
          item.level = `${item.ExceptionLevel || '-'}/${
            item.DeviceLevel || '-'
          }`;
          item.status = status[key] || { normal: 0, error: 0 };
        });
        callback && callback(data);
      }
      // if (response) {
      //   callback?.(response?.data);
      // }
    },
    *queryProcessSection({ payload }, { call, put }) {
      const res = yield call(queryProcessSection, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: { processList: res.data },
        });
      }
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
