
import React, { useRef,useState,useEffect } from 'react'
import moment from 'moment';
import { useModel } from 'umi';
import { DatePicker,Input } from 'antd';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import PageTable from '@/components/PageTable'
import OrgSearch from '@/components/OrgSearch';
import { getDeleteformList } 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 deleteDateRef = useRef(moment())
  const { initialState } = useModel('@@initialState');
  const [orgName,setOrgName]=useState('')


  const handleBeforSearch = (params) => {
    const { createDate, deleteDate, ...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')
    }
    let deleteStart, deleteEnd;
    if (!deleteDate || deleteDate.length < 1) {
      // 默认时间是今天
      deleteStart = moment().format('YYYY-MM-DD')
      deleteEnd = deleteStart
    } else {
      deleteStart = moment(deleteDate[0]).format('YYYY-MM-DD')
      deleteEnd = moment(deleteDate[1]).format('YYYY-MM-DD')
    }

    return { ...others, start, end, deleteStart, deleteEnd };
  }
  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} />
      ),
    },
    {
      title: '企业名称',
      dataIndex: 'orgName',
      key: 'orgName',
      search: false,
    },
    {
      title: '操作人',
      dataIndex: 'deleteUserName',
      key: 'deleteUserName',
      search:false
    },
    {
      title: '操作时间',
      dataIndex: 'deleteDate',
      key: 'deleteDate',
      render: (t) => formatterTime(t),
      renderFormItem: (_, record) => <RangePicker defaultValue={[deleteDateRef.current, deleteDateRef.current]} placeholder={['开始日期', '结束日期']} format='YYYY-MM-DD' />
    },
    {
      title: '申请人',
      dataIndex: 'userName',
      key: 'userName',
    },
    {
      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
        columns={columns}
        request={getDeleteformList}
        options={false}
        beforeSearchSubmit={handleBeforSearch}
        rowKey="formId"
      />
    </PageHeaderWrapper>
  )
}
