import { Permission } from "@cqsjjb/jjb-common-decorator/permission";
import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Button, Descriptions, Modal, Space } from "antd";
import { useState } from "react";
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 [refusalReason, setCurrentRefusalReason] = useState("");
  const [viewRejectReasonModalVisible, setViewRejectReasonModalVisible] = useState(false);

  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: 2, personBelongType };
    },
  });

  return (
    <Page isShowAllAction={false}>
      <Search
        options={[
          { name: "applyPersonUserName", label: "人员姓名" },
        ]}
        form={form}
        onFinish={getData}
      />
      <Table
        columns={[
          { title: "所属企业", dataIndex: "applyPersonCorpName" },
          { 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: 150,
            fixed: "right",
            render: (_, record) => (
              <Space>
                {props.permission(props.viewBtn || "jgd-enclosed-personnel-records-show") && (
                  <Button
                    type="link"
                    onClick={() => {
                      props.history.push(`./view?id=${record.id}`);
                    }}
                  >
                    查看
                  </Button>
                )}
                {(record.auditFlag === 3 && props.permission(props.rejectReasonBtn || "jgd-enclosed-personnel-records-bh")) && (
                  <Button
                    type="link"
                    onClick={() => {
                      setViewRejectReasonModalVisible(true);
                      setCurrentRefusalReason(record.refusalReason);
                    }}
                  >
                    驳回原因
                  </Button>
                )}
              </Space>
            ),
          },
        ]}
        {...tableProps}
      />
      {
        viewRejectReasonModalVisible && (
          <ViewRejectReasonModal
            refusalReason={refusalReason}
            onCancel={() => {
              setViewRejectReasonModalVisible(false);
              setCurrentRefusalReason("");
            }}
          />
        )
      }
    </Page>
  );
}

const ViewRejectReasonModal = (props) => {
  return (
    <Modal
      open
      title="驳回原因"
      maskClosable={false}
      onCancel={props.onCancel}
      width={600}
      footer={[
        <Button key="cancel" onClick={props.onCancel}>取消</Button>,
      ]}
    >
      <Descriptions
        column={1}
        bordered
        styles={{ label: { width: 200 } }}
        items={[
          { label: "驳回原因", children: props.refusalReason },
        ]}
      />
    </Modal>
  );
};

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