text: /* global window */
  import modelExtend from 'dva-model-extend'
  import { pathMatchRegexp } from 'utils'
  import {
    queryMediaList,
    createMedia,
    removeMedia,
    updateMedia,
    removeMediaList,
  } from 'api'
  import { pageModel } from 'utils/model'
  
  export default modelExtend(pageModel, {
    namespace: 'media',
  
    state: {
      currentItem: {},
      modalVisible: false,
      modalType: 'create',
      selectedRowKeys: [],
    },
  
    subscriptions: {
      setup({ dispatch, history }) {
        history.listen(location => {
          if (pathMatchRegexp('/media', location.pathname)) {
            const payload = location.query || { page: 1, pageSize: 10 }
            dispatch({
              type: 'query',
              payload,
            })
          }
        })
      },
    },
  
    effects: {
      *query({ payload = {} }, { call, put }) {
        const data = yield call(queryMediaList, payload)
        if (data.code === '0000') {
          yield put({
            type: 'querySuccess',
            payload: {
              list: data.result.list,
              pagination: {
                current: Number(payload.page) || 1,
                pageSize: Number(payload.pageSize) || 10,
                total: data.result.total,
              },
            },
          })
        } else {
          throw data
        }
      },
  
      *delete({ payload }, { call, put, select }) {
        const data = yield call(removeMedia, { _id: payload })
        const { selectedRowKeys } = yield select(_ => _.Media)
        if (data.code === '0000') {
          yield put({
            type: 'updateState',
            payload: {
              selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload),
            },
          })
        } else {
          throw data
        }
      },
  
      *multiDelete({ payload }, { call, put }) {
        const data = yield call(removeMediaList, payload)
        if (data.code === '0000') {
          yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        } else {
          throw data
        }
      },
  
      *create({ payload }, { call, put }) {
        const data = yield call(createMedia, payload)
        if (data.code === '0000') {
          yield put({ type: 'hideModal' })
        } else {
          throw data
        }
      },
  
      *update({ payload }, { select, call, put }) {
        const _id = yield select(({ media }) => media.currentItem._id)
        const newMedia = { ...payload, _id }
        const data = yield call(updateMedia, newMedia)
        if (data.code === '0000') {
          yield put({ type: 'hideModal' })
        } else {
          throw data
        }
      },
    },
  
    reducers: {
      showModal(state, { payload }) {
        return { ...state, ...payload, modalVisible: true }
      },
  
      hideModal(state) {
        return { ...state, modalVisible: false }
      },
    },
  })
  