import React, { useState ,useEffect} from 'react';
import { Button, message, Tooltip } from 'antd';
import Nodata from '../../../forge/Nodata';
import { getProjectById } from '../../api';

export default ({ detail, projectId, applyTaskId, isStudentApplyDate, secondStudentApplyDate, applyTask }) => {
  const [info, setInfo] = useState(detail);

  useEffect(()=>{
    if(!detail && projectId){
      // 通过项目Id查询项目详情
      getProjectById(projectId).then(response=>{
        if(response && response.message === 'success'){
          setInfo(response.data)
        }
      })
    }
  },[detail])

  return (
    info ? <div className={`projectDetailBox ${detail ? '':'byTask'}`}>
      <div className="projectDetailHead">
        <p>GitLink项目地址:&nbsp;&nbsp;<a href={info.gitlinkUrl} className='linkUrl' target={"_blank"}>{info.gitlinkUrl}</a></p>
        <div>项目简介:&nbsp;&nbsp;{info.projectIntro}</div>
      </div>
      {info.registrationTaskList && info.registrationTaskList.length > 0 ? info.registrationTaskList.map((item, index)=>{
        return <div className='taskItem mt15' key={index}>
          <div className="left">
            <div className="taskTitle" onClick={()=>{window.location.href=`/glcc/subjects/detail/${item.id}`}}><Tooltip title={item.taskName}>{item.taskName}</Tooltip></div>
            <div className='mt15 oneLine leftWidth'>导师姓名: &nbsp;&nbsp;{item.tutorName}</div>
            {item.tutorMail && <div className='mb15 email oneLine leftWidth'>邮箱地址: &nbsp;&nbsp;<span><Tooltip title={item.tutorMail}>{item.tutorMail}</Tooltip></span></div>}
          </div>
          <div className="center">
            <div className="taskDesc">{item.taskDesc}</div>
            {item.taskUrl && <div className="taskUrl oneLine">课题链接: &nbsp;&nbsp;<a href={item.taskUrl} target={"_blank"}>{item.taskUrl}</a></div>}
            <div>
              {/* 报名详情按钮 */}
              {applyTaskId && Object.keys(applyTaskId).includes(item.id.toString()) && <Button onClick={()=>{window.location.href=`/glcc/student/apply/${item.id}`}} className='lookDetail mr10'>报名详情</Button>}

              {/* 课题申请时间范围内： 申请课题 */}
              {/* 第一次报名 */}
              {isStudentApplyDate && !(applyTaskId && Object.keys(applyTaskId).includes(item.id.toString())) && <Button type='primary' className='applyBut mr10' onClick={()=>{applyTask(item.id)}}>申请课题</Button>}
              {/* 第二次报名 锁定状态 */}
              {secondStudentApplyDate && !(applyTaskId && Object.keys(applyTaskId).includes(item.id.toString())) && (item.locked ? <Tooltip title={"该课题已有学生入选"}><Button type='primary' className='mr10' disabled>申请课题</Button></Tooltip> : <Button type='primary' className='applyBut mr10' onClick={()=>{applyTask(item.id)}}>申请课题</Button>)}

              {/* 查看课题详情按钮 */}
              <Button onClick={()=>{window.open(`/glcc/subjects/detail/${item.id}`)}} className='lookDetail'>课题详情</Button>
            </div>
          </div>
          <div className="right oneLine taskUrl"><span className='taskReward'>￥{item.taskReward}</span></div>
        </div>
      }) : <Nodata _html="课题暂无数据" small={true}/>}
    </div>: <div className="projectDetailBox nodata"><Nodata _html="暂无数据" small={true}/></div>
  )
}