import {Modal, Card, Descriptions, List, Button} from 'antd';
import React, { useEffect, useRef,useState } from 'react';
import { ProDescriptions} from '@ant-design/pro-components';
import {
  addCmsUserWorkExperiencesList
} from '@/services/Management/list';
export type ListFormProps = {
  onClose: (flag?: boolean, formVals?: unknown) => void;
  open: boolean;
  values?: Partial<API.MobileUser.ListRow>;
  matching?: any;
};

const listEdit: React.FC<ListFormProps> = (props) => {

  const mapRef = useRef(null);
  const [loading, setLoading] = useState(false);
  const [contactInfoList, setContactInfoList] = useState<API.Management.WorkExperence[]>([]);

  useEffect(() => {
    if (props.open && props.values) {
      console.log(props.values);
      fetchResumeDetail();
    }
  }, [props.values,props.open]);

  const fetchResumeDetail = async () => {
    if (!props.values?.userId) return;

    setLoading(true);
    try {
      const parm={userId:props.values.userId};
      const response = await addCmsUserWorkExperiencesList(parm);
      if (response.code === 200) {
        setContactInfoList(response.rows);
      }
    } catch (error) {
      console.error('获取经历详情失败:', error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <Modal
      open={props.open}
      onCancel={() => props.onClose()}
      title="简历详情"
      width={800}
      footer={[
        <Button key="cancel" onClick={() => props.onClose()}>
          取消
        </Button>
      ]}
    >
      <ProDescriptions
        column={2}
        loading={loading}
      >
        {
          <ProDescriptions.Item
            dataIndex="contactInfoList"
            label=""
            span={2}
            render={() => {
              if (loading) return '加载中...';
              if (!Array.isArray(contactInfoList) || contactInfoList.length === 0) {
                return <span style={{ color: '#999' }}>暂无经历信息</span>;
              }
              return (
                <List
                  itemLayout="vertical"
                  dataSource={contactInfoList}
                  renderItem={(item, index) => (
                    <List.Item
                      style={{ marginBottom: 16 }}
                    >
                      <Card
                        title={`经历信息 #${index + 1}`}
                        bordered
                        style={{ width: '100%', boxShadow: '0 2px 8px rgba(0,0,0,0.1)' }}
                      >
                        <Descriptions column={2} size="small">
                          <Descriptions.Item label="公司名称">{item.companyName || '未填写'}</Descriptions.Item>
                          <Descriptions.Item label="职务">{item.position || '未填写'}</Descriptions.Item>
                          <Descriptions.Item label="开始时间">{item.startDate || '未填写'}</Descriptions.Item>
                          <Descriptions.Item label="截止时间">{item.endDate || '未填写'}</Descriptions.Item>
                          <Descriptions.Item label="描述" span={2}>
                            {item.description || '未填写'}
                          </Descriptions.Item>
                        </Descriptions>
                      </Card>
                    </List.Item>
                  )}
                />
              );
            }}
          />
          }
      </ProDescriptions>
    </Modal>
  );
};

export default listEdit;
