import React, { useState, useEffect } from 'react';
import styles from './css/index.less';
import { Input ,Menu,Table,Space,Upload,message,Pagination, Button } from 'antd';

import { Link, useIntl, connect, Dispatch,history } from 'umi';

import { timestampToTime3 } from '../../utils/time';

import TitleSearch from '../../components/TitleSearch/TitleSearch';
import { RA, urlEncode, filterObj, filterObjbyTg } from '@/utils/method';


const pathname = ["CommunityMaintenance", "ShelfLifeService", "OwnerComplaints", "ReportOnline", "ProblemFeedback"]
const handle_Status = [[0,"Pending"], [1,"Processed"]]//["未处理","处理","拒绝"] , [2,"Rejected"]
const goToName = ["Reply", "Detail"];

const module="CommunityService"
const PropertyServices = (props: any) => {

  const { dispatch, location, Data } = props;

  const [loading, setLoading] = useState(false)
  const [pageNum, setPageNum] = useState(1)
  const [serviceTypeGobal, setServiceType] = useState("")
  const [readyData,setReadyData]=useState(null)

  const columns = [
    { title: "Community Name",dataIndex: 'community_name',},
    { title: "Ower Name",dataIndex: 'ower_name',},
    { title: "Reply",dataIndex: 'handle_name',render: (text: any) => (<Space size="middle">{text!=null?text:"--"}</Space>)},
    { title: "Create Time",dataIndex: "create_time",render: (text: any, record: any) => (<Space size="middle">{timestampToTime3(text.time)}</Space>)},
    { title: "Handle status", dataIndex: 'handle_status',render: (text: any, record: any) => (<Space size="middle">{handle_Status[text][1]}</Space>)},
    { title: "Actions",dataIndex: 'handle_status',render: (text: any, record: any) => (<Space size="middle"><a onClick={goToDetail.bind(this,text,record)}>{goToName[text]}</a></Space>)},
  ];

  const goToDetail = (value: any, record: any, e: any) => {
    console.log(value);
    console.log(serviceTypeGobal);
    console.log(record);
    //页面递进
    history.push(
      location.pathname+(value==0?"/Edit":"/Detail")+urlEncode(
        {
          serviceType: serviceTypeGobal,
          id: record.id,
          user_id:record.user_id,
          community_id:record.community_id
        }
        )
    )
  }

  useEffect(() => {
    if (Data != null) {setLoading(false)}
  }, [Data]);

  const ClickTitleSearch = (comment: any) => {
    //页面搜索
    comment.serviceType = serviceTypeGobal
    comment.current = 1
    history.push(location.pathname+urlEncode(filterObj(comment)))
  }
  const PageChange = (values: any) => { //.current
    //页面跳转参数
    var tmp = filterObjbyTg(location.query, ["handleStatus", "ownerName", "communityName"])
    tmp["current"]=values.current
    history.push(location.pathname + urlEncode(tmp))
  }

  useEffect(()=>{
    if (location != null) {
      //细分类：页面指令
      var serviceType =""
      pathname.map((item, index) => {
        if ((location.pathname).indexOf(item) > -1) {
          serviceType = "" + (index + 1)
          setServiceType(serviceType)
        }
      })
      //分页设置
      if (location.query.current!=null) {
        setPageNum(parseInt(location.query.current))
      } else {
        setPageNum(1)
      }

      //前置数据
      console.log("前置数据");
      var tmp = filterObjbyTg(location.query, ["handleStatus", "ownerName", "communityName"])
      tmp.handleStatus = handle_Status[parseInt(tmp.handleStatus)] //特殊处理
      setReadyData(tmp)

      //获取数据
      setLoading(true)
      console.log(serviceType)
      console.log(location.query)
      RA(40, {
        serviceType: serviceType,
        handleStatus: location.query.handleStatus != undefined ? parseInt(location.query.handleStatus) : null,
        ownerName: location.query.ownerName != undefined ? (location.query.ownerName) : null,
        communityName: location.query.communityName != undefined ? (location.query.communityName) : null,
      }, module, dispatch)

    }
  }, [location])



  return (
    <div className={styles.base}>

      <TitleSearch
        communitySelect={true}
        listkey={['ownerName']}
        list={['Owner Name']}
        status={[{ name: ["handleStatus", "Status"], data: handle_Status }]}
        defaultValue={readyData}
        onSubmit={ClickTitleSearch}
      />
        <Table
          rowKey={"id"}
          style={{ marginTop: 16 }}
          loading={loading}
          columns={columns}
          dataSource={ Data != null ?Data.data.rows:[]}
          pagination={Data != null ? { current: pageNum, total: Data.data.rows.length } : {}}
          onChange={PageChange}
        />
    </div>
  );
};

function mapStateToProps(state:any) {
  const { Data } = state.CommunityService;
  return {
    Data
  };
}
export default connect(mapStateToProps)(PropertyServices);
