import { PlusOutlined } from '@ant-design/icons';
import { Button, Card, List, Progress, Typography, Image, message } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import { history, useRequest } from 'umi';
import { getRulesList, getRulesPaper, getSubjectInfo } from './service';
import React, { useEffect,useRef ,useState} from 'react';
import styles from './style.less';

import ProTable from '@ant-design/pro-table';
import { Legend } from 'bizcharts';

const SubjectList = () => {

  const columns: ProColumns<GithubIssueItem>[] = [
    {
      dataIndex: 'index',
      valueType: 'indexBorder',

      hideInTable: true,
    },
    {
      title: '封面',
      valueType: 'image',
      dataIndex: 'url',
      width: "10%",
      render: (dom, entity) => {
        return <Image key={entity.url} width={200} height={150} src={`/dsideal_yy/html/${entity.url}`} fallback="../fallback.svg" />
      },
    },
    {
      title: '标题',
      dataIndex: 'rules_name',
      hideInTable: true,
    },
    {
      title: "内容",
      dataIndex: "content",
      hideInTable: false,
      hideInForm: true,
      hideInSearch: true,
      render: (dom, entity) => {
        return (
          <div>
            <h3>{entity.rules_name}</h3>
            <hr color='#f0f0f0' />
            <div>考试时间：{entity.examination_time}分钟</div>
            <div>满分：{entity.sum_score}分</div>
            <div>及格：{entity.pass_score}分</div>
          </div>
        )
      },
    },
    {
      title: '考试时间',
      key: 'examination_time',
      dataIndex: 'examination_time',
      valueType: 'text',
      hideInTable: true,
    },
    {
      title: '总分',
      key: 'sum_score',
      dataIndex: 'sum_score',
      valueType: 'text',
      hideInTable: true,
    },
    {
      title: '及格',
      key: 'pass_score',
      dataIndex: 'pass_score',
      valueType: 'text',
      hideInTable: true,
    },

    {
      title: '操作',
      valueType: 'option',
      width: "10%",
      render: (text, record, _, action) => [
        <Button
          type="primary"
          key="editable"
          onClick={() => {

            runRulesPaper({ rules_id: record.id, time: record.examination_time })
            // history.push(`/mockExamination/index/paper/${record.id}/${record.examination_time}`);



          }}
        >
          开始考试
        </Button>,

      ],
    },
  ];
  const actionRef = useRef<ActionType>();
  const [list, setList] = useState(false)


  useEffect(() => {
    setList(false)
  }, [list])

  const { run: runRulesPaper } = useRequest(getRulesPaper,
    {
      manual: true,
      formatResult: (result) => {
        return result;
      },
      onSuccess: (result, params) => {
        let num = 0
        console.log(result, 'eee', params);

        if (result.success) {
          if (result.question_list.length > 0) {


            result.question_list[0].question_type_count.map((e) => {
              num += e.count
            })
          }
        }
        if (num > 0) {

          history.push(`/mockExamination/index/paper/${params[0].rules_id}/${params[0].time}`)
          // console.log(num, 'eee', params);
        } else {

          message.error("暂无试题。");
        }


      }
    });
  return (
    <PageContainer content={false} extraContent={false}>
      <div className={styles.cardList}>
        <ProTable
          autoDataLoad={false}
          columns={columns}
          showHeader={false}
          actionRef={actionRef}
          request={async (value) => {
            const promise = Promise;
            const RulesData = await getRulesList({
              ...value,
              b_use: 1,
              rules_type: 0
            })
            console.log(RulesData.table_List)
            const rulesList = RulesData.table_List
            rulesList.map(async (e, i) => {
              setList(false)
              const _data1 = await getSubjectInfo({
                subject_id: e.subject_id
              });
              const urlInfo = JSON.parse(JSON.stringify(_data1.data.attachment_json))
              rulesList[i].url = urlInfo.url
              console.log('urlInfo.url',urlInfo.url)
              setList(true)
            })
             return { data: rulesList }


          }}
          /*editable={{
            type: 'multiple',
          }}
          columnsState={{
            persistenceKey: 'pro-table-singe-demos',
            persistenceType: 'localStorage',
          }}*/
          search={false}
          rowKey="id"
          toolBarRender={false}
          pagination={{
            pageSize: 10,
          }}
        //dateFormatter="string"
        //headerTitle="高级表格"
        />
      </div>
    </PageContainer>
  );
};

export default SubjectList;
