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';
import Styles from '../style.less';


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);
    this.state = {
       dataSource: { records: [] },
       visibleData: { visible: false, customerId: '', realtyConsultant: '' },
    }
  }

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

  componentDidUpdate(preProps, preState) {
    console.log(this.props.visibleData.realtyConsultant)
    if (this.props.visibleData.customerId !== preState.visibleData.customerId) {
      this.getList({ pageNumber: 1, pageSize: 5 })
      this.setState({ visibleData: this.props.visibleData });
    }
  }

  // 弹框确定按钮
  // eslint-disable-next-line react/sort-comp
  handleOk() {
    this.setState({ visibleData: { visible: false, customerId: '', realtyConsultant: '' } })
  }

  // 弹框取消按钮
  handleCancel() {
    this.setState({ visibleData: { visible: false, customerId: '', realtyConsultant: '' } })
  }

  getList(params) {
    // 网路请求
    request({ ...apis.customer.consultant, params: { ...params } }).then(res => {
      this.setState({ dataSource: res })
    }).catch(err => {
      // eslint-disable-next-line no-unused-expressions
      <Alert
        style={{
          marginBottom: 24,
        }}
        message={err}
        type="error"
        showIcon
      />
    })
  }

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

   // 分页
  onChange(pageNum) {
    this.getList({ pageNumber: pageNum, pageSize: 5 })
  }

  // 提交
  submitGm(record) {
    const { url, method } = apis.customer.recommendEdit
    const tempUrl = url.substring(0, url.lastIndexOf('id')).concat(this.state.visibleData.customerId)

    // 网路请求
    request({ url: tempUrl, method, data: { customerId: this.state.visibleData.customerId, realtyConsultant: record.personId } }).then(res => {
      // eslint-disable-next-line no-unused-expressions
      this.openNotificationWithIcon('success', '操作成功')
      this.handleCancel()
    }).catch(err => {
      // eslint-disable-next-line no-unused-expressions
      this.openNotificationWithIcon('error', err)
    })
  }

  render() {
    const columns = [
      // {
      //   title: '编号',
      //   dataIndex: 'personId',
      //   key: 'personId',
      // },
      {
        title: '姓名',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '电话',
        dataIndex: 'phone',
        key: 'phone',
      },
      {
        title: '部门',
        dataIndex: 'department',
        key: 'department',
      },
      {
        title: '岗位',
        dataIndex: 'post',
        key: 'post',
      },
      {
        title: '操作',
        dataIndex: 'personId',
        key: 'personId',
        // eslint-disable-next-line no-nested-ternary
        render: (_, record) => <>{ this.props.visibleData.realtyConsultant !== record.personId && <Button type="danger" onClick={() => this.submitGm(record)}>确定</Button>}</>,
      },
    ]
    return (
      <>
        <Modal
            title="选择置业顾问"
            width={800}
            destroyOnClose="true"
            footer={null}
            visible={this.state.visibleData.visible}
            // onOk={() => this.handleOk()}
            onCancel={(e) => this.handleCancel(e)}
          >
            <Table dataSource={this.state.dataSource.records} columns={columns} pagination={{ total: this.state.dataSource.total, onChange: e => this.onChange(e) }} />
          </Modal>
      </>
    );
  }
}

export default ModalAttribution
