import * as registerService from '../../services/common/register';
import { message } from 'antd';
import { dataUtils, formUtils } from '@utils';

export default {

  namespace: 'register',

  state: {
    list: [],
    total: 0,
    current: 0,
    mode: 'create',
    filterParams: {},
    formData: {},
    addrContacts: [],
    addrQualifys: [],
    edpPbSupp: {},
    srmSuppAdd: {},
    disagree: true,
    saveDisabled: false,
    items: [],
  },


  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/register') {
          dispatch({ type: 'clean' });
          dispatch({ type: 'query' });
        }
      });
    },
  },


  effects: {

    * save({ payload }, { select, call, put }) {
      const edpPbSupp = yield select(({ register }) => register.edpPbSupp);
      const srmSuppAdd = yield select(({ register }) => register.srmSuppAdd);
      const addrQualifys = yield select(({ register }) => register.addrQualifys);
      const addrContacts = yield select(({ register }) => register.addrContacts);

      yield put({
        type: 'updateState',
        payload: {
          saveDisabled: true,
        },
      });

      for (var qualify of addrQualifys) {
        if (qualify.id < 0) {
          qualify.attachRepo = Math.abs(qualify.id);
          qualify.id = ''
        }
      }

      const newData = {
        addrQualifys: addrQualifys,
        addrContacts: addrContacts,
        edpPbSupp: {
          ...formUtils.standardiseFormData(edpPbSupp),
        },
        srmSuppAdd: {
          ...formUtils.standardiseFormData(srmSuppAdd),
          ...formUtils.standardiseFormData(payload),
        },
        submitted: false,
      };

      const data = yield call(registerService.create, newData);
      if (data.status === 200) {
        yield put({
          type: 'updateState',
          payload: {
            saveDisabled: false,
          },
        });
        if (data.success) {
          yield put({ type: 'addCurrent' });
        }else{
          throw data;
        }
      } else {
        throw data;
      }
    },

    * query({ payload }, { call, select, put }) {
      const data = yield call(registerService.getItem, { ...payload });
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            items: data.data,
          },
        });
      } else {
        throw data;
      }
    },

    * qualify({ }, { call, put, select }) {
      const edpPbSupp = yield select(({ register }) => register.edpPbSupp);
      const addrQualifys = yield select(({ register }) => register.addrQualifys);
      const gysxzList = edpPbSupp.gysxz;
      const data = yield call(registerService.requireQualifys, { gysxzList });
      if (data.success) {
        for (let i in data.data) {
          let hadAddrQualifys = false;
          for (let key of addrQualifys) {
            if (data.data[i] == key.qualifyType) {
              hadAddrQualifys = true;
            }
          }
          if (!hadAddrQualifys) {
            let newData = {
              id: dataUtils.getRandomInt(-99999999, -1),
              qualifyType: data.data[i]
            };
            yield put({
              type: 'addRow',
              payload: { ...newData },
            });
          }
        };
      } else {
        throw data;
      }
    },

    * requireQualifys({ }, { call, put, select }) {
      const edpPbSupp = yield select(({ register }) => register.edpPbSupp);
      const addrQualifys = yield select(({ register }) => register.addrQualifys);
      const gysxzList = edpPbSupp.gysxz;
      const data = yield call(registerService.requireQualifys, { gysxzList });
      if (data.success) {
        for (let i in data.data) {
          let hadAddrQualifys = false;
          for (let key of addrQualifys) {
            if (data.data[i] == key.qualifyType) {
              hadAddrQualifys = true;
            }
          }
          if (!hadAddrQualifys) {
            let newData = {
              id: dataUtils.getRandomInt(-99999999, -1),
              qualifyType: data.data[i]
            };
            yield put({
              type: 'addRow',
              payload: { ...newData },
            });
            message.error('缺少资质类型');
            return;
          }
        };
        yield put({
          type: 'addCurrent',
        });
      } else {
        throw data;
      }
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          current: 0,
          mode: 'create',
          disagree: true,
          filterParams: {},
          addrContacts: [],
          addrQualifys: [],
          edpPbSupp: {},
          srmSuppAdd: {},
        },
      });
    },
  },

  reducers: {

    addRow(state, { payload }) {
      const { addrQualifys } = state;
      const newList = addrQualifys.slice();
      // newList.unshift(payload)
      newList.push(payload);
      return {
        ...state,
        addrQualifys: newList,
      };
    },

    deleteRow(state, { payload }) {
      let { addrQualifys } = state;
      const newList = addrQualifys.slice().filter(item => item.id !== payload);
      return {
        ...state,
        addrQualifys: newList,
      };
    },

    updateCell(state, { payload }) {
      const { addrQualifys } = state;
      const newList = addrQualifys.slice();
      const { key, index, value } = payload;
      newList[index][key] = value;
      if (key === 'relateItem') {
        newList[index]['relateToItemId'] = value.id;
        newList[index]['itemCode'] = value.code;
        newList[index]['itemName'] = value.name;
      }
      return {
        ...state,
        addrQualifys: newList,
      };
    },

    addAddrRow(state, { payload }) {
      const { addrContacts } = state;
      const newAddrContacts = addrContacts.slice();
      // newList.unshift(payload)
      newAddrContacts.push(payload);
      return {
        ...state,
        addrContacts: newAddrContacts,
      };
    },

    deleteAddrRow(state, { payload }) {
      let { addrContacts } = state;
      const newAddrContacts = addrContacts.slice().filter(item => item.id !== payload);
      return {
        ...state,
        addrContacts: newAddrContacts,
      };
    },

    updateAddrCell(state, { payload }) {
      const { addrContacts } = state;
      const newAddrContacts = addrContacts.slice();
      const { key, index, value } = payload;
      newAddrContacts[index][key] = value;
      return {
        ...state,
        addrContacts: newAddrContacts,
      };
    },

    addCurrent(state, { payload }) {
      const { current } = state;
      const newCurrent = current + 1;
      return {
        ...state,
        current: newCurrent,
      };
    },

    minusCurrent(state, { payload }) {
      const { current } = state;
      const newCurrent = current - 1;
      return {
        ...state,
        current: newCurrent,
      };
    },

    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    updateForm(state, { payload }) {
      const { key, value } = payload;
      const edpPbSupp = state.edpPbSupp;
      const newEdpPbSupps = Object.assign({}, edpPbSupp);
      newEdpPbSupps[key] = value;
      return {
        ...state,
        edpPbSupp: newEdpPbSupps,
      };
    },

    updateAddForm(state, { payload }) {
      const { key, value } = payload;
      const srmSuppAdd = state.srmSuppAdd;
      const newSrmSuppAdd = Object.assign({}, srmSuppAdd);
      newSrmSuppAdd[key] = value;
      return {
        ...state,
        srmSuppAdd: newSrmSuppAdd,
      };
    },

  },

};
