import { Permission } from "@cqsjjb/jjb-common-decorator/permission";
import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Button, Space } from "antd";
import { useState } from "react";
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 { UPLOAD_FILE_TYPE_ENUM } from "zy-react-library/enum/uploadFile/gwj";
import useGetFile from "zy-react-library/hooks/useGetFile";
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_VEHICLE_APPLY } from "~/enumerate/namespace";

function List(props) {
  const [list, setList] = useState([]);

  const { loading: getFileLoading, getFile } = useGetFile();

  const [form] = Search.useForm();
  const { tableProps, getData } = useTable(props["enclosedAreaVehicleApplyList"], {
    form,
    params: () => {
      let carBelongType = 1;
      if (props.entrance === "enterprise")
        carBelongType = 2;
      if (props.entrance === "stakeholder")
        carBelongType = 3;
      return { processOrRecord: 1, carBelongType };
    },
    onSuccess: async (data) => {
      const list = await getFile({ single: false, dataSource: data.list, eqType: UPLOAD_FILE_TYPE_ENUM[602], foreignKey: "attachmentId" });
      setList(list);
    },
  });

  return (
    <Page isShowAllAction={false}>
      <Search
        options={[
          { name: "licenceNo", label: "车牌号" },
        ]}
        form={form}
        onFinish={getData}
      />
      <Table
        toolBarRender={() => (
          <Space>
            {props.permission(props.addBtn || "jgd-enclosed-vehicle-apply-add") && (
              <Button
                type="primary"
                icon={(<AddIcon />)}
                onClick={() => {
                  props.history.push("./add");
                }}
              >
                申请权限
              </Button>
            )}
          </Space>
        )}
        columns={[
          { title: "车辆类型", dataIndex: "vehicleTypeName" },
          { title: "申请人", dataIndex: "applyPersonUserName" },
          { title: "部门", dataIndex: "applyPersonDepartmentName" },
          { title: "手机号", dataIndex: "userPhone" },
          { title: "申请区域", dataIndex: "closedAreaName" },
          {
            title: "申请时间范围",
            dataIndex: "visitStartTime",
            render: (_, record) => `${record.visitStartTime} - ${record.visitEndTime}`,
          },
          { title: "申请原因", dataIndex: "applyReason" },
          { title: "车牌号", dataIndex: "licenceNo" },
          { title: "车辆照片", dataIndex: "files", render: (_, record) => <TooltipPreviewImg files={record.files} /> },
          { 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-vehicle-apply-show") && (
                  <Button
                    type="link"
                    onClick={() => {
                      props.history.push(`./view?id=${record.id}`);
                    }}
                  >
                    查看
                  </Button>
                )}
              </Space>
            ),
          },
        ]}
        {...tableProps}
        loading={tableProps.loading || getFileLoading}
        dataSource={list}
      />
    </Page>
  );
}

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