EditModal.tsx 3.66 KB
import React, { useState, useEffect } from 'react';
import { Button, Form, message, InputNumber, Select, Modal} from 'antd';
import ShopSelectNew from '@/components/ShopSelectNew';
import { useStore } from '../index';
import { saveConfigApi, getHaveShopListApi } from '../api';
import { debounce } from 'lodash';

const Option = Select.Option;

export default function Index() {
  const [form] = Form.useForm();
  const { current, setCurrent, setLoading } = useStore();
  const [confirm, setConfirm] = useState<boolean>(false);
  const [disabledShopIds, setDisabledShopIds] = useState<number[]>([]);

  useEffect(() => {
    if (current.visible && current.data.id) {
      handleSetValue();
      const shopList = current.data.shopList || [];
      getHaveShopListApi()
      .then(res => {
        const shopIdList = res.data || [];
        const _shopIds = shopList.map(v => v.shopId) || [];
        const disabledShop = shopIdList.filter(v => !(_shopIds.includes(v))) || [];
        setDisabledShopIds(disabledShop);
      })
      .catch(e => {
        message.error(e.message);
      });
    } else {
      getHaveShopListApi()
      .then(res => {
        const shopIdList = res.data || [];
        setDisabledShopIds(shopIdList);
      })
      .catch(e => {
        message.error(e.message);
      });
    }
  }, [current.visible]);

  function handleCancle() {
    setCurrent({visible: false, data: {}});
  }

  async function handleSubmit() {
    const params = await form.validateFields();
    setConfirm(true);
    const _params = {
      id: current.data?.id,
      dialAims: params.dialAims,
      shopList: params.shopList?.map((v: any) => ({shopId: v.value, shopName: v.label}))
    };
    saveConfigApi(_params)
    .then(res => {
      message.success(res.result);
      setConfirm(false);
      setCurrent({visible: false, data: {}});
      setLoading(true);
    })
    .catch(e => {
      message.error(e.message);
      setConfirm(false);
    });
  }

  function handleSetValue() {
    form.setFieldsValue({
      dialAims: current.data.dialAims,
      shopList: current.data.shopList?.map((v: any) => ({label: v.shopName, value: v.shopId}))
    });
  }

  function handleResetValue() {
    form.setFieldsValue({shopList: [], dialAims: undefined});
  }
  return (
    <Modal
      title={current.data.id ? "编辑" : "新增"}
      destroyOnClose
      visible={current.visible}
      maskClosable={false}
      onCancel={handleCancle}
      afterClose={() => handleResetValue()}
      width="60%"
      footer={[
        <Button key="cancel" disabled={confirm} onClick={handleCancle} style={{marginLeft: 10}}>取消</Button>,
        <Button key="submit" disabled={confirm} onClick={debounce(handleSubmit, 380)} type="primary" htmlType="submit" loading={confirm}>确认</Button>
        ]}
    >
      <Form
        form={form}
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 10 }}
      >

        <Form.Item
          label="线索有效接通目标"
          name="dialAims"
          rules={[{ required: true, message: "请输入" }]}
        >
          <InputNumber
            style={{width: '100%'}}
            controls={false}
            min={0}
            formatter={value => `${value}条/天`}
            precision={0}
            parser={value => value?.replace('条/天', '')}
          />
        </Form.Item>
        <Form.Item
          label="适用门店"
          name="shopList"
          rules={[{ required: true, message: "请选择门店" }]}
          labelAlign="right"
        >
          <ShopSelectNew disabledShopIds={disabledShopIds} defaultOptions={{bizTypes: "1"}} placeholder="请选择门店" multiple />
        </Form.Item>
        
      </Form>

    </Modal>
  );
}