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 useTable from "zy-react-library/hooks/useTable";
import { getLabelName } from "zy-react-library/utils";
import { ENCLOSED_AREA_AUDIT_STATUS_ENUM, ENCLOSED_AREA_BELONGING_END_ENUM } from "~/enumerate/constant";
import { NS_ENCLOSED_AREA_PERSONNEL_APPLY } from "~/enumerate/namespace";

function List(props) {
  const [viewRejectReasonModalVisible, setViewRejectReasonModalVisible] = useState(false);
  const [refusalReason, setCurrentRefusalReason] = useState("");

  const [form] = Search.useForm();

  const { tableProps, getData } = useTable(props["enclosedAreaPersonnelApplyList"], {
    form,
    params: { processOrRecord: 2, personBelongType: -1 },
  });

  return (
    <Page isShowAllAction={false}>
      <Search
        options={[
          { name: "applyPersonUserName", label: "人员姓名" },
        ]}
        form={form}
        onFinish={getData}
      />
      <Table
        columns={[
          {
            title: "申请来源",
            dataIndex: "personBelongType",
            render: (_, record) => getLabelName({ list: ENCLOSED_AREA_BELONGING_END_ENUM, status: record.personBelongType }),
          },
          { 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: "entourage",
            render: (_, record) => record.entourage ? JSON.parse(record.entourage).length : 0,
          },
          {
            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("jgd-ent-long-personnel-records-show") && (
                  <Button
                    type="link"
                    onClick={() => {
                      const entrance = ENCLOSED_AREA_BELONGING_END_ENUM.find(item => +item.bianma === record.personBelongType).bianma;
                      props.history.push(`./view?id=${record.id}&entrance=${entrance}`);
                    }}
                  >
                    查看
                  </Button>
                )}
                {(record.auditFlag === 3 && props.permission("jgd-ent-long-personnel-records-refusal-reason")) && (
                  <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));
