import { routerRedux } from 'dva/router'
import * as stockInOrderService from '../../../services/cooperate/order/stockInOrder'

import { dataUtils, stringUtils } from '@utils'

const initTime = '00:00:00'
const endTime = '23:59:59'

export default {

  namespace: 'stockInOrderForm',

  state: {
    list: [],
    total: 1,
    diffData: false,
    editable: false,
  },

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

  effects: {
    * query({ payload }, { call, put, select }) {
      // payload && payload.suppId && (payload.suppId = payload.suppId.id)
      // payload && payload.recpOuId && (payload.recpOuId = payload.recpOuId.id)
      // if (payload.recpDateFrom) {
      //   payload.recpDateFrom = payload.recpDateFrom + 'T' + initTime
      //   payload.recpDateTo = payload.recpDateTo + 'T' + endTime
      // }
      yield put({ type: 'clean' });
      let { editable } = yield select(({ stockInOrderForm }) => stockInOrderForm);
      const perms = yield select(({ app }) => app.perms);
      // 角色控制 供应商 可以录入差异
      if (perms) {
        for (const p of perms) {
          if (p.menuId === 'cp_order_sio_view') {
            editable = true;
          }
        }
      }
      const data = yield call(stockInOrderService.query, payload)
      if (data.success) {

        const rows = data.data.rows
        rows.map(row => {
          row.recpDate = dataUtils.ymd(row.recpDate)
        })

        yield put({
          type: 'updateState',
          payload: {
            list: data.data.rows,
            total: data.data.total,
            editable,
          },
        })

        yield put(routerRedux.replace({
          pathname: location.pathname,
          search: '?' + stringUtils.JSON2QueryString({ ...payload, reqDate: null }),
        }))
      } else {
        throw data
      }
    },

    * saveDetail({ payload }, { select, call, put }) {
      const { list } = yield select(({ stockInOrderForm }) => stockInOrderForm);
      const newList = list.slice();
      const { index, key, pIndex, value } = payload;
      if (key === 'deliQty') {
        newList[pIndex].details[index].deliQty = value ? value : 0;
        newList[pIndex].details[index].diffQty = parseFloat((newList[pIndex].details[index].deliQty - newList[pIndex].details[index].qty).toFixed(10));
        newList[pIndex].details[index].deliAmt = parseFloat((newList[pIndex].details[index].deliQty * newList[pIndex].details[index].deliPrice).toFixed(10));
        newList[pIndex].details[index].differenceAmt = parseFloat((newList[pIndex].details[index].deliAmt - newList[pIndex].details[index].amt).toFixed(10));
      }
      if (key === 'deliPrice') {
        newList[pIndex].details[index].deliPrice = value ? value : 0;
        newList[pIndex].details[index].diffPrice = parseFloat((newList[pIndex].details[index].deliPrice - newList[pIndex].details[index].price).toFixed(10));
        newList[pIndex].details[index].deliAmt = parseFloat((newList[pIndex].details[index].deliQty * newList[pIndex].details[index].deliPrice).toFixed(10));
        newList[pIndex].details[index].differenceAmt = parseFloat((newList[pIndex].details[index].deliAmt - newList[pIndex].details[index].amt).toFixed(10));
      }
      const data = yield call(stockInOrderService.update, newList[pIndex].details[index]);
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: newList,
          },
        })
      } else {
        throw data;
      }
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          list: [],
          total: 1,
          diffData: false,
          editable: false,
        },
      })
    },
  },

  reducers: {
    showDiffData(state, { payload }) {
      const { diffData } = state;
      return { ...state, diffData: !diffData };
    },

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