import React, { useCallback, useEffect, useState } from 'react';
import { Input, Button, Form } from 'antd';

import ItemPublicityComplain from '../components/itemPublicityComplain';
import StatusNav from '../../components/statusNav';
import { publicityArr } from '../static';
import { publicityComplainList, } from '../api';
import '../index.scss';

export default Form.create()(({ current_user, form, showNotification, match, history }) => {
  const { getFieldDecorator, validateFields, setFieldsValue } = form;
  const [approve, setApprove] = useState(1);
  const [loading, setLoading] = useState(false);
  const [searchObj, setSearchObj] = useState({});
  const [status, setStatus] = useState('2');
  const [curPage, setCurPage] = useState(1);
  const [total, setTotal] = useState(0);
  const [taskList, setTaskList] = useState([]);

  const [reload, setReload] = useState(0);

  useEffect(() => {
    const params = {
      ...searchObj,
      status,
      orderBy: 'createdAtDesc',
      curPage,
      pageSize: 10,
    };
    setLoading(true);
    publicityComplainList(params).then(data => {
      if (data) {
        setTaskList(data.rows);
        setTotal(data.total);
      }
      setLoading(false);
    })
  }, [reload, status, curPage, searchObj]);


  const helper = useCallback(
    (name, rules, widget) => (
      <Form.Item>
        {getFieldDecorator(name, { rules, validateFirst: true, })(widget)}
      </Form.Item>
    ), []);


  function onSearch() {
    validateFields((err, values) => {
      if (!err) {
        setSearchObj(values);
      }
    });
  }

  const changeOptionId = useCallback((option) => {
    setStatus(option.dicItemCode.toString() || '0,1');
    setCurPage(1);
  }, []);


  function changeApprove(approve) {
    setApprove(approve);
    setCurPage(1);
    if (approve === 1) {
      setStatus('2');
    } else {
      setStatus('0,1');
    }
  }

  function clearSearch() {
    setFieldsValue({
      numberInput: '',
    });
    setSearchObj({});
  }

  const reloadList = useCallback(() => {
    setReload(Math.random());
  }, [])

  return (
    <div className="centerbox task-manage">
      
      <div className="center-screen" >
        <div className="center-left-but">
          <Button className="circle-button" type={approve === 1 ? 'primary' : ''} onClick={() => { changeApprove(1) }}>待审批</Button>
          <Button className="circle-button" type={approve === 2 ? 'primary' : ''} onClick={() => { changeApprove(2) }}>已审批</Button>
        </div>

        <div className="center-right-but">
          {helper(
            "numberInput",
            [{ max: 20, message: '长度不能超过20个字符' }],
            <Input
              placeholder="输入成果编号进行检索"
            />
          )}
          <Button className="mr10" type="primary" onClick={onSearch}>搜索</Button>
          <Button className="mr10" type="" onClick={clearSearch}>清除</Button>
        </div>
      </div>


      <div className="center-content">
        {
          approve === 2 && <StatusNav
            key={'status'}
            type={'status'}
            options={publicityArr}
            changeOptionId={changeOptionId}
          />
        }
        <ItemPublicityComplain
          list={taskList}
          curPage={curPage}
          total={total}
          changePage={(page) => { setCurPage(page) }}
          loading={loading}
          showNotification={showNotification}
          reloadList={reloadList}
        />
      </div>

    </div>
  )
}
)