
import React, { useRef } from 'react'
import moment from 'moment';
import { Badge, Image, DatePicker } from 'antd';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { getList, exportList } from '@/services/work'
import PageTable from '@/components/PageTable'
import OrgSearch from '@/components/OrgSearch';

const { RangePicker } = DatePicker;

const formatterTime = (val) => {
  return val ? moment(val).format('YYYY-MM-DD HH:mm') : '';
};

export default (props) => {
  const initDate = useRef(moment())
  const actionRef = useRef();

  const handleBeforSearch = (params) => {
    const { createDate, ...others } = params;

    let start, end;
    if (!createDate || createDate.length < 1) {
      // 默认时间是今天
      start = moment().format('YYYY-MM-DD')
      end = start
    } else {
      start = moment(createDate[0]).format('YYYY-MM-DD')
      end = moment(createDate[1]).format('YYYY-MM-DD')
    }

    return { ...others, start, end };
  }

  const columns = [
    {
      title: '企业名称',
      dataIndex: 'orgId',
      key: 'orgId',
      search: true,
      hideInTable:true,
      renderFormItem: () => (
        <OrgSearch placeholder='请输入企业名称' />
      )
    },
    {
      title: '企业名称',
      dataIndex: 'orgName',
      key: 'orgName',
      search: false,      
    },
    {
      title: '申请人',
      dataIndex: 'userName',
      key: 'userName',
      search: true,
    },
    {
      title: '抗原截图',
      dataIndex: 'antigenImage',
      key: 'antigenImage',
      search: false,
      render: t => {t?<Image src={t} width="64px" height="64px" />:''}
    },
    {
      title: '抗原结果',
      dataIndex: 'antigenIsNormal',
      key: 'antigenIsNormal',
      search: false,
      render: t => (
        <span>
          {t ? <Badge status="success" text="正常" /> : <Badge status="error" text="异常" />}
        </span>
      )
    },
    {
      title: '核酸截图',
      dataIndex: 'nucleicImage',
      key: 'nucleicImage',
      search: false,
      render: t => <Image src={t} width="64px" height="64px" />
    },
    {
      title: '核酸结果',
      dataIndex: 'nucleicIsNormal',
      key: 'nucleicIsNormal',
      search: false,
      render: t => (
        <span>
        {t ? <Badge status="success" text="正常" /> : <Badge status="error" text="异常" />}
        </span>
      )
    },
    {
      title: '申请时间',
      dataIndex: 'createDate',
      key: 'createDate',
      render: (t) => formatterTime(t),
      renderFormItem: (_, record) => <RangePicker defaultValue={[initDate.current, initDate.current]} placeholder={['开始日期', '结束日期']} format='YYYY-MM-DD' />
    },
  ]


  return (
    <PageHeaderWrapper>
      <PageTable
        actionRef={actionRef}
        columns={columns}
        request={getList}
        expfunc={exportList}
        options={false}
        form={{ ignoreRules: false }}
        manualRequest={true}
        revalidateOnFocus={false}
        beforeSearchSubmit={handleBeforSearch}
        rowKey="formId"
      />
    </PageHeaderWrapper>
  )
}
