WorkItemLimit.tsx 5.91 KB
import React, { useState, useEffect, Ref, forwardRef } from 'react';
import { Card, Button, Table, Input, message, Select } from 'antd';
import TableSelect from '@/pages/coupon/CouponConfig/components/TableSelect';
import { PlusOutlined } from "@ant-design/icons";
import * as API from '../api';
import * as IF from '../interface';
import usePagination from '@/hooks/usePagination';
import { deRepetition } from '@/components/Condition/SeriesSelector/components/utils';
import useInitial from '@/hooks/useInitail';
import { getBrandFilterApi, getSeriesApi } from '@/common/api';
import { debounce } from 'lodash';

const st = require('@/pages/coupon/CouponConfig/style.less');

const Column = Table.Column;
const Search = Input.Search;

interface Props {
  onChange?: (value: any[]) => any,
  value?: any[],
  style?: any
  disabled?: boolean,
  title?: string,
  useScene: any, //作业项类型
}

function PartItem(props: Props, ref?: Ref<any>) {
  const { onChange, value, style, disabled, title, useScene } = props;
  /**机修作业项,类型1,2,5 */
  const params = useScene === 1 ? { workTypes: "1,2,5,9" } : { workType: useScene };
  const { loading, setLoading, list, setParams, paginationConfig, innerParams } = usePagination(API.fetchListApi, params);
  const { data: brandList } = useInitial(getBrandFilterApi, [], {});
  const [seriesList, setSeriesList] = useState<CommonApi.OptionVO[]>([]);
  const [visible, setVisible] = useState(false);
  const [listData, setListData] = useState<IF.ListVO[]>([]);

  useEffect(() => {
    value && setListData(value.map((v: any) => ({ itemCode: v.applyValue, itemName: v.applyName })));
  }, []);

  function getSeries(brandId: number) {
    getSeriesApi(brandId).then((res) => {
      setSeriesList(res.data || []);
    }).catch((err) => {
      message.error(err.message);
    });
  }

  const onFilter = debounce((params) => setParams({ ...innerParams, ...params }, true), 600);
  function selectedItems(value: any[]) {
    const partIds = value.length && value.map((i: any) => i.applyValue).toString();
    API.fetItemsApi({ partIds }).then(res => {
      setListData((res.data || {}).data || []);
    }).catch(e => {
      message.error(e.message);
    });
  }

  function onModalSelected(value: any) {
    const newData = deRepetition(listData.concat(value), 'itemCode');
    setListData(newData);
    onChange && onChange(newData.map((item: any) => ({ applyValue: item.itemCode, applyName: item.itemName, applyType: 31 })));
  }

  function remove(itemCode: string) {
    const newData = [...listData];
    const index = newData.findIndex(item => itemCode === item.itemCode);
    newData.splice(index, 1);
    setListData(newData);
    onChange && onChange(newData.map(item => ({ applyValue: item.itemCode, applyName: item.itemName, applyType: 31 })));
  }

  return (
    <Card
      style={style}
      className={st.cardTable}
      title={`${title}(${useScene == 1 ? "机修" : (useScene == 3 ? "钣喷" : "洗车")})`}
      extra={!disabled && (
        <div style={{ flex: 1, display: 'flex', justifyContent: 'space-between', flexDirection: 'row' }}>
          <Button type="primary" icon={<PlusOutlined />} onClick={() => setVisible(true)}>新增</Button>
        </div>
      )}
    >
      <Table
        rowKey="itemCode"
        dataSource={listData}
        // pagination={false}
        scroll={{ y: 400 }}
        bordered
      >
        <Column title="作业代码" dataIndex="itemCode" width="30%" />
        <Column title="作业名称" dataIndex="itemName" align="center" width="30%" />
        {/* <Column title="作业类型" dataIndex="workTypeName" align="center" width="20%" /> */}
        {!disabled && (
          <Column
            title="操作"
            width="20%"
            render={(text, record: IF.ListVO) => {
              return (
                <a onClick={() => remove(record.itemCode!)}>删除</a>
              );
            }}
          />
        )}
      </Table>
      <TableSelect
        visible={visible}
        onCancel={() => setVisible(false)}
        fetchList={() => setLoading(true)}
        dataSource={list}
        onSure={onModalSelected}
        value={listData}
        pagination={paginationConfig}
        loading={loading}
        title="作业项"
        columns={[
          { title: '作业名称', dataIndex: 'itemName' },
          { title: '作业代码', dataIndex: 'itemCode' },
          { title: '作业类型', dataIndex: 'workTypeName' },
        ]}
        filter={[
          <Select
            optionFilterProp="children"
            showSearch
            allowClear
            placeholder="搜索品牌"
            style={{ width: 200, margin: 5 }}
            onChange={(value) => {
              setParams({ ...innerParams, brandId: value }, true);
              getSeries(value);
            }}
          >
            {brandList.map((item) => <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>)}
          </Select>,
          <Select
            optionFilterProp="children"
            mode="multiple"
            showSearch
            allowClear
            placeholder="搜索车系"
            style={{ width: 200, margin: 5 }}
            onChange={(v) => { console.log(v); setParams({ ...innerParams, seriesIds: v.join(',') }, true); }}
          >
            {seriesList.map((item) => <Select.Option key={item.id} value={item.id}>{item.name}</Select.Option>)}
          </Select>,
          <Search
            allowClear
            value={innerParams.keywords}
            style={{ margin: 5 }}
            placeholder="搜索作业名称/代码"
            onChange={(e) => onFilter({ keywords: e.target.value })}
          />,
          <Search
            allowClear
            value={innerParams.specCode}
            style={{ margin: 5 }}
            placeholder="搜索整车代码"
            onChange={(e) => onFilter({ specCode: e.target.value })}
          />
        ]}
      />
    </Card>
  );
}

export default forwardRef(PartItem);