import React, { useState, useRef } from 'react';
import Table, { ProColumns, ActionType } from '@/components/Table';
import {
  getAccountList,
  deleteUser,
  updateUser,
  changeUserStatus,
} from '@/services/system/accountManage';
import { fetchTableData } from '@/utils/table';
import DeleteButton from '@/components/Table/DeleteButton';
import { Popover, Switch } from 'antd';
import AddAccountModal from '../components/AddAccountModal';
import EditAccountModal from '../components/EditAccountModal';
import { getRoleList } from '@/services/system/role';
import { Access, useAccess } from 'umi';

const AccountManageList = () => {
  const [isModalVisible, setIsModalVisible] = useState(false);
  const [isEditModal, setIsEditModal] = useState(false);
  const [modalData, setModalData] = useState({});

  const handleEdit = (row: any) => {
    setModalData(row);
    setIsEditModal(true);
  };

  const handleDelete = async (name: any) => {
    await deleteUser({ name: name });
  };
  const tableRef = useRef<ActionType>();
  const access = useAccess();

  const columns: ProColumns<any>[] = [
    {
      title: '账号',
      dataIndex: 'name',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '角色',
      dataIndex: 'role',
      hideInSearch: true,
      valueType: 'select',
      request: async () => {
        const res = await getRoleList({ page: 1, size: 10 });
        const options = new Array();
        res.items.forEach((element) => {
          options.push({ label: element.role_name, value: element.id });
        });
        return options;
      },
    },
    {
      title: '操作',
      valueType: 'option',
      width: 180,
      render: (_, row) => [
        <Access accessible={access.canShowButton('账号编辑')}>
          <a
            key="edit"
            placeholder=""
            onClick={() => {
              handleEdit(row);
            }}
          >
            编辑
          </a>
        </Access>,
        <Access accessible={access.canShowButton('账号启用禁用')}>
          <Popover
            content={
              <div>
                <p>账号启用/禁用</p>
              </div>
            }
          >
            <Switch
              key="switch"
              defaultChecked={row.status}
              size="small"
              onChange={async (checked) => {
                await changeUserStatus({ name: row.name, status: checked });
              }}
            />
          </Popover>
        </Access>,
        <Access accessible={access.canShowButton('账号删除')}>
          <DeleteButton
            key="delete"
            onDelete={() => {
              handleDelete(row.name);
              tableRef.current?.reload();
            }}
          />
        </Access>,
      ],
    },
  ];
  return (
    <div>
      <Table
        columns={columns}
        rowKey="name"
        search={false}
        toolBarActions={[
          {
            type: 'add',
            permission: '账号添加',
            onConfirm: () => {
              setIsModalVisible(true);
            },
          },
        ]}
        actionRef={tableRef}
        request={async (params) => {
          const res = await fetchTableData(getAccountList, params);
          return res;
        }}
      />
      <AddAccountModal
        visible={isModalVisible}
        onCancel={function () {
          setIsModalVisible(false);
        }}
        onOk={async function (): Promise<void> {
          setIsModalVisible(false);
          tableRef.current?.reload();
        }}
      />
      <EditAccountModal
        visible={isEditModal}
        editModalData={modalData}
        onCancel={function () {
          setIsEditModal(false);
        }}
        onOk={async function (): Promise<void> {
          setIsEditModal(false);
          tableRef.current?.reload();
        }}
      />
    </div>
  );
};

export default AccountManageList;
