changeAdd.jsx 13.5 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1
import React, { useState, useEffect, useCallback, useRef } from 'react';
皮倩雯's avatar
皮倩雯 committed
2
import { Form, Modal, Space, Divider, Radio, Checkbox, notification } from 'antd';
皮倩雯's avatar
皮倩雯 committed
3 4 5 6 7

import styles from './incident.less';
import Sortable from 'sortablejs';
const CheckboxGroup = Checkbox.Group;
const AddModal = props => {
皮倩雯's avatar
皮倩雯 committed
8
    const { callBackSubmit = () => { }, isType, pickItem, visible, filed, filed1, characterValue, newCheckedList, flag } = props;
皮倩雯's avatar
皮倩雯 committed
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
    const [loading, setLoading] = useState(false);
    const [value, setValue] = useState('');
    const [checkValue, setCheckValue] = useState([]);
    const [form] = Form.useForm();
    const [title, setTitle] = useState([])
    const { Item } = Form;
    const [checkedList, setCheckedList] = useState([]);//选中的复选框内容
    const [indeterminate, setIndeterminate] = useState([]);
    const [checkAll, setCheckAll] = useState([]);
    const [selectData, setSelectData] = useState([])
    let objArr = []
    const onChangeList = (list, index, title) => {
        const checkedListArr = [...checkedList]
        checkedListArr[index] = list
        setCheckedList(checkedListArr);
        const indeterminateArr = [...indeterminate]
        const checkAllArr = [...checkAll]
        indeterminateArr[index] = !!list.length && list.length < filed[title].length
        checkAllArr[index] = list.length === filed[title].length
        setIndeterminate(indeterminateArr)
        setCheckAll(checkAllArr);
    };
    const onChangeList1 = (list, index, title) => {
        const checkedListArr = [...checkedList]
        checkedListArr[index] = list
        setCheckedList(checkedListArr);
        const indeterminateArr = [...indeterminate]
        const checkAllArr = [...checkAll]
        indeterminateArr[index] = !!list.length && list.length < filed1[title].length
        checkAllArr[index] = list.length === filed1[title].length
        setIndeterminate(indeterminateArr)
        setCheckAll(checkAllArr);
    };

    const onSubmit = () => {
皮倩雯's avatar
皮倩雯 committed
44
        isType === 'rule' ? callBackSubmit(`${value === '无' || value === '' ? '' : value + ','}${checkValue.join(',')}`) : callBackSubmit({ checkedList, str: selectData.join(","), pickItem, stt:selectData, title:title, filed22:filed1, isType:isType });
皮倩雯's avatar
皮倩雯 committed
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
    }
    const onCheckAllChange = e => {
        const indeterminateArr = [...indeterminate]
        const checkAllArr = [...checkAll]
        const checkedListArr = [...checkedList]
        checkAllArr[e.target.index] = e.target.checked
        indeterminateArr[e.target.index] = false
        e.target.checked ? checkedListArr[e.target.index] = e.target.checkvalue : checkedListArr[e.target.index] = []
        setCheckedList(checkedListArr);
        setIndeterminate(indeterminateArr);
        setCheckAll(checkAllArr);
    };
    useEffect(() => {
        selectAll()
    }, [checkedList]);

    const selectAll = () => {
        objArr = []
        checkedList.map(item => {
            objArr = objArr.concat(item)
        })
        setSelectData(objArr)
    }

    useEffect(() => {
皮倩雯's avatar
皮倩雯 committed
70 71
        if (isType != '' && isType === 'add') {
            console.log(filed)
皮倩雯's avatar
皮倩雯 committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            let arr = Object.keys(filed)
            console.log(arr, 'arr')
            setTitle(arr)
            let checkArr = []
            let indeterminateArr = []
            let checkAllArr = []
            console.log(newCheckedList);
            arr.map((item, index) => {
                checkArr[index] = []
                newCheckedList.map(checkItem => {
                    if (filed[item].includes(checkItem)) {
                        checkArr[index].push(checkItem)
                    }
                })
                indeterminateArr.push(!!checkArr[index].length && checkArr[index].length < filed[item].length)
                checkAllArr.push(checkArr[index].length === filed[item].length)
            })
            setCheckedList(checkArr)
            setIndeterminate(indeterminateArr)
            setCheckAll(checkAllArr)
            let newArr = characterValue.length ? characterValue.split(",") : []
            setSelectData(newArr)
            draftSort()
皮倩雯's avatar
皮倩雯 committed
95
        } else if (isType === 'app') {
皮倩雯's avatar
皮倩雯 committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            console.log(filed1)
            let arr = Object.keys(filed1)
            console.log(arr, 'arr')
            setTitle(arr)
            let checkArr = []
            let indeterminateArr = []
            let checkAllArr = []
            console.log(newCheckedList);
            arr.map((item, index) => {
                checkArr[index] = []
                newCheckedList.map(checkItem => {
                    if (filed1[item].includes(checkItem)) {
                        checkArr[index].push(checkItem)
                    }
                })
                indeterminateArr.push(!!checkArr[index].length && checkArr[index].length < filed1[item].length)
                checkAllArr.push(checkArr[index].length === filed1[item].length)
            })
            setCheckedList(checkArr)
            setIndeterminate(indeterminateArr)
            setCheckAll(checkAllArr)
            let newArr = characterValue.length ? characterValue.split(",") : []
            setSelectData(newArr)
            draftSort()
        }

    }, [visible]);

    //拖拽初始化及逻辑
    const draftSort = () => {
        let el = document.getElementById('doctor-drag-items');
        if (el) {
            let sortable = Sortable.create(el, {
                animation: 100, //动画参数
                onEnd: function (evt) { //拖拽完毕之后发生,只需关注该事件
                    let arr = [];
                    let len = evt.from.children.length;
                    for (let i = 0; i < len; i++) {
                        arr.push(evt.from.children[i].getAttribute('drag-id'))
                    }
                    setSelectData(arr)
                }
            });
        }

    }
皮倩雯's avatar
皮倩雯 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    if (isType != 'app') {
        return (
            <Modal
                title='字段集选择'
                bodyStyle={{ width: '100%', minHeight: '100px' }}
                style={{ top: '10px' }}
                width="700px"
                destroyOnClose
                centered={true}
                maskClosable={false}
                onOk={onSubmit}
                okText="确认"
                cancelText="取消"
                {...props}
                forceRender={true}
                getContainer={false}
            >
                {visible && (
                    <div className={styles.listCard}>
                        <div className={styles.cardItem} style={{ borderRight: '1px solid #99bbe8' }}>
                            <Divider orientation="left" style={{ margin: '0 0 10px 0', backgroundColor: '#dfe8f6' }}>待选字段列表</Divider>
                            
                            {flag === 0 ?
                                <>
                                <div className={styles.cardContent}>
皮倩雯's avatar
皮倩雯 committed
167
                                    <span style={{color: 'red',fontSize: '1rem', marginLeft: '25px'}}>请先选择事件主表</span>
皮倩雯's avatar
皮倩雯 committed
168 169 170 171 172 173
                                </div>
                                </>:
                                <>
                                {JSON.stringify(filed) == "{}" ?
                                  <>
                                        <div className={styles.cardContent}>
皮倩雯's avatar
皮倩雯 committed
174
                                            <span style={{color: 'red',fontSize: '1rem', marginLeft: '25px'}}>表字段缺失请先配置表/字段</span>
皮倩雯's avatar
皮倩雯 committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                                        </div>
                                  </>
                                  :
                                  <>
                                      <div className={styles.cardContent}>
                                        {title.map((item, index) => {
                                            return <div className={styles.cardItemData} key={index}>
                                                <Divider orientation="left" style={{ margin: '0 0 10px 0', color: '#15428b', borderTopColor: '#99bbe8' }}>{item} <Checkbox indeterminate={indeterminate[index]} onChange={onCheckAllChange} index={index} checkvalue={filed[item]} checked={checkAll[index]}> </Checkbox></Divider>
                                                <CheckboxGroup options={filed[item]} value={checkedList[index]} onChange={(e) => onChangeList(e, index, item)} /></div>
                                        })}
                                    </div>
                                  </>
                            }
                                    
                                </>
                                
                            }

皮倩雯's avatar
皮倩雯 committed
193
                        </div>
皮倩雯's avatar
皮倩雯 committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                        <div className={styles.cardItem}>
                            <Divider orientation="left" style={{ margin: '0 0 10px 0', backgroundColor: '#dfe8f6' }}>已选字段列表</Divider>
                            <div className={styles.cardContent}>
                                <div className={styles.doctorTable}>
                                    <table>
                                        <thead>
                                            <tr>
                                                <td>字段名</td>
                                            </tr>
                                        </thead>
                                        <tbody id='doctor-drag-items'>
                                            {selectData && selectData.length > 0 ?
                                                selectData.map((item, index) => {
                                                    return <tr drag-id={item} key={index} style={{ cursor: 'move' }}>
                                                        <td><span title={item}>{item}</span></td>
                                                    </tr>
                                                })
                                                : <tr><td colSpan='10' style={{ textAlign: 'center' }}>暂无数据</td></tr>
                                            }
                                        </tbody>
                                    </table>
                                </div>
皮倩雯's avatar
皮倩雯 committed
216

皮倩雯's avatar
皮倩雯 committed
217
                            </div>
皮倩雯's avatar
皮倩雯 committed
218 219
                        </div>
                    </div>
皮倩雯's avatar
皮倩雯 committed
220
                )
皮倩雯's avatar
皮倩雯 committed
221

皮倩雯's avatar
皮倩雯 committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                }
            </Modal>
        );
    } else if (isType === 'app') {
        return (
            <Modal
                title= '部门或角色'
                bodyStyle={{ width: '100%', minHeight: '100px' }}
                style={{ top: '10px' }}
                width="700px"
                destroyOnClose
                centered={true}
                maskClosable={false}
                onOk={onSubmit}
                okText="确认"
                cancelText="取消"
                {...props}
                forceRender={true}
                getContainer={false}
            >
                {visible && (
                    <div className={styles.listCard}>
                        <div className={styles.cardItem} style={{ borderRight: '1px solid #99bbe8' }}>
                            <Divider orientation="left" style={{ margin: '0 0 10px 0', backgroundColor: '#dfe8f6' }}>待选字段列表</Divider>
                                    <div className={styles.cardContent}>
                                        {title.map((item, index) => {
                                            return <div className={styles.cardItemData} key={index}>
                                                <Divider orientation="left" style={{ margin: '0 0 10px 0', color: '#15428b', borderTopColor: '#99bbe8' }}>{item} <Checkbox indeterminate={indeterminate[index]} onChange={onCheckAllChange} index={index} checkvalue={filed1[item]} checked={checkAll[index]}> </Checkbox></Divider>
                                                <CheckboxGroup options={filed1[item]} value={checkedList[index]} onChange={(e) => onChangeList1(e, index, item)} /></div>
                                        })}
                                    </div>
皮倩雯's avatar
皮倩雯 committed
253
                        </div>
皮倩雯's avatar
皮倩雯 committed
254 255 256 257 258 259 260 261 262 263
                        <div className={styles.cardItem}>
                            <Divider orientation="left" style={{ margin: '0 0 10px 0', backgroundColor: '#dfe8f6' }}>已选字段列表</Divider>
                            <div className={styles.cardContent}>
                                <div className={styles.doctorTable}>
                                    <table>
                                        <thead>
                                            <tr>
                                                <td>字段名</td>
                                            </tr>
                                        </thead>
皮倩雯's avatar
皮倩雯 committed
264
                                        <tbody id='doctor-drag-items'>
皮倩雯's avatar
皮倩雯 committed
265 266
                                            {selectData && selectData.length > 0 ?
                                                selectData.map((item, index) => {
皮倩雯's avatar
皮倩雯 committed
267
                                                    return <tr drag-id={item} key={index} style={{ cursor: 'move' }}>
皮倩雯's avatar
皮倩雯 committed
268 269 270 271 272 273 274 275
                                                        <td><span title={item}>{item}</span></td>
                                                    </tr>
                                                })
                                                : <tr><td colSpan='10' style={{ textAlign: 'center' }}>暂无数据</td></tr>
                                            }
                                        </tbody>
                                    </table>
                                </div>
皮倩雯's avatar
皮倩雯 committed
276

皮倩雯's avatar
皮倩雯 committed
277
                            </div>
皮倩雯's avatar
皮倩雯 committed
278 279
                        </div>
                    </div>
皮倩雯's avatar
皮倩雯 committed
280
                )
皮倩雯's avatar
皮倩雯 committed
281

皮倩雯's avatar
皮倩雯 committed
282 283 284 285
                }
            </Modal>
        );
    }
皮倩雯's avatar
皮倩雯 committed
286 287
};
export default AddModal;