index.tsx 10.7 KB
import React, { useState, useEffect, useMemo } from 'react';
import { Card, Form, Input, Button, Select, Row, message, InputNumber, Radio, Switch, Modal } from 'antd';
import * as api from './api';
import { useStore } from '../../index';
import SpecList from './SpecList';
import { history } from 'umi';
import SelectStandardName from './SelectStandardName';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import SelectPartsTable from './SelectPartsTable';
import { getNamePart } from '@/pages/capital/StandardMange/api';

const layout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 16 },
};
const Option = Select.Option;
const RadioGroup = Radio.Group;

export default function AddItem() {
  const { typeName, data = [], currentItem = {}, unitItem, setBreadcrumbs, setLoading, setCurrentBreadcrumb, setIsPart, editDisabled } = useStore();
  const [saveConfig, setSaveConfig] = useState<boolean>(false);
  const [nameId, setNameId] = useState<number>();
  const [visible, setVisible] = useState(false);
  const [partValue, setPartValue] = useState<{
    partId?: number, partCode?: string,
  }>({});
  const [form] = Form.useForm();

  useEffect(() => {
    form.setFieldsValue({
      status: true,
      ...currentItem,
      unit: currentItem.unit ? currentItem.unit : undefined,
      specList: currentItem.specList || [],
      premise: currentItem.premise || 0,
      part: Number(currentItem.part) || 0,
      purchaseChannelList: currentItem.purchaseChannelList && JSON.stringify(currentItem) != '{}' ? currentItem.purchaseChannelList.map(i => ({ value: i.subjectId, label: i.subjectName })) : undefined,
    });
    currentItem.partCode && setPartValue({ partCode: currentItem.partCode, partId: currentItem.partId });
  }, []);

  function save(feildValue: any) {
    const params = {
      ...feildValue,
      ...partValue,
      id: currentItem.id,
      name: currentItem.name || feildValue.name.label || feildValue.name,
      nameId: currentItem.nameId || feildValue.name.value || nameId,
      unit: feildValue.unit.label || currentItem.unit,
      maxPrice: feildValue.maxPrice || 0,
      purchaseChannelList: feildValue.purchaseChannelList.map((i: any) => ({ subjectId: i.value, subjectName: i.label }))
    };
    setSaveConfig(true);
    api.saveForm(params).then(res => {
      setSaveConfig(false);
      setNameId(undefined);
      setPartValue({});
      if (res.data && !currentItem.id) {
        Modal.confirm({
          title: '保存成功',
          icon: <ExclamationCircleOutlined />,
          content: '是否跳转授权物品领用?',
          okText: '去授权',
          cancelText: '返回',
          onOk() {
            history.push(`/ams/standardMange/receive/rules/goods/${res.data}/name`);
          },
          onCancel() {
            setLoading(true);
            setBreadcrumbs([{ name: '列表', key: 'list' }]);
            setCurrentBreadcrumb({ name: '列表', key: 'list' });
          },
        });
        return;
      }
      message.success('保存成功');
      setLoading(true);
      setBreadcrumbs([{ name: '列表', key: 'list' }]);
      setCurrentBreadcrumb({ name: '列表', key: 'list' });
    }).catch(e => {
      message.error(e.message);
    }).finally(() => {
      setSaveConfig(false);
    });
  }

  function changeSpc(value: number) {
    setIsPart(!!value);
    form.setFieldsValue({ name: undefined, specList: [] });
  }

  /**选择配件处理成规格型号数据 */
  function onSelct(item: any) {
    const spec = [
      { name: "配件编码", code: "partCode" },
      { name: "配件件号", code: "partNo" },
      { name: "类型", code: "typeName" },
      { name: "品牌", code: "brandNames" }
    ].map((i, key) => ({
      sort: key,
      specDetails: item[i.code],
      standardSpecName: i.name,
    }));
    if (!item.brandNames) {
      spec.splice(3, 1);
    }
    getNamePart(item.partCode).then(res => {
      const { id } = res.data || {};
      setNameId(id);
    }).catch(e => {
      message.error(e.message);
    });
    setPartValue({ partCode: item.partCode, partId: item.id });
    form.setFieldsValue({ name: item.partName, partCode: item.partCode, partId: item.id, specList: spec, type: 2 });
  }

  return (
    <Card>
      <h1 style={{ marginBottom: 20 }}>1.基本信息</h1>
      <Form {...layout} form={form} onFinish={save}>
        <Form.Item label="是否配件" name="part" rules={[{ required: true, message: '请选择' }]}>
          <RadioGroup disabled={!!editDisabled} onChange={(e) => { changeSpc(e.target.value); }}>
            <Radio value={1}>是</Radio>
            <Radio value={0}>否</Radio>
          </RadioGroup>
        </Form.Item>
        <Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.part != currentValues.part}>
          {({ getFieldValue }): any => {
            return getFieldValue("part") ? (
              <Form.Item name="name" label="配件名称" rules={[{ required: true, message: '请选择物品名称' }]}>
                <Input.Search placeholder="点击选择" disabled={!!currentItem.id} enterButton="选择" onSearch={() => setVisible(true)} />
              </Form.Item>
            ) : (
              <Form.Item name="name" label="物品名称" rules={[{ required: true, message: '请选择物品名称' }]}>
                <SelectStandardName disabled={!!currentItem.id} onChange={(v) => { form.setFieldsValue({ type: v.type }); }} />
              </Form.Item>
            );
          }}
        </Form.Item>
        <Form.Item label="物品类型" name="type" rules={[{ required: true, message: '请选择物品类型' }]}>
          <Select
            placeholder="请选择"
            allowClear
            disabled
          >
            {typeName.map(i => (
              <Option value={i.type} key={i.type}>{i.name}</Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item label="单位" name="unit" rules={[{ required: true, message: '请选择物品单位' }]}>
          <Select
            placeholder="请选择(可搜索)"
            labelInValue
            optionFilterProp="children"
            showSearch
            disabled={!!editDisabled}
          >
            {unitItem.map((i: StanderList.UnitItem) => (
              <Option value={i.id} key={i.id}>{i.unitName}</Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item label="状态" name="status" valuePropName="checked" rules={[{ required: true, message: '请选择' }]}>
          <Switch disabled={!!editDisabled} checkedChildren="启用" unCheckedChildren="禁用" />
        </Form.Item>
        <Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.type != currentValues.type}>
          {({ getFieldValue }): any => {
            return getFieldValue("type") === 1 ? (
              <Form.Item label="摊销周期 (月)" name="amortizePeriod" rules={[{ required: true, message: '请输入摊销周期' }]}>
                <InputNumber disabled={!!editDisabled} min={0} style={{ width: '100%' }} placeholder="请输入" />
              </Form.Item>
            ) : null;
          }}
        </Form.Item>
        <Form.Item label="备注" name="remark">
          <Input.TextArea disabled={!!editDisabled} placeholder="请输入" allowClear />
        </Form.Item>
        <h1 style={{ marginBottom: 20 }}>2.规格型号</h1>

        <Form.Item label="规格配置" name="specList" rules={[{ required: true, message: '请配置规格型号' }]}>
          <SpecList />
        </Form.Item>
        <h1 style={{ marginBottom: 20 }}>3.采购规则</h1>
        <Form.Item label="最大采购单价" required>
          <Form.Item
            name="maxPrice"
            noStyle
            rules={[{ required: true, message: '最大采购金额(单个物品)' }]}
          >
            <InputNumber disabled={!!editDisabled} style={{ width: 150 }} min={0} precision={4} placeholder="请输入金额" />
          </Form.Item>
          <span style={{ marginLeft: 10 }}>元</span>
        </Form.Item>
        <Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.type != currentValues.type}>
          {({ getFieldValue }): any => {
            return getFieldValue("type") === 3 ? (
              <Form.Item label="最小单位采购数量" name="minNum" {...layout} rules={[{ required: true, message: '请输入最小采购数量' }]}>
                <InputNumber disabled={!!editDisabled} style={{ width: 150 }} min={0} placeholder="请输入单位数量" />
              </Form.Item>
            ) : null;
          }}
        </Form.Item>
        <Form.Item label="计划提前天数" name="planAheadDay" {...layout}>
          <InputNumber disabled={!!editDisabled} style={{ width: 150 }} min={0} placeholder="请输入" />
        </Form.Item>
        <Form.Item label="采购渠道" name="purchaseChannelList" rules={[{ required: true, message: '请选择采购渠道' }]}>
          <Select disabled={!!editDisabled} placeholder="请选择采购渠道" allowClear labelInValue mode="multiple" showSearch optionFilterProp="children">
            {(data || []).map((i: any) => (
              <Option value={i.id} key={i.id}>{i.name}</Option>
            ))}
          </Select>
        </Form.Item>
        <h1 style={{ marginBottom: 20 }}>4.领用条件</h1>
        <Form.Item label="领用前提条件" name="premise" rules={[{ required: true, message: '请选择' }]}>
          <Select disabled={!!editDisabled} placeholder="请选择">
            <Option value={0} key={0}>无限制</Option>
            <Option value={1} key={1}>入职后</Option>
            <Option value={2} key={2}>转正后</Option>
          </Select>
        </Form.Item>
        <Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.premise != currentValues.premise}>
          {({ getFieldValue }): any => {
            const premise = getFieldValue("premise");
            return premise ? (
              <Form.Item label={premise === 1 ? "入职后" : "转正后"} name="premiseValue" {...layout} rules={[{ required: true, message: '请输入' }]}>
                <InputNumber disabled={!!editDisabled} style={{ width: '100%' }} addonAfter="月" min={0} placeholder="请输入领取前提条件值" />
              </Form.Item>
            ) : null;
          }}
        </Form.Item>
        <Row justify="center">
          <Button type="primary" onClick={() => { setBreadcrumbs([{ name: '列表', key: 'list' }]); setCurrentBreadcrumb({ name: '列表', key: 'list' }); }}>返回</Button>
          <Button style={{ marginLeft: 25 }} type="primary" disabled={!!editDisabled} loading={saveConfig} onClick={() => form.submit()}>保存</Button>
        </Row>
      </Form>
      <SelectPartsTable
        visible={visible}
        value={[]}
        onCancel={() => setVisible(false)}
        onChange={onSelct}
      />
    </Card>
  );
}