import React, { useEffect } from 'react';
import { ModalForm, ProForm, ProFormText, ProFormDigit, ProFormSelect } from '@ant-design/pro-components';
import { Form } from 'antd';
import { addJobForCompany, updateJobForCompany } from '@/services/jobportal/outdoorFairDetail';

interface Props {
  open: boolean;
  fairId: number;
  company: any;
  job: any;
  onCancel: () => void;
  onSuccess: () => void;
}

const JobEditModal: React.FC<Props> = ({ open, fairId, company, job, onCancel, onSuccess }) => {
  const [form] = Form.useForm();

  useEffect(() => {
    if (open) {
      form.resetFields();
      if (job) {
        form.setFieldsValue(job);
      }
    }
  }, [open, job, form]);

  return (
    <ModalForm
      title={job ? '编辑岗位' : `为「${company?.companyName || ''}」添加岗位`}
      form={form}
      open={open}
      width={500}
      modalProps={{ destroyOnClose: true, onCancel }}
      onFinish={async (values) => {
        if (job) {
          const res = await updateJobForCompany({ ...job, ...values });
          if (res.code === 200) { onSuccess(); return true; }
        } else if (company) {
          const res = await addJobForCompany({ fairId, companyId: company.id, ...values });
          if (res.code === 200) { onSuccess(); return true; }
        }
        return false;
      }}
    >
      <ProFormText name="jobTitle" label="岗位名称" rules={[{ required: true, message: '请输入岗位名称' }]} placeholder="请输入岗位名称" />
      <ProForm.Group>
        <ProFormDigit name="minSalary" label="最低薪资(K)" width="sm" min={0} rules={[{ required: true }]} fieldProps={{ precision: 0 }} />
        <ProFormDigit name="maxSalary" label="最高薪资(K)" width="sm" min={0} rules={[{ required: true }]} fieldProps={{ precision: 0 }} />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormSelect name="education" label="学历要求" width="sm"
          options={['高中', '大专', '本科', '硕士', '博士', '不限'].map(v => ({ label: v, value: v }))}
          rules={[{ required: true }]}
        />
        <ProFormSelect name="experience" label="经验要求" width="sm"
          options={['不限', '应届生', '1-3年', '3-5年', '5-10年', '10年以上'].map(v => ({ label: v, value: v }))}
          rules={[{ required: true }]}
        />
      </ProForm.Group>
      <ProFormDigit name="vacancies" label="招聘人数" width="md" min={1} rules={[{ required: true }]} fieldProps={{ precision: 0 }} />
    </ModalForm>
  );
};

export default JobEditModal;
