import { ReduxAction, ReduxSagaEffects } from '@interfaces';
import * as tenderService from '../../../services/source/tenders';
import { routerRedux } from 'dva/router';
import pathToRegexp from 'path-to-regexp';
import {  enums } from '@utils';
import { message } from 'antd';
import moment from 'moment';

const dateFormat = 'YYYY-MM-DD HH:mm:ss';

const { bidStatus,resultCode } = enums;
export default {

  namespace: 'tenderResult',

  state: {
    formData:{},
    list: [],
    total: 0,
    currentItem: {},
    modalVisible: false,
    mode: 'view',
    sData:{
      businessName:'商务部分（50分）',
      registeredCapitalName:'注册资本（5）',
      enterpriseQualificationName:'企业资质及等级（5）',
      enterpriseReputationName:'企业荣誉及知名度（5）',
      enterpriseSalesName:'企业销售业绩（5）',
      quotationName:'报价得分（30）',
      technicalName:'技术部分（50分）',
      qualityContentName:'质量内容（40）',
      saleServiceName:'售后服务（10）'
    },
    sList: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const match = pathToRegexp('/source-mgmt/bid-mgmt/tender-result/:id').exec(location.pathname);
        // console.log('match==>',match);
        if (match) {
          dispatch({ type: 'query', payload: match[1]  });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      const data = yield call(tenderService.review, payload);
      if (data.success) {
        const itemData = data.data.items;
        itemData.forEach(i => {
          i.item = `${i.itemDesc}(${i.itemCode})`;
          i.supp = `${i.suppName}(${i.suppCode})`;
          i.rank = `${i.ranking}(${i.score})`;
          i.price = `${i.netPrice}/${i.taxPrice}`;
          i.showAmt = `${i.netAmt}/${i.amt}`;
        });
        yield put({
          type: 'updateState',
          payload: {
            formData:data.data,
            list: itemData,
          },
        });
      } else {
        throw data;
      }
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 0,
          currentItem: {},
          modalVisible: false,
          mode: 'create',
        },
      });
    },

    * cancel({ payload }, { put }) {
      yield put(routerRedux.goBack());
    },
  },

  reducers: {

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

  },
}
