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

import Style from './style.less'
import HotBlock from '../HotBlock'
import HouseListInfo from '../HouseListInfo'
import request from '../../../../../../utils/request';
import apis from '../../../../../../services/apis';

const HouseInfo = props => {
  const [data, setData] = useState({ list: [], total: 0 })
  const [raiseData, setRaiseData ] = useState({})
  const [house, setHouse] = useState({visable:false, salesBatchId:'',buildingId:'', raiseRecordId:''});
  const raiseRecordId = props.raiseRecordId
  const salesBatchId = props.salesBatchId
  const buildingId = props.buildingId
  const raiseId = props.raiseId

  useEffect(() => {
    getList({ pageNum: 1, pageSize: 10 });
    getRaiseInfo();
  }, [raiseRecordId])

  function getList(params) {
    // 网路请求
    request({ ...apis.house.listHouseByRaiseRecordId, params: { ...params,raiseRecordId: raiseRecordId} }).then(res => {
      setData(res)
    }).catch(err => {
      // openNotificationWithIcon('error', err.message)
    })
  }

  function getRaiseInfo(){
    request({ ...apis.house.taRaiseById, urlData: {id: raiseId} }).then((data) => {
      setRaiseData(data)
      console.log(raiseData, 'raiseDataraiseDataraiseData')
    })
  }

  function refreshList(e){
    setHouse(e)
    getList()
  }

  function infoOnCancel(e){
    console.log(12321344)
    props.onCancel();
  }

  //锁定和解锁
  const updateLockingStatus = rowData => () => {
    console.log(rowData,'12321323')
    const type = rowData.houseLockingStatus === 'locked' ? "unlocked":"locked"
    request({ ...apis.house.updateLockingStatus, params: {houseId:rowData.houseId, raiseHouseId:rowData.raiseHouseId, personId:rowData.lockingPersonId, type:type, raiseRecordId:raiseRecordId} }).then(res => {
      message.info("操作成功")
      props.onCancel(false)
      getList();
    }).catch(err => {
    })
  }


  const columns = [
    {
      title: '房源编号',
      dataIndex: 'houseId',
      key: 'houseId',
      align: 'center',
    },
    {
      title: '房源',
      dataIndex: 'buildingName',
      key: 'buildingName',
      align: 'center',
      render: (text, records) => <span>{records.termName}/{records.blockName}/{records.unitName}/{records.floorName}/{records.roomName}</span>,
    },
    {
      title: '预选实际人数',
      dataIndex: 'realHeat',
      key: 'realHeat',
      align: 'center',
    },
    {
      title: '认筹实际人数',
      dataIndex: 'raiseRealHeat',
      key: 'raiseRealHeat',
      align: 'center',
    },
    {
      title: '锁定状态',
      dataIndex: 'houseLockingStatus',
      key: 'houseLockingStatus',
      align: 'center',
      render: (x, row) => <><span>{row.houseLockingStatus === 'locked'?'已锁定':'未锁定'}</span></>,
    },
    {
      title: '锁定人',
      dataIndex: 'lockingPersonName',
      key: 'lockingPersonName',
      align: 'center',
      render: (x, row) => <><span>{row.houseLockingStatus === 'locked'? row.lockingPersonName :''}</span></>,
    },
    {
      title: '操作',
      dataIndex: '',
      key: '',
      align: 'center',
      render: (text, record) => (
        <AuthButton name="admin.salesBatch.detail.get" noRight={null}>
          <a style={{ color: '#66B3FF' }} onClick={updateLockingStatus(record)} >{record.houseLockingStatus === 'locked' ? '解锁':'锁定'}</a>
        </AuthButton>
      ),
    },
  ];

  return (
    <>
      <Modal footer={null} title="认筹单房源清单" visible={props.visible} onCancel={() => infoOnCancel()} width={800}>
        <AuthButton noRight={null}>
          {raiseData.houseLockingType === 'auto' ? null : <Button type="danger" style={{marginLeft:'663px'}} onClick={() => setHouse({visable:true, salesBatchId:salesBatchId, raiseRecordId:raiseRecordId, buildingId:buildingId})}>添加房源</Button>}
        </AuthButton>
        <Table dataSource={data.records} columns={columns} pagination={false} rowKey="activityList" />
        <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: '30px' }}>
          <Pagination showQuickJumper defaultCurrent={1} total={data.total} onChange={e => changePageNum(e)} current={data.current}/>
        </div>
      </Modal>
      <HouseListInfo visible={house.visable} salesBatchId={house.salesBatchId} buildingId={house.buildingId} raiseRecordId = {house.raiseRecordId} onCancel={() => refreshList({visable:false, salesBatchId:salesBatchId, buildingId:''})}/>
    </>
  );
}


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

export default WrappedBase

// export default function UserInfo(props) {
// }
