import React, { useRef,useEffect,useState } from 'react'
import { useModel } from 'umi';
import moment from 'moment';
import { Badge, Image, DatePicker,Button,Popconfirm,message,Input } from 'antd';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import PageTable from '@/components/PageTable'
import OrgSearch from '@/components/OrgSearch';
import { getList, exportList,deleteworkForm } from '@/services/work'
import { getDetail } from '@/services/org';

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 { initialState } = useModel('@@initialState');
  const [orgName,setOrgName]=useState('')

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

    let start, end,orgId;
    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')
    }
    if (initialState.currentUser.orgId) {
      orgId=initialState.currentUser.orgId
      return { ...others, start, end,orgId };
    }
    return { ...others, start, end };
  }

  const handleDelete=(val)=>{
    deleteworkForm(val).then(res=>{
      message.success('删除成功');
      actionRef.current.reload();
    }).catch(err=>{
      message.error(err.message || err);
    })
  }
  useEffect(()=>{
    if (initialState.currentUser.orgId) {
      getDetail(initialState.currentUser.orgId).then((res)=>{
        setOrgName(res.orgName);
      })
    }
  },[initialState.currentUser.orgId])
  
  const columns = [
    {
      title: '企业名称',
      dataIndex: 'orgId',
      key: 'orgId',
      search: true,
      hideInTable:true,
      renderFormItem: () => (
        !initialState.currentUser.orgId?
        <OrgSearch placeholder='请输入企业名称' />:<Input readOnly value={orgName} />
      ),
      formItemProps: !initialState.currentUser.orgId?{
        rules: [          
          {
            required: true,
            message: '请输入企业名称',
          }
        ],
      }:false,
    },
    {
      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' />
    },
    {
      title: '操作',
      valueType: 'option',
      width: 160,
      render: (_, record) => [
        <Popconfirm
          key={4}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record.formId)}
          okText="确定"
          cancelText="取消"
        >
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>,
      ],
    },
  ]


  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>
  )
}
