WorkTiemConfig.jsx 2.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* eslint-disable no-underscore-dangle */
import React, { useEffect } from 'react';

import { Form, Modal, Input, TimePicker, message } from 'antd';
import moment from 'moment';
import { AddOrEditWorkTime } from '@/services/holidays/holidays';

const WorkTiemConfig = props => {
  const { onSubumit, handleCancel, visible, msg, modalType, allTime } = props;
  const [form] = Form.useForm();
  useEffect(() => {
    form.resetFields();
    if (visible) {
      getFormData();
    }
  }, [visible]);
  // 获取表单回显
  const getFormData = () => {
    console.log(msg, 'msg');
    form.setFieldsValue({
21
      workTime: [moment(msg.from, 'HH:mm'), moment(msg.to, 'HH:mm')],
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    });
  };
  // 时间段是否重叠
  const isOverlap = (StartA, EndA, StartB, EndB) => {
    const getT = t => new Date(t);
    return !(getT(EndA) <= getT(StartB) || getT(StartA) >= getT(EndB));
  };

  // 提交表单
  const onFinish = () => {
    form.validateFields().then(validate => {
      if (validate) {
        let obj = {};
        console.log(modalType);
        console.log(validate);
37 38 39
        console.log(moment(validate.workTime[0]._d).format('HH:mm'));
        let startTime = moment(validate.workTime[0]._d).format('HH:mm');
        let endTime = moment(validate.workTime[1]._d).format('HH:mm');
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        for (let i = 0; i < allTime.length; i++) {
          if (allTime[i].name !== msg.name) {
            console.log(`${moment().format('YYYY-MM-DD')} ${allTime[i].from}`);
            if (
              isOverlap(
                `${moment().format('YYYY-MM-DD')} ${startTime}`,
                `${moment().format('YYYY-MM-DD')} ${endTime}`,
                `${moment().format('YYYY-MM-DD')} ${allTime[i].from}`,
                `${moment().format('YYYY-MM-DD')} ${allTime[i].to}`,
              )
            ) {
              message.error(`时间段与${allTime[i].name}时间段重合`);
              return;
            }
          }
        }

        AddOrEditWorkTime({ type: msg.type, startTime, endTime }).then(res => {
          if (res.code === 0) {
            message.success('修改成功');
            onSubumit();
          }
        });
      }
    });
  };
  return (
    <Modal
      title={`编辑${msg.name}时间`}
      visible={visible}
      onOk={onFinish}
      onCancel={handleCancel}
      maskClosable={false}
      destroyOnClose
    >
      <Form
        form={form}
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 18 }}
        initialValues={{ remember: true }}
      >
        <Form.Item label="工作时间" name="workTime">
82
          <TimePicker.RangePicker format="HH:mm" />
83 84 85 86 87 88
        </Form.Item>
      </Form>
    </Modal>
  );
};
export default WorkTiemConfig;