import { addStoreLog } from '@/services/stock';
import { ProCard } from '@ant-design/pro-components';
import { message } from 'antd';
import { useState } from 'react';
import OutAndInForm from './components/outAndInForm';

const OutAndIn = (props) => {
  const { data, onCancel, onSuccess } = props;

  const [tabKey, setTabKey] = useState('in');

  const onFinish = async (values) => {
    console.log(data, values);

    if (data.id) {
      addStoreLog({
        storeId: Number(data.id),
        amount: tabKey === 'out' ? -values.amount : values.amount,
        optType: tabKey,
      }).then((res) => {
        message.success('操作成功');

        onSuccess();
      });
    }
  };

  return (
    <ProCard
      tabs={{
        // tabPosition,
        centered: true,
        activeKey: tabKey,
        destroyInactiveTabPane: false,
        items: [
          {
            label: `入库`,
            key: 'in',
            children: (
              <OutAndInForm data={data} type={tabKey} onFinish={onFinish} onCancel={onCancel} />
            ),
          },
          {
            label: `出库`,
            key: 'out',
            children: (
              <OutAndInForm data={data} type={tabKey} onFinish={onFinish} onCancel={onCancel} />
            ),
          },
          {
            label: `盘点`,
            key: 'fix',
            children: (
              <OutAndInForm data={data} type={tabKey} onFinish={onFinish} onCancel={onCancel} />
            ),
          },
        ],
        onChange: (key) => {
          setTabKey(key);
        },
      }}
    />
  );
};

export default OutAndIn;
