index.jsx 7.2 KB
Newer Older
wuhao's avatar
wuhao committed
1
/* eslint-disable react-hooks/exhaustive-deps */
wuhao's avatar
wuhao committed
2 3 4 5 6 7 8 9 10 11
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';
import { useRequest } from 'ahooks';
import { doFetch } from '@/utils/doFetch';
import InitForm from '@/components/InitForm';
import { message, Divider } from 'antd';
wuhao's avatar
wuhao committed
12
import { useModel } from '@umijs/max';
wuhao's avatar
wuhao committed
13 14

function Requisition(props) {
TZW's avatar
TZW committed
15
  let actionRef = useRef(),
wuhao's avatar
wuhao committed
16 17 18 19 20
    formRef = useRef();
  const [drawer, setdrawer] = useState({
      open: false,
    }),
    [activeTabKey, setactiveTabKey] = useState('1');
wuhao's avatar
wuhao committed
21 22 23
  const {
    initialState: { currentUser },
  } = useModel('@@initialState');
wuhao's avatar
wuhao committed
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 119 120 121 122 123

  const { run, loading } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, params) => {
      if (res?.code == '0000') {
        message.success('操作成功');
        actionRef?.current?.reload();
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
      }
    },
  });

  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
            setdrawer((s) => ({
              ...s,
              open: true,
              item: row,
              title: '详情',
              val: 'detail',
              title: '详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          disabled: row.status == 2,
          onClick: () => {
            setdrawer((s) => ({
              ...s,
              open: true,
              item: row,
              title: '审批',
              val: 'detailaddon',
              addon: (
                <>
                  <InitForm
                    style={{ background: '#f0f0f0', padding: 12, borderTop: '#1890ff solid 1px' }}
                    fields={[
                      {
                        title: '审批结果',
                        dataIndex: 'approvalResult',
                        key: 'approvalResult',
                        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
                        valueType: 'radio',
                        options: [
                          { label: '通过', value: '1' },
                          { label: '不通过', value: '2' },
                        ],
                      },
                      {
                        title: '审批备注',
                        dataIndex: 'approvalRemark',
                        key: 'approvalRemark',
                        valueType: 'textarea',
                        colProps: { span: 24 },
                      },
                    ]}
                    onFinish={(vals) => {
                      run({
                        url: '/sparepart/spareApplyTask/approval',
                        params: { ...vals, id: row?.id },
                      });
                    }}
                  />
                </>
              ),
            }));
          },
        }}
      >
        审批
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
          title: '是否删除?',
          okText: '确认',
          cancelText: '取消',
wuhao's avatar
wuhao committed
124
          disabled: row.status !== 1,
wuhao's avatar
wuhao committed
125 126 127 128 129 130 131
          onConfirm: () => {
            run({ url: pathconfig?.delete || '/delete', params: { id: row?.id } });
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
wuhao's avatar
wuhao committed
132
          disabled: row.status !== 1,
wuhao's avatar
wuhao committed
133 134 135 136 137 138 139 140
        }}
      >
        删除
      </PremButton>
    );
  };

  const columns = useMemo(() => {
TZW's avatar
TZW committed
141
    let defcolumn = getcolumns(setdrawer,drawer?.type, currentUser?.fullName).filter(
wuhao's avatar
wuhao committed
142 143
      (it) => it.key == activeTabKey,
    )[0]?.columns;
wuhao's avatar
wuhao committed
144
    let defpath =
TZW's avatar
TZW committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      getcolumns(setdrawer,drawer?.type).filter((it) => it.key == activeTabKey)[0]?.pathconfig ?? {};
    if (activeTabKey == 1) {
      return defcolumn.concat({
        title: '操作',
        valueType: 'option',
        width: 150,
        render: (text, row, _, action) => [
          // defpath?.enabledetail && detail(text, row, _, action),
          defpath?.enableedit && edit(text, row, _, action),
          defpath?.enabledelete && remove(text, row, _, action),
        ],
      });
    } else {
      return defcolumn;
    }
wuhao's avatar
wuhao committed
160 161 162 163 164 165 166 167 168 169
  }, [activeTabKey, drawer?.type]);

  const pathconfig = useMemo(() => {
    let defpath = getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]?.pathconfig ?? {};
    return defpath;
  }, [activeTabKey]);

  return (
    <div style={{ position: 'relative' }}>
      <AutoTable
TZW's avatar
1  
TZW committed
170
        pagetitle={<h3 className="page-title">备件领用</h3>}
wuhao's avatar
wuhao committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        columns={columns}
        path={pathconfig?.list || '/ngic-auth/sysUser/query/page'}
        actionRef={actionRef}
        pageextra={pathconfig?.enableadd ? 'add' : null}
        resizeable={false}
        addconfig={{
          // access: 'sysDepartment_save',
          btn: {
            disabled: false,
            onClick: async () => {
              let res = await doFetch({
                url: '/base/pmBaseBusinessData/querySpareStockType',
                params: {},
              });
              let type = res?.data?.data?.type;
              setdrawer((s) => ({
                ...s,
                open: true,
                item: null,
                title: '新增',
                val: 'add',
                type,
              }));
            },
          },
        }}
        tabList={getcolumns()}
        activeTabKey={activeTabKey}
        onTabChange={(key) => {
          setactiveTabKey(key);
        }}
      />

      <DrawerPro
wuhao's avatar
wuhao committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        fields={columns?.filter((it) => {
          if (drawer?.item?.status == 1) {
            return (
              [
                'cdetails',
                'approvalUserName',
                'approvalTime',
                'approvalResultName',
                'approvalRemark',
              ].indexOf(it?.dataIndex) == -1
            );
          } else {
            return true;
          }
        })}
wuhao's avatar
wuhao committed
220 221 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 253 254 255
        detailpath={pathconfig?.detail || null}
        defaultFormValue={drawer?.item}
        params={{ id: drawer?.item?.id }}
        formRef={formRef}
        placement="right"
        onClose={() => {
          setdrawer((s) => ({
            ...s,
            open: false,
          }));
        }}
        {...drawer}
        onFinish={(vals) => {
          const detailsList = vals?.detailsList?.map?.((it, i) => {
            return {
              spareStockId: it?.id,
              operateNum: it?.operateNum,
            };
          });
          if (drawer?.val == 'add') {
            run({ url: pathconfig?.add || '/add', params: { ...vals, detailsList } });
          } else if (drawer?.val == 'edit') {
            run({
              url: pathconfig?.edit || '/edit',
              params: { ...vals, id: drawer?.item?.id, detailsList },
            });
          }
        }}
      >
        {drawer?.addon}
      </DrawerPro>
    </div>
  );
}

export default Requisition;