HolidayConfig.jsx 6.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import React, { useState, useEffect, useRef } from 'react';
import { Form, Modal, Input, message, Radio, Select, AutoComplete } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { AddFlowHoliday, DeleteFlowHoliday } from '@/services/holidays/holidays';
const { confirm } = Modal;
const holidaysList = new Set(['元旦', '春节', '清明节', '劳动节', '端午节', '中秋节', '国庆节']);
const HolidayConfig = props => {
  const { visible, onSubumit, handleCancel, msg } = props;
  const [options, setOptions] = useState([]);
  const [flag, setFlag] = useState(0);
  const [form] = Form.useForm();
  const [formRest] = Form.useForm();
  useEffect(() => {
    if (visible) {
      let obj = msg.msg;
16
      console.log(msg, 'afsdfasd');
17 18 19 20 21 22 23
      let list = [...holidaysList].map(item => ({ value: item, label: item }));
      if (obj.nWeek === 6) {
        list.push({ value: '周六', label: '周六' });
      }
      if (obj.nWeek === 7) {
        list.push({ value: '周日', label: '周日' });
      }
24
      // console.log(list, 'list');
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
      setOptions(list);
      if (!msg.isRest) {
        confirm({
          title: '确定要改为休息吗?',
          icon: <ExclamationCircleOutlined />,
          content: (
            <div>
              <div>{`${msg.msg.date}(${obj.ncWeek})`}</div>
              <Form
                form={form}
                preserve={false}
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 18 }}
                initialValues={{ remember: true }}
                labelAlign="left"
              >
                <Form.Item
                  label="假期名称"
                  name="dayName"
                  style={{
                    margin: '10px 0 0 0',
                  }}
                >
                  {obj.nWeek === 6 || obj.nWeek === 7 ? (
49
                    <Select placeholder="请选择假日名称" options={list} />
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                  ) : (
                    <AutoComplete placeholder="请输入假日名称" options={msg.holidayOptions} />
                  )}
                </Form.Item>
              </Form>
            </div>
          ),
          okText: '是',
          okType: 'primary',
          cancelText: '否',
          onOk() {
            return new Promise((resolve, reject) => {
              form.validateFields().then(validate => {
                let name;
                name = validate.dayName;
                // if (validate.dayName === '周六') {
                //   name = '周六';
                // }
                // if (validate.dayName === '周日') {
                //   name = '周日';
                // }
                let dayType = 1;
                if (validate.dayName === '周六' || validate.dayName === '周日') {
                  dayType = 2;
                }
                if (holidaysList.has(validate.dayName)) {
                  dayType = 3;
                }
                if (validate.dayName) {
                  AddFlowHoliday({
                    dayName: name,
                    dayType,
                    startDate: msg.date,
                    endDate: msg.date,
                  }).then(res => {
                    if (res.code === 0) {
                      message.success('修改成功');
                      onSubumit();
                      resolve();
                    } else {
                      message.error(res.msg);
                      reject();
                    }
                  });
                } else {
                  message.error('请填写假期名称');
                  reject();
                }
              });
            });
          },
          onCancel() {
            handleCancel();
          },
        });
      } else if (obj.nWeek !== 6 && obj.nWeek !== 7) {
        confirm({
          title: '确定要改为工作吗?',
          icon: <ExclamationCircleOutlined />,
          content: `${msg.date}(${obj.ncWeek})`,
          okText: '是',
          okType: 'primary',
          cancelText: '否',
          onOk() {
            // setCanSelect(true);
            DeleteFlowHoliday({ dayDate: msg.date }).then(res => {
              if (res.code === 0) {
                message.success('删除成功');
                onSubumit();
              } else {
                message.error(res.msg);
              }
            });
          },
          onCancel() {
            handleCancel();
          },
        });
      }
    }
  }, [visible]);
  const onFinish = () => {
    formRest.validateFields().then(values => {
      console.log(values);
      if (values.dayType === 1) {
        DeleteFlowHoliday({ dayDate: msg.date }).then(res => {
          if (res.code === 0) {
            message.success('删除成功');
            onSubumit();
          } else {
            message.error(res.msg);
          }
        });
      } else {
        AddFlowHoliday({
          dayName: values.dayName,
          dayType: values.dayName === '周日' || values.dayName === '周六' ? 2 : 3,
          startDate: msg.date,
          endDate: msg.date,
        }).then(res => {
          if (res.code === 0) {
            message.success('修改成功');
            onSubumit();
          } else {
            message.error(res.msg);
          }
        });
      }
    });
  };
  const onFieldsChange = changedFields => {
    if (changedFields[0].name[0] === 'dayType') {
      setFlag(flag + 1);
    }
  };
165

166 167 168 169
  return (
    <div>
      <Modal
        title={null}
170
        visible={visible && (msg.msg.nWeek === 6 || msg.msg.nWeek === 7) && msg.isRest}
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        onOk={onFinish}
        onCancel={handleCancel}
        maskClosable={false}
        destroyOnClose
        width={416}
      >
        <Form
          form={formRest}
          preserve={false}
          labelCol={{ span: 6 }}
          wrapperCol={{ span: 18 }}
          initialValues={{ remember: true }}
          onFieldsChange={onFieldsChange}
          labelAlign="left"
        >
          <Form.Item
            label="操作类型"
            name="dayType"
            style={{ margin: '10px 0 0 0' }}
            initialValue={1}
          >
            <Radio.Group>
              <Radio value={1}>改为上班</Radio>
              <Radio value={2}>改为节假日</Radio>
            </Radio.Group>
          </Form.Item>
          <Form.Item
            label="假期名称"
            name="dayName"
            rules={[
              {
                required: formRest.getFieldValue('dayType') === 2,
              },
            ]}
            style={{
              margin: '10px 0 0 0',
              display: formRest.getFieldValue('dayType') === 2 ? 'flex' : 'none',
            }}
          >
            {msg.msg.nWeek === 6 || msg.msg.nWeek === 7 ? (
              <Select placeholder="请选择假日名称" options={options} />
            ) : (
              <AutoComplete placeholder="请输入假日名称" options={options} />
            )}
          </Form.Item>
        </Form>
      </Modal>
    </div>
  );
};

export default HolidayConfig;