import React from "react";
import { useNavigate } from "react-router-dom";
import { queryTable, queryDict } from "@/utils/request";
import Page from "@/components/Page";
import { ProTable } from "@ant-design/pro-components";
import { Button, message, Popconfirm } from "antd";
import { getSysPosition, deleteSysPosition } from "@/service/sysposition";
import { getSysOrg } from "@/service/sysorg";

const querySysPositionList = queryTable(getSysPosition);
const queryOrg = queryDict(getSysOrg, { labelKey: "name", valueKey: "orgId" });

export default (props) => {
  const actionRef = React.useRef();
  const navigate = useNavigate();

  const handleDelete = (id) => {
    if (id) {
      deleteSysPosition(id).then((res) => {
        actionRef.current.reload();
      });
    }
  };

  const columns = [
    {
      title: "岗位名称",
      dataIndex: "name",
    },
    {
      title: "所属机构",
      dataIndex: "orgId",
      valueType: "select",
      request: queryOrg,
    },
    {
      title: "状态",
      dataIndex: "status",
      search: false,
      valueEnum: {
        1: {
          text: "正常",
          status: "Processing",
        },
        0: {
          text: "禁用",
          status: "Error",
        },
      },
    },
    {
      title: "操作",
      valueType: "option",
      width: 200,
      render: (_, record) => [
        // <Button
        //   key={1}
        //   style={{ padding: 0 }}
        //   type="link"
        //   onClick={() => {
        //     updateStatus(record);
        //   }}
        // >
        //   {record.status === 1 ? "禁用" : "启用"}
        // </Button>,
        <Button
          key={2}
          style={{ padding: 0 }}
          type="link"
          onClick={() => {
            navigate(`/system/position/edit?id=${record.positionId}`);
          }}
        >
          编辑
        </Button>,
        <Popconfirm
          key={3}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record.positionId)}
          okText="确定"
          cancelText="取消"
        >
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>,
      ],
    },
  ];

  return (
    <Page>
      <ProTable
        actionRef={actionRef}
        rowKey="positionId"
        toolBarRender={() => [
          <Button
            key="1"
            type="primary"
            onClick={() => {
              navigate("/system/position/edit");
            }}
          >
            新增
          </Button>,
        ]}
        request={querySysPositionList}
        columns={columns}
      />
    </Page>
  );
};
