import React, { PureComponent, useState, useEffect } from 'react'
import { Modal, Row, Col, Table, Pagination, Button, Form, Input,message } from 'antd'
import EnDash from '../EnDash'

import Style from './style.less'
import HotBlock from '../HotBlock'
import request from '../../../../../../utils/request';
import apis from '../../../../../../services/apis';
import AuthButton from '@/components/AuthButton';



const HouseListInfo = props => {
  const [data, setData] = useState([])
  const raiseRecordId = props.raiseRecordId
  useEffect(() => {
    console.log('raiseRecordIdraiseRecordId',raiseRecordId)
    getList({ pageNum: 1, pageSize: 10 });
  }, [props.raiseRecordId])

  function getList(params) {
    // 网路请求
    request({ ...apis.house.taHousingResources, params: { ...params, salesBatchId:props.salesBatchId, lockingStatus:'unlocked', buildingId:props.buildingId} }).then(res => {
      setData(res)
    }).catch(err => {
      // openNotificationWithIcon('error', err.message)
    })
  }

  const toAddHouse = rowData => () =>{
    console.log(rowData,'record');
    request({ ...apis.house.addUnlockRaiseHouse, params: {houseId: rowData.houseId, raiseRecordId: raiseRecordId, lockingStatus:'unlocked'} }).then(res => {
      message.info("添加成功");
      props.onCancel();
    }).catch(err => {
      // openNotificationWithIcon('error', err.message)
    })
  }

  const changePageNum = (pageNumber) => {
    getList({ pageNum: pageNumber, pageSize: 10, ...props.form.getFieldsValue() })
  }


  const columns = [
    {
      title: '房源编号',
      dataIndex: 'houseId',
      key: 'houseId',
      align: 'center',
    },
    {
      title: '房源',
      dataIndex: 'room',
      key: 'room',
      align: 'center',
      render: (text, records) => <span>{records.termname}/{records.blockName}/{records.unitName}/{records.floorName}/{records.roomName}</span>,
    },
    {
      title: '价格(万元)',
      dataIndex: 'price',
      key: 'price',
      align: 'center',
    },
    {
      title: '对应户型',
      dataIndex: 'apartmentName',
      key: 'apartmentName',
    },
    {
      title: '预选实际热度',
      dataIndex: 'realHeat',
      key: 'realHeat',
    },
    {
      title: '认筹实际热度',
      dataIndex: 'raiseRealHeat',
      key: 'raiseRealHeat',
    },
    {
      title: '操作',
      dataIndex: '',
      key: '',
      align: 'center',
      render: (text, record) => (
        <AuthButton name="admin.salesBatch.detail.get" noRight={null}>
          <a style={{ color: '#66B3FF' }} onClick={toAddHouse(record)} >添加</a>
        </AuthButton>
      ),
    },
  ];

  function handleSubmit (e) {
    e.preventDefault();
    props.form.validateFields((err, values) => {
      if (!err){
        getList({ pageNum: 1, pageSize: 10, ...values })
      }
    });
  }

  // 重置搜索
  function handleReset () {
    props.form.resetFields();
  }

  const { getFieldDecorator } = props.form;

  return (
    <>
      <Modal footer={null} title="添加房源" visible={props.visible} onCancel={props.onCancel} width={600}>
        <span>可选房源仅包括当前销售批次中已发布且未被锁定的房源</span>
        <Form layout="inline" onSubmit={handleSubmit}>
            <Form.Item>
              {getFieldDecorator('houseId')(
                <Input placeholder="房源编号" style={{width:'150px'}} />,
              )}
            </Form.Item>
            <Form.Item>
              {getFieldDecorator('termName')(
                <Input placeholder="期区" style={{width:'150px'}}/>,
              )}
            </Form.Item>
            <Form.Item>
              {getFieldDecorator('blockName')(
                <Input placeholder="楼栋" style={{width:'150px'}}/>,
              )}
            </Form.Item>
            <Form.Item>
              {getFieldDecorator('unitName')(
                <Input placeholder="单元" style={{width:'150px'}}/>,
              )}
            </Form.Item>
            <Form.Item>
              {getFieldDecorator('floorName')(
                <Input placeholder="层" style={{width:'150px'}}/>,
              )}
            </Form.Item>
            <Form.Item>
              {getFieldDecorator('roomName')(
                <Input placeholder="户号" style={{width:'150px'}}/>,
              )}
            </Form.Item>
            <Form.Item>
              <Button type="primary" htmlType="submit" >
                搜索
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={handleReset}>
                重置
              </Button>
          </Form.Item>
        </Form>
        <Table dataSource={data.records} columns={columns} pagination={false} rowKey="carouseFigureList"/>
        <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
          <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={changePageNum} current={data.current}/>
        </div>      
      </Modal>
    </>
  );
}

const WrappedBase = Form.create({ name: 'HouseListInfo' })(HouseListInfo);

export default WrappedBase
