import { Permission } from "@cqsjjb/jjb-common-decorator/permission";
import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Button, Space } from "antd";
import AddIcon from "zy-react-library/components/Icon/AddIcon";
import Page from "zy-react-library/components/Page";
import Search from "zy-react-library/components/Search";
import Table from "zy-react-library/components/Table";
import TooltipPreviewImg from "zy-react-library/components/TooltipPreviewImg";
import useTable from "zy-react-library/hooks/useTable";
import { getLabelName } from "zy-react-library/utils";
import { ENCLOSED_AREA_AUDIT_STATUS_ENUM } from "~/enumerate/constant";
import { NS_ENCLOSED_AREA_PERSONNEL_APPLY } from "~/enumerate/namespace";

function List(props) {
  const [form] = Search.useForm();
  const { tableProps, getData } = useTable(props["enclosedAreaPersonnelApplyList"], {
    form,
    params: () => {
      let personBelongType = 1;
      if (props.entrance === "enterprise")
        personBelongType = 2;
      if (props.entrance === "stakeholder")
        personBelongType = 3;
      return { processOrRecord: 1, personBelongType };
    },
  });

  return (
    <Page isShowAllAction={false}>
      <Search
        options={[
          { name: "applyPersonUserName", label: "人员姓名" },
        ]}
        form={form}
        onFinish={getData}
      />
      <Table
        toolBarRender={() => (
          <Space>
            {props.permission(props.addBtn || "jgd-enclosed-personnel-apply-add") && (
              <Button
                type="primary"
                icon={(<AddIcon />)}
                onClick={() => {
                  props.history.push("./add");
                }}
              >
                申请权限
              </Button>
            )}
          </Space>
        )}
        columns={[
          { title: "姓名", dataIndex: "applyPersonUserName" },
          { title: "部门", dataIndex: "applyPersonDepartmentName" },
          { title: "手机号", dataIndex: "userPhone" },
          { title: "申请区域", dataIndex: "closedAreaName" },
          {
            title: "申请时间范围",
            dataIndex: "visitStartTime",
            render: (_, record) => `${record.visitStartTime} - ${record.visitEndTime}`,
          },
          {
            title: "个人照片",
            dataIndex: "userFaceUrl",
            render: (_, record) => <TooltipPreviewImg files={[record.userFaceUrl]} />,
          },
          { title: "所属项目", dataIndex: "projectName", hidden: !(props.entrance === "stakeholder") },
          {
            title: "申请状态",
            dataIndex: "auditFlag",
            render: (_, record) => getLabelName({ list: ENCLOSED_AREA_AUDIT_STATUS_ENUM, status: record.auditFlag }),
          },
          {
            title: "操作",
            width: 80,
            fixed: "right",
            render: (_, record) => (
              <Space>
                {props.permission(props.viewBtn || "jgd-enclosed-personnel-apply-show") && (
                  <Button
                    type="link"
                    onClick={() => {
                      props.history.push(`./view?id=${record.id}`);
                    }}
                  >
                    查看
                  </Button>
                )}
              </Space>
            ),
          },
        ]}
        {...tableProps}
      />
    </Page>
  );
}

export default Connect([NS_ENCLOSED_AREA_PERSONNEL_APPLY], true)(Permission(List));
