import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Descriptions, Divider, Spin } from "antd";
import { useEffect, useState } from "react";
import Page from "zy-react-library/components/Page";
import PreviewImg from "zy-react-library/components/PreviewImg";
import { UPLOAD_FILE_TYPE_ENUM } from "zy-react-library/enum/uploadFile/gwj";
import useGetFile from "zy-react-library/hooks/useGetFile";
import useGetUrlQuery from "zy-react-library/hooks/useGetUrlQuery";
import { getLabelName } from "zy-react-library/utils";
import { VEHICLE_TYPE_ENUM } from "~/enumerate/constant";
import { NS_VEHICLE_APPLY } from "~/enumerate/namespace";

function View(props) {
  const query = useGetUrlQuery();
  const { loading: getFileLoading, getFile } = useGetFile();

  const [info, setInfo] = useState({});

  const getData = async () => {
    const { data } = await props["vehicleApplyInfo"]({ id: query.id });
    const drivingLicenseFile = await getFile({
      eqType: UPLOAD_FILE_TYPE_ENUM[601],
      eqForeignKey: data.drivingLicenseId,
    });
    const attachmentFile = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM[602], eqForeignKey: data.attachmentId });
    setInfo({
      ...data,
      drivingLicenseFile,
      attachmentFile,
    });
  };

  useEffect(() => {
    getData();
  }, []);

  const getVehicleBelongFields = (info) => {
    if (!props.vehicleBelongType) {
      return [{ label: "车辆所属人", children: info.employeeVehicleUserName, span: 24 }];
    }
    else if (props.vehicleBelongType === 2 || props.vehicleBelongType === 4) {
      return [
        { label: "车辆所属类型", children: getLabelName({ list: VEHICLE_TYPE_ENUM, status: info.vehicleBelongType }) },
        { label: "车辆所属部门", children: info.vehicleDepartmentName },
      ];
    }
    else if (props.vehicleBelongType === 1 || props.vehicleBelongType === 3 || props.vehicleBelongType === 8) {
      return [
        { label: "车辆所属类型", children: getLabelName({ list: VEHICLE_TYPE_ENUM, status: info.vehicleBelongType }) },
        { label: "车辆所属部门", children: info.vehicleDepartmentName },
        { label: "车辆所属人", children: info.employeeVehicleUserName, span: 24 },
      ];
    }
  };

  return (
    <Page headerTitle="查看" contentPadding="0 20px 20px 20px">
      <Spin spinning={props.vehicleApply.vehicleApplyLoading || getFileLoading}>
        <Divider orientation="left">申请信息</Divider>
        <Descriptions
          column={2}
          bordered
          styles={{ label: { width: 200 } }}
          items={[
            ...getVehicleBelongFields(info),
            { label: "车牌类型", children: info.licenceTypeName },
            { label: "车牌号", children: info.licenceNo },
            { label: "车辆类型", children: info.vehicleTypeName },
            { label: "访问港区", children: info.gateLevelAuthArea && JSON.parse(info.gateLevelAuthArea).area.map(item => item.value).join("、") },
            { label: "访问开始时间", children: info.visitStartTime },
            { label: "访问结束时间", children: info.visitEndTime },
            { label: "行驶证照片", children: (<PreviewImg files={info.drivingLicenseFile} />), span: 24 },
            { label: "车辆图片", children: (<PreviewImg files={info.attachmentFile} />), span: 24 },
          ]}
        />
        <Divider orientation="left">审批信息</Divider>
        <Descriptions
          column={1}
          bordered
          styles={{ label: { width: 200 } }}
          items={[
            { label: "审批人", children: info.currentAuditUserName },
          ]}
        />
      </Spin>
    </Page>
  );
}

export default Connect([NS_VEHICLE_APPLY], true)(View);
