import type { FC } from 'react';
import { useEffect ,useState} from 'react';
import { message, Button } from 'antd';
import { Line, Radar } from '@ant-design/charts';
import { Link, history, useRequest,useParams } from 'umi';
import { PageContainer } from '@ant-design/pro-layout';
import moment from 'moment';
import EditableLinkGroup from './components/EditableLinkGroup';
import styles from './style.less';
import {getExaminationById,getExaminationRuleById ,getPersonPaperId,getCurrentDate,savePersonAnswerTime,getPersonAnswerTime } from './service';
import cookie from 'react-cookies';
import OfflineData from './components/OfflineData';

import './index.less';
const Qualification = () => {
  const [examinationInfo, setExamination] = useState({});
  const [rule, setRule] = useState({});
  const [paperId, setPaperId] = useState('');
  const params = useParams();
  //获取考试详情
  const data = useRequest(() => {
    return getExaminationById({
      examination_id:params.examination_id
    });
  },{
    formatResult: (result) => {
      console.log('result',result);
      setExamination(result.bean)
    }
  });
  const ruleInfo = useRequest(() => {
    return getExaminationRuleById({
      examination_id:params.examination_id
    });
  },{
    formatResult: (result) => {
      console.log('result规则',result);
      setRule(result.bean)
    }
  });

  const paperInfo = useRequest(() => {
    return getPersonPaperId({
      examination_id:params.examination_id,
      person_id:cookie.load('person_id')
    });
  },{
    formatResult: (result) => {
      setPaperId(result.paper_id)
    }
  });

  // const getTime=(time)=>{
  //   const current_date=new Date(time.current_date.replace(/-/g, "/"))
  //   const start_date=new Date(rule.examination_start_time.replace(/-/g, "/"));
  //   const end_date=new Date(rule.examination_end_time.replace(/-/g, "/"));
  //   const duration=end_date.getTime()-start_date.getTime();
  //   const dateDiff=end_date.getTime()-current_date.getTime();
  //   const date=dateDiff/1000;
  //   const time=rule.examination_time*60;
  //   history.push(`/dashboard/qualification/examAnswer/${params.examination_id}/${examinationInfo.examination_name}/${time}`)
  // }

  return (
    <div className="qualification" style={{height:window.innerHeight-200,background:'#fff',width:'100%',padding:20}}>
      <div ><span className="title"  onClick={()=>{history.push('/dashboard/workplace')}} style={{color:'#8C8C8C',cursor:'pointer',}}>首页</span> / 资质考试 / {examinationInfo?.examination_name}</div>
      <div style={{paddingTop:'3rem',width:'20rem',margin:'0 auto'}}>
        <div  className='condition' >
          <div className='condition-title' >考试项目：</div>
          <span className='condition-text' >{examinationInfo?.examination_name}</span>
        </div>
        <div  className='condition' >
          <div className='condition-title' >考试时间：</div>
          <span className='condition-text' >{examinationInfo?.examination_start_time}-{examinationInfo?.examination_end_time}</span>
        </div>
        <div  className='condition' >
          <div className='condition-title' >考试时长：</div>
          <span className='condition-text' >{rule?.examination_time}分钟</span>
        </div>
        <div  className='condition' >
          <div className='condition-title' >满分：</div>
          <span className='condition-text' >{rule?rule.sum_score===null?'--':rule.sum_score:'--'}分</span>
        </div>
        <div  className='condition' >
          <div className='condition-title' >通过标准：</div>
          <span className='condition-text' >{rule?rule.pass_score===null?'--':rule.pass_score:'--'}分</span>
        </div>
        <div style={{textAlign:'center'}}>
          <Button type={"primary"} onClick={async() => {
            if(paperId===''){
              message.warning('您已参加过考试，不能开始考试！');
            }else {
              try {
                const info=await savePersonAnswerTime({
                  examination_id:params.examination_id,
                  paper_id:paperId,
                  examination_time:rule.examination_time,
                  person_id:cookie.load('person_id')
                })
                console.log('startExam------',info)

                // const time = await getCurrentDate();
                // getTime(time);
                const time=rule.examination_time*60;
                history.push(`/dashboard/qualification/examAnswer/${params.examination_id}/${examinationInfo.rules_id}/${examinationInfo.examination_name}/${time}`)

              }catch (error) {
                if(error.response){
                  message.error(error.response.info);
                }
              }
            }





          }
            }


          >开始考试</Button>
        </div>

      </div>
      <div className={"tip"}>
        *考试开始后，不可随意离开，离开答题页，依然记录时间
      </div>
    </div>
  );
};

export default Qualification;
