index.tsx 9.01 KB
import React, { useEffect, useState } from "react";
import { message, Modal, DatePicker } from "antd";
import { useStore } from "../index";
import ComfirmDiffModal from "../components/ComfirmDiffModal";
import * as Api from "./api";
import _ from "lodash";
import SubidyColumn from "../components/SubidyColumn";
import TransferTable from "../components/TransferTable";
import usePagination from "@/hooks/usePagination";

interface Props { }
const { RangePicker } = DatePicker;
interface Search {
  vin?: string;
  shopId?: number; //下零售门店
  retailBegin?: any; //下零售开始时间
  retailEnd?: any; //下零售结束时间
  subsidyName?: string; //补贴类型
}
export default function CreateModal(props: Props) {
  const {
    subsidyTypeList,
    visible,
    rebateId,
    setVisible,
    data,
    setLoading: setDetailLoading,
    currentItem,
    setOrderId,
    commonOrderList,
    orderId,
    detailVisible,
    setDetailVisible,
    commonComfirmApi,
    setSelectedRowkeys,
    setSubmitSelectedRowkeys,
    selectedRowKeys,
    commonWaitCheckList,
    handleCheckListApi,
    commonCheckList,
    commonRemoveCheckList,
    commonEnterCheckList,
    readOnly,
    carOrderTabkey,
    setCarOrderTabkey,
  } = useStore();
  const { dimension } = currentItem;
  const [comfrimloading, setComfirmLoading] = useState(false);

  const [delay, setDelay] = useState(true);
  const [checkDelay, setCheckDelay] = useState(true);
  const [orderList, setOrderList] = useState([]);
  const [confirmOrderList, setConfirmOrderList] = useState([]);
  // 请求参数
  const queryParams = new Map([
    [1, { rebateId, subsidyType: currentItem.subsidyType }],
    [2, { rebateId }],
    [3, { rebateId, promotionId: currentItem.promotionId }],
    [4, { rebateId }],
  ]);
  const waitCheckListApi = () => {
    if ([1, 2, 3].includes(dimension) && carOrderTabkey === '2') {
      return currentItem.supportType === 1 ? Api.getRebateTicketList : Api.getRebateRetailList;
    }
    // if (dimension === 3 && carOrderTabkey === '2') {
    //   return currentItem.supportType === 1 ? Api.getRebateTicketList : Api.getRebateRetailList;
    // }
    return commonWaitCheckList[dimension];
  };

  useEffect(() => {
    if (carOrderTabkey) {
      setParams(queryParams.get(dimension), true);
    }
  }, [carOrderTabkey]);
  /** 未确认清单 */
  // commonWaitCheckList[dimension],
  const {
    list,
    loading,
    setParams,
    paginationConfig,
    setLoading,
    innerParams,
  } = usePagination<FvmAllowance.WaitCheckListItems>(
    waitCheckListApi(),
    queryParams.get(dimension),
    { delay }
  );

  // 确认清单
  const {
    list: checkList,
    loading: checkLoading,
    setParams: setCheckParams,
    paginationConfig: checkPaginationConfig,
    setLoading: setCheckLoading,
    innerParams: checkInnerParams,
  } = usePagination<FvmAllowance.CheckListItems>(
    commonCheckList[dimension],
    queryParams.get(dimension),
    { delay: checkDelay }
  );

  const fetchTableList = () => {
    if (dimension === 1) {
      //厂家补贴
      if (rebateId && currentItem.subsidyType) {
        setParams(
          {
            rebateId,
            subsidyType: currentItem.subsidyType,
          },
          true
        );
        setCheckParams(
          {
            rebateId,
            subsidyType: currentItem.subsidyType,
          },
          true
        );
        setDelay(false);
        setCheckDelay(false);
      }
    }
    //厂家代收款
    if (dimension === 2) {
      if (rebateId) {
        setParams(
          {
            rebateId,
          },
          true
        );
        setCheckParams(
          {
            rebateId,
          },
          true
        );
        setCheckDelay(false);
        setDelay(false);
      }
    }
    //厂家促销(正激励)
    if (dimension === 3) {
      if (rebateId && currentItem.promotionId) {
        setParams(
          {
            rebateId,
            promotionId: currentItem.promotionId,
          },
          true
        );
        setCheckParams(
          {
            rebateId,
            promotionId: currentItem.promotionId,
          },
          true
        );
        setCheckDelay(false);
        setDelay(false);
      }
    }
    //现金启票奖励
    if (dimension === 4) {
      if (rebateId) {
        setParams(
          {
            rebateId,
          },
          true
        );
        setCheckParams(
          {
            rebateId,
          },
          true
        );
        setCheckDelay(false);
        setDelay(false);
      }
    }
  };
  useEffect(() => {
    if (visible) {
      fetchTableList();
    }
  }, [rebateId, currentItem, visible]);

  // useEffect(() => {
  //   if (list instanceof Array) {
  //     setOrderList([...list]);
  //   }
  //   if (checkList instanceof Array) {
  //     setConfirmOrderList([...checkList]);
  //   }
  // }, [list, checkList]);

  function getNotConfirmOrder(searchParams?: Search) {
    setLoading(true);
    const pa = {
      rebateId,
      subsidyType: currentItem.subsidyType,
      ...searchParams,
    };
    setParams({ ...pa, ...searchParams }, true);
  }

  /**
   *厂家补贴折让提交清单
   * @param searchParams
   */
  function getConfirmOrder(searchParams?: Search) {
    setLoading(true);
    const pa = {
      rebateId,
      subsidyType: currentItem.subsidyType,
    };
    setParams({ ...pa, ...searchParams }, true);
  }

  const addList = async (values: any[]) => {
    // const res = orderList.filter((x) => !values.find((y) => y.id === x.id));
    const orderIds = values.map((i) => i.id);
    /**
     *  选中厂家补贴折让加入提交清单
     */
    let submitApi = commonEnterCheckList[dimension];
    if ([1, 2].includes(dimension) && carOrderTabkey === '2') {
      submitApi = handleCheckListApi[dimension];
    }
    if (dimension === 3 && carOrderTabkey === '2') {
      submitApi = currentItem.supportType === 1 ? Api.promotionTicketCheck : Api.promotionSaleCheck;
    }
    const { result, success } = await submitApi({
      rebateId,
      orderIds,
      carId: carOrderTabkey === '2' ? values[0].id : undefined,
      inwardName: values[0].inwardName,
      subsidyType: currentItem.subsidyType,
      promotionId: dimension == 3 ? currentItem.promotionId : undefined
    });
    if (!success) {
      message.error(result.message);
      return;
    } else {
      message.success("操作成功");
    }
    // 从表格一删除
    // setOrderList([...res]);
    setSelectedRowkeys([]);
    /** 加入表格二 */
    // setConfirmOrderList([...confirmOrderList, ...values]);
    // 重新加载两个列表
    setParams({ ...innerParams }, true);
    setCheckParams({ ...checkInnerParams }, true);
  };

  const removeList = async (values: any[]) => {
    const res = confirmOrderList.filter(
      (x) => !values.find((y) => y.id === x.id)
    );
    // 从表格二删除
    // setConfirmOrderList([...res]);
    setSubmitSelectedRowkeys([]);
    /** 加入表格一 */
    // setOrderList([...orderList, ...values]);
    /**
     * 选中厂家补贴折让移出提交清单
     */
    const orderIds = values.map((i) => i.id);

    // const { result, success } = await api.rebateSubsidyRemove({
    const { result, success } = await commonRemoveCheckList[dimension]({
      rebateId,
      orderIds,
      subsidyType: currentItem.subsidyType,
    });
    if (!success) {
      message.error(result);
      return;
    }
    // 重新加载两个列表
    setParams({ ...innerParams }, true);
    setCheckParams({ ...checkInnerParams }, true);
  };

  const getColumn = () => {
    const tmpColumn = SubidyColumn()[dimension][1];
    if (readOnly) {
      tmpColumn.pop();
    }
    return tmpColumn;
  };
  return (
    <Modal
      title={`${currentItem.fundsName || ""}车辆清单`}
      visible={visible}
      width="70%"
      maskClosable={false}
      confirmLoading={comfrimloading}
      destroyOnClose
      onCancel={() => { setVisible(false); setCarOrderTabkey(''); }}
      onOk={() => { setVisible(false); setCarOrderTabkey(''); !readOnly && setDetailLoading(true); }}
    // footer={
    //   <Button
    //     onClick={() => { setVisible(false); !readOnly && setDetailLoading(true); }}
    //   >
    //     {readOnly ? "取消" : "完成"}
    //   </Button>
    // }
    >
      <div>
        <TransferTable
          upLoading={loading}
          dowmLoading={checkLoading}
          dataSource1={list}
          dataSource2={checkList}
          columns1={SubidyColumn()[dimension][0]}
          columns2={getColumn()}
          paginationConfig={paginationConfig}
          checkPaginationConfig={checkPaginationConfig}
          addList={addList}
          removeList={removeList}
          getConfirmOrder={getConfirmOrder}
          getNotConfirmOrder={getNotConfirmOrder}
        />
      </div>
      <ComfirmDiffModal
        visible={detailVisible}
        onCancel={() => {
          setDetailVisible(false);
          setOrderId(undefined);
        }}
        orderId={orderId}
        fetchList={() => {
          setLoading(true);
          setCheckLoading(true);
        }}
      />
    </Modal>
  );
}