import React from 'react';
import { Button, Modal, notification } from 'antd';
import request from '@/utils/request';
import apis from '@/services/apis';
import TableList from '@/components/TableList';

const Attribution = props => {
  console.log(props, 'props');

  const { visible, data } = props.modelProps || {};

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

  // 提交
  function submitGm(record) {
    // 网路请求
    request({
      ...apis.customer.recommendEdit,
      urlData: { id: data.customerId },
      data: { customerId: data.customerId, realtyConsultant: record.userId },
    })
      .then(res => {
        // eslint-disable-next-line no-unused-expressions
        openNotificationWithIcon('success', '操作成功');
        props.handleCancel();
      })
      .catch(err => {
        // eslint-disable-next-line no-unused-expressions
        openNotificationWithIcon('error', err);
      });
  }

  const columns = [
    {
      title: '姓名',
      dataIndex: 'userName',
      key: 'userName',
    },
    {
      title: '电话',
      dataIndex: 'phone',
      key: 'phone',
    },
    {
      title: '部门',
      dataIndex: 'department',
      key: 'department',
    },
    {
      title: '岗位',
      dataIndex: 'position',
      key: 'position',
    },
    {
      title: '操作',
      dataIndex: 'personId',
      key: 'personId',
      render: (_, record) => (
        <>
          {data.realtyConsultant != record.userId && (
            <Button type="primary" onClick={() => submitGm(record)}>
              确定
            </Button>
          )}
        </>
      ),
    },
  ];

  return (
    <>
      <Modal
        title="变更状态"
        width={800}
        destroyOnClose="true"
        footer={null}
        visible={visible}
        onCancel={() => props.onCancel()}
      >
        {data && (
          <TableList
            rowKey="userId"
            api={apis.customer.buildingConsultant}
            params={{ buildingId: data.buildingId }}
            columns={columns}
          />
        )}
      </Modal>
    </>
  );
};

export default Attribution;
