king
2021-09-01 31ec63f0419895876cbaba99637a884a32d33d0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react'
import { useDrag, useDrop } from 'react-dnd'
import { Icon, Select, DatePicker, Input, Popover, Form } from 'antd'
import moment from 'moment'
 
import asyncComponent from '@/utils/asyncComponent'
import DateGroup from '../dategroup'
import './index.scss'
 
const { MonthPicker, WeekPicker, RangePicker } = DatePicker
const { Search } = Input
const CheckCard = asyncComponent(() => import('@/templates/modalconfig/checkCard'))
const appType = sessionStorage.getItem('appType')
 
const Card = ({ id, card, showField, moveCard, copyCard, findCard, editCard, delCard }) => {
  const originalIndex = findCard(id).index
  const [{ isDragging }, drag] = useDrag({
    item: { type: 'search', id, originalIndex },
    collect: monitor => ({
      isDragging: monitor.isDragging(),
    }),
  })
  const [, drop] = useDrop({
    accept: 'search',
    canDrop: () => true,
    drop: ({ id: draggedId }) => {
      if (!draggedId || draggedId === id) return
      const { index: originIndex } = findCard(draggedId)
      if (originIndex === -1) return
 
      const { index: overIndex } = findCard(id)
      moveCard(draggedId, overIndex)
    },
  })
  const opacity = isDragging ? 0 : 1
 
  let _defaultValue = '' // 下拉搜索、时间范围类型,初始值需要预处理
 
  if (card.type === 'multiselect' || card.type === 'select' || card.type === 'link') {
    if (card.initval) {
      let _option = card.options.filter(option => option.Value === card.initval)[0]
      if (_option) {
        _defaultValue = _option.Text || ''
      } else {
        _defaultValue = ''
      }
    } else if (card.setAll === 'true') {
      _defaultValue = 'All'
    }
  } else if (card.type === 'daterange') {
    _defaultValue = [null, null]
    if (card.initval) {
      try {
        let _initval = JSON.parse(card.initval)
        _defaultValue = [moment().subtract(_initval[0], 'days'), moment().subtract(_initval[1], 'days')]
      } catch (e) {
        _defaultValue = [null, null]
      }
    }
  }
 
  let formItem = null
  if (card.type === 'text') {
    if (card.inputType === 'search') {
      formItem = (<Search style={{marginTop: '4px'}} placeholder={card.labelShow === 'false' ? card.label : ''} value={card.initval} enterButton />)
    } else {
      formItem = (<Input style={{marginTop: '4px'}} placeholder={card.labelShow === 'false' ? card.label : ''} value={card.initval} />)
    }
  } else if (card.type === 'multiselect' || card.type === 'select' || card.type === 'link') {
    formItem = (<Select value={_defaultValue}></Select>)
  } else if (card.type === 'date' && appType === 'mob') {
    formItem = (<div className="mob-list-item">{card.initval ? moment().subtract(card.initval, 'days').format('YYYY-MM-DD') : '请选择'}<Icon type="right" /></div>)
  } else if (card.type === 'datemonth' && appType === 'mob') {
    formItem = (<div className="mob-list-item">{card.initval ? moment().subtract(card.initval, 'month').format('YYYY-MM') : '请选择'}<Icon type="right" /></div>)
  } else if (card.type === 'date') {
    formItem = (<Input style={{marginTop: '4px'}} placeholder={card.labelShow === 'false' ? card.label : ''} value={card.initval} />)
  } else if (card.type === 'dateweek') {
    formItem = (<WeekPicker value={card.initval ? moment().subtract(card.initval * 7, 'days') : null} />)
  } else if (card.type === 'datemonth') {
    formItem = (<MonthPicker value={card.initval ? moment().subtract(card.initval, 'month') : null} />)
  } else if (card.type === 'daterange') {
    formItem = (<RangePicker
      className="data-range"
      placeholder={['BeginTime', 'EndTime']}
      renderExtraFooter={() => 'extra footer'}
      value={_defaultValue}
    />)
  } else if (card.type === 'group') {
    formItem = (<DateGroup card={card} />)
  } else if (card.type === 'checkcard') {
    formItem = <CheckCard config={card} />
  }
 
  return (
    <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
      <div className="mk-popover-control">
        <Icon className="edit" title="edit" type="edit" onClick={() => editCard(id)} />
        <Icon className="copy" title="copy" type="copy" onClick={() => copyCard(id)} />
        <Icon className="close" title="delete" type="close" onClick={() => delCard(id)} />
      </div>
    } trigger="hover">
      <div className={'page-card ' + card.labelShow + ' ' + card.type} style={{ opacity: opacity}}>
        <div ref={node => drag(drop(node))}>
          <Form.Item
            labelCol={{xs: { span: 24 }, sm: { span: 8 }}}
            wrapperCol = {{xs: { span: 24 }, sm: { span: 16 }}}
            label={card.labelShow !== 'false' ? card.label : ''}
            required={card.required === 'true'}
            help={showField ? card.field + (card.datefield ? ', ' + card.datefield : '') : ''}
          >
            {formItem}
          </Form.Item>
        </div>
      </div>
    </Popover>
  )
}
export default Card