index.tsx 3.78 KB
import React, {useState} from "react";
import {Divider, InputNumber, message, Popconfirm, Table} from 'antd';
import { useStore } from '@/pages/pms/partPlan/AmountPlan';
import lodash from 'lodash';
import useInitial from "@/hooks/useInitail";
import {getList, ListVO, Params, savePlanApi} from "@/pages/pms/partPlan/AmountPlan/api";
import StepBnt from "@/pages/pms/comonents/StepBnt";

const { Column } = Table;
export default function Index() {
  const { setCurrent, dfParams } = useStore();
  const [bntLoading, setBntLoading] = useState(false);
  const { data: parts, setData: setParts } = useInitial<ListVO[], Params>(getList, [], dfParams);
  const [poolId, setPoolId] = useState<any>();

  const changeCnt = lodash.debounce(count => setParts(parts.map((it: ListVO) => (it.poolId == poolId ? ({...it, cnt: count}) : it))), 800);
  const changePrice = lodash.debounce(price => setParts(parts.map((it: ListVO) => (it.poolId == poolId ? ({...it, price}) : it))), 800);

  function prev() {
    setCurrent(2);
  }
  function submit() {
    setBntLoading(true);
    savePlanApi({
      ...dfParams,
      // @ts-ignore
      storageIds: (dfParams.storageIds|| '').split(','),
      parts: parts.map(it => ({...it, partId: it.id}))
    }).then(() => {
      setCurrent(1);
      setBntLoading(false);
    }).catch((e: any) => {
      message.error(e.message);
      setBntLoading(false);
    });
  }
  return (
    <div>
      <Table rowKey={(v: ListVO) => `${v.poolId}`} scroll={{y: 500, x: 2000}} dataSource={parts || []} pagination={false}>
        <Column title="配件编码" dataIndex="code" fixed="left" />
        <Column title="配件名称" dataIndex="name" />
        <Column title="库房名称" dataIndex="storageName" />
        <Column title="门店名称" dataIndex="shopName" />
        <Column title="在库数量" dataIndex="stockCnt" />
        <Column title="滚动90天月均出库数" dataIndex="outCnt" />
        <Column
          title="采购单价"
          dataIndex="price"
          fixed="right"
          width={120}
          render={(t: any, _: ListVO) => (_.poolId != poolId ? (_.price || 0) : (
            <InputNumber
              value={_.price}
              onChange={changePrice}
              style={{ width: '100%' }}
              min={0}
            />
          ))}
        />
        <Column
          title="采购数量"
          dataIndex="cnt"
          fixed="right"
          width={120}
          render={(t, _: ListVO) => (_.poolId != poolId ? (_.cnt || 0) : (
            <InputNumber
              value={_.cnt}
              onChange={changeCnt}
              style={{ width: '100%' }}
              min={1}
            />
          ))}
        />
        <Column
          title="操作"
          fixed="right"
          render={(t, _: ListVO) => (
            <>
              {_.poolId != poolId ? (
                <a onClick={() => setPoolId(_.poolId)}>编辑</a>
              ) : (
                <a onClick={() => setPoolId(null)}>保存</a>
              )}
              <Divider type="vertical" />
              <Popconfirm
                title="是否删除?"
                onConfirm={() => setParts(parts.filter((it: ListVO) => it.poolId != _.poolId))}
                okText="确定"
                cancelText="取消"
              >
                <a onClick={(e) => { e.preventDefault(); }} style={{ color: 'red' }}>删除</a>
              </Popconfirm>
            </>
          )}
        />
      </Table>
      <div style={{margin: 10, display: 'flex', justifyContent: 'flex-end'}}>
        <span style={{fontSize: 16}}>总金额:{(parts.reduce((sum, i) => (sum + ((i.price || 0) * (i.cnt || 0))), 0)).toFixed(2)} 元</span>
      </div>
      <StepBnt
        disable={false}
        bntLoading={bntLoading}
        prev={prev}
        submit={submit}
      />
    </div>
  );
}