import React, { useState, useEffect } from 'react';
import { Form, Icon, Input, Button, DatePicker, Select, Card, Row, Col, Pagination, Alert, Table, Avatar, Radio, Modal, Descriptions, notification } from 'antd';
import moment from 'moment';
import request from '../../../utils/request';
import apis from '../../../services/apis';


const { Option } = Select;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { Meta } = Card;

/**
 * 分配置业顾问
 *
 * @param {*} props
 * @returns
 */
class ModalAttribution extends React.Component {
  constructor(props) {
    super(props);
    // console.log(props, 'props')
    this.state = {
      dataSource: { records: [] },
      visibleData: { visible: false, buildingId: '' },
    }
  }

  // 挂载之后
  componentDidMount() {
    // this.getList({ pageNumber: 1, pageSize: 5 })
  }

  componentDidUpdate(preProps, preState) {
    if (this.props.visible !== preState.visibleData.visible) {
      this.getUserList({ pageNumber: 1, pageSize: 5, buildingId: this.props.buildingId })
      this.setState({ visibleData: { visible: this.props.visible, buildingId: this.props.buildingId } });
    }
  }

  // 弹框取消按钮
  handleCancel() {
    this.props.onCancel()
  }

  getUserList(params) {
    console.log('params: ', params)
    if (params.buildingId === '' || params.buildingId === null || params.buildingId === undefined) {
      return
    }
    // 网路请求
    request({ ...apis.customer.buildingConsultant, params: { ...params } }).then(res => {
      this.setState({ dataSource: res })
    }).catch(err => {

    })
  }

  openNotificationWithIcon = (type, message) => {
    notification[type]({
      message,
      description:
        '',
    });
  };

  // 分页
  onChange(pageNum) {

    this.getUserList({ pageNumber: pageNum, pageSize: 5, buildingId: this.state.visibleData.buildingId })
  }

  // 提交
  submitGm(record) {
    this.props.onCancel(record)
  }

  render() {
    const columns = [
      {
        title: '姓名',
        dataIndex: 'userName',
        key: 'userName',
        width: 120,
      },
      {
        title: '电话',
        dataIndex: 'phone',
        key: 'phone',
        width: 150,
      },
      {
        title: '部门',
        dataIndex: 'department',
        key: 'department',
        width: 130,
      },
      {
        title: '岗位',
        dataIndex: 'position',
        key: 'position',
        width: 120,
      },
      {
        title: '操作',
        dataIndex: 'personId',
        key: 'personId',
        width: 80,
        // eslint-disable-next-line no-nested-ternary
        render: (_, record) => <>{record.userId !== this.props.userId ? <Button type="danger" onClick={() => this.submitGm(record)}>确定</Button> : ''}</>,
      },
    ]
    return (
      <>
        <Modal
          title="分配置业顾问"
          width={600}
          destroyOnClose="true"
          footer={null}
          visible={this.state.visibleData.visible}
          onCancel={(e) => this.handleCancel(e)}
        >
          <Table rowKey="userId" dataSource={this.state.dataSource.records} columns={columns} pagination={{ pageSize: 5, total: this.state.dataSource.total, onChange: e => this.onChange(e) }} />
        </Modal>
      </>
    );
  }
}

export default ModalAttribution
