index.jsx 6.86 KB
Newer Older
TZW's avatar
TZW committed
1 2 3 4 5 6
/* 组织管理
 * @Author: Li Hanlin
 * @Date: 2022-11-24 11:16:02
 * @Last Modified by: Li Hanlin
 * @Last Modified time: 2022-11-24 14:44:11
 */
wuhao's avatar
wuhao committed
7 8 9 10 11 12
import * as React from 'react';
import { useState, useMemo, useRef } from 'react';
import DrawerPro from '@/components/DrawerPro';
import AutoTable from '@/components/AutoTable';
import PremButton from '@/components/PremButton';
import getcolumns from './columns';
TZW's avatar
TZW committed
13 14
import { doFetch } from '@/utils/doFetch';
import { message } from 'antd';
wuhao's avatar
wuhao committed
15

TZW's avatar
TZW committed
16
function Type(props) {
wuhao's avatar
wuhao committed
17 18
  const actionRef = useRef(),
    formRef = useRef();
TZW's avatar
TZW committed
19
  const [drawer, setDrawer] = useState({
wuhao's avatar
wuhao committed
20 21 22
    visible: false,
  });

TZW's avatar
TZW committed
23 24 25 26 27 28 29
  const urlParams = {
    save: '/auth/sysDepartment/save',
    remove: '/auth/sysDepartment/deleteById',
    list: '/auth/sysDepartment/query/tree',
    detail: '/asset/equipmentType/queryById',
  };

wuhao's avatar
wuhao committed
30 31 32 33 34 35 36
  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
TZW's avatar
TZW committed
37
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
              ...s,
              visible: true,
              item: row,
              title: '详情',
              val: 'detail',
              title: row.userName + '的详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
TZW's avatar
TZW committed
59
            setDrawer((s) => ({
wuhao's avatar
wuhao committed
60 61
              ...s,
              visible: true,
TZW's avatar
TZW committed
62 63 64 65 66 67
              // detailpath: urlParams.detail,
              // params: { id: row.key },
              item: {
                ...row,
                departmentName: row.title,
              },
wuhao's avatar
wuhao committed
68 69
              title: '编辑',
              val: 'edit',
TZW's avatar
TZW committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
              onFinish: async (vals) => {
                console.log(vals);
                let params = {
                  ...vals,
                  id: row.key,
                  parentId: row.parentKey,
                  departmentType: row.departmentType,
                };
                console.log(params);
                delete params.title;
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
                  message.success('编辑成功!');
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
wuhao's avatar
wuhao committed
93 94 95 96 97 98 99 100 101
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

TZW's avatar
TZW committed
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 142 143 144 145 146 147 148 149
  const addChild = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
            setDrawer((s) => ({
              ...s,
              visible: true,
              detailpath: null,

              item: {
                parentKey: row.title,
                departmentTypeName: '部门类型',
              },
              title: '新增子级',
              val: 'addChild',
              onFinish: async (vals) => {
                let params = {
                  ...vals,
                  parentId: row.key,
                  equipmentTypeName: vals.title,
                  departmentType: '1',
                };
                delete params.title;
                delete params.parentKey;
                let res = await doFetch({
                  url: urlParams.save,
                  params,
                });
                if (res.code === '0000') {
                  message.success('新增子级成功!');
                  setDrawer((s) => ({
                    ...s,
                    visible: false,
                  }));
                  actionRef.current.reload();
                }
              },
            }));
          },
        }}
      >
        新增子级
      </PremButton>
    );
  };

wuhao's avatar
wuhao committed
150 151 152 153
  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
TZW's avatar
TZW committed
154
          title: '是否删除该设备类型?',
wuhao's avatar
wuhao committed
155 156
          okText: '确认',
          cancelText: '取消',
TZW's avatar
TZW committed
157 158 159 160 161 162 163 164 165 166
          onConfirm: async () => {
            let res = await doFetch({ url: urlParams.remove, params: { id: row.key } });
            if (res.code === '0000') {
              message.success('删除成功!');
              setDrawer((s) => ({
                ...s,
                visible: false,
              }));
              actionRef.current.reload();
            }
wuhao's avatar
wuhao committed
167 168 169 170 171 172 173 174 175 176 177 178 179
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
TZW's avatar
TZW committed
180 181 182 183 184 185
    let defcolumn = getcolumns(setDrawer);
    if (drawer?.val == 'add' || drawer?.val == 'edit') {
      defcolumn[3].hideInForm = true;
    } else {
      defcolumn[3].hideInForm = false;
    }
wuhao's avatar
wuhao committed
186 187 188 189 190
    return defcolumn.concat({
      title: '操作',
      valueType: 'option',
      width: 150,
      render: (text, row, _, action) => [
TZW's avatar
TZW committed
191
        addChild(text, row, _, action),
wuhao's avatar
wuhao committed
192 193 194 195
        edit(text, row, _, action),
        remove(text, row, _, action),
      ],
    });
TZW's avatar
TZW committed
196
  }, [drawer.val]);
wuhao's avatar
wuhao committed
197 198 199 200

  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
TZW's avatar
TZW committed
201
        pagetitle={<h3 className="page-title">组织管理</h3>}
wuhao's avatar
wuhao committed
202
        columns={columns}
TZW's avatar
TZW committed
203
        path={urlParams.list}
wuhao's avatar
wuhao committed
204 205
        actionRef={actionRef}
        pageextra={'add'}
TZW's avatar
TZW committed
206
        resizeable={false}
wuhao's avatar
wuhao committed
207 208 209 210
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
            disabled: false,
TZW's avatar
TZW committed
211
            type: 'primary',
wuhao's avatar
wuhao committed
212
            onClick: () => {
TZW's avatar
TZW committed
213
              setDrawer((s) => ({
wuhao's avatar
wuhao committed
214 215
                ...s,
                visible: true,
TZW's avatar
TZW committed
216 217 218
                item: {
                  departmentTypeName: '公司类型',
                },
wuhao's avatar
wuhao committed
219
                title: '新增',
TZW's avatar
TZW committed
220
                detailpath: null,
wuhao's avatar
wuhao committed
221
                val: 'add',
TZW's avatar
TZW committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                onFinish: async (vals) => {
                  let params = {
                    ...vals,
                    departmentType: 0,
                  };
                  let res = await doFetch({
                    url: urlParams.save,
                    params,
                  });
                  if (res.code === '0000') {
                    message.success('新增成功!');
                    setDrawer((s) => ({
                      ...s,
                      visible: false,
                    }));
                    actionRef.current.reload();
                  }
                },
wuhao's avatar
wuhao committed
240 241 242 243
              }));
            },
          },
        }}
TZW's avatar
TZW committed
244
        pagination={false}
wuhao's avatar
wuhao committed
245 246 247 248
      />

      <DrawerPro
        fields={columns}
TZW's avatar
TZW committed
249
        defaultFormValue={drawer?.item ?? {}}
wuhao's avatar
wuhao committed
250 251 252
        formRef={formRef}
        placement="right"
        onClose={() => {
TZW's avatar
TZW committed
253
          setDrawer((s) => ({
wuhao's avatar
wuhao committed
254 255 256 257 258 259 260 261 262 263
            ...s,
            visible: false,
          }));
        }}
        {...drawer}
      />
    </div>
  );
}

TZW's avatar
TZW committed
264
export default Type;