import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Descriptions, Divider, message, Spin } from "antd";
import { useEffect, useState } from "react";
import FormBuilder from "zy-react-library/components/FormBuilder";
import Page from "zy-react-library/components/Page";
import PreviewImg from "zy-react-library/components/PreviewImg";
import { FORM_ITEM_RENDER_ENUM } from "zy-react-library/enum/formItemRender";
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 { NS_ENCLOSED_AREA_VEHICLE_APPLY } from "~/enumerate/namespace";

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

  const query = useGetUrlQuery();
  const [form] = FormBuilder.useForm();
  const { loading: getFileLoading, getFile } = useGetFile();

  const getData = async () => {
    const { data } = await props["enclosedAreaVehicleApplyInfo"]({ id: query.id });
    const vehicleLicenseFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM[601], eqForeignKey: data.drivingLicenseId });
    const vehicleFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM[602], eqForeignKey: data.attachmentId });
    const informSignFiles = await getFile({ eqType: UPLOAD_FILE_TYPE_ENUM[610], eqForeignKey: data.informSignId });
    setInfo({
      ...data,
      vehicleLicenseFiles,
      vehicleFiles,
      informSignFiles,
    });
  };

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

  const onSubmit = async (values) => {
    const { success } = await props["enclosedAreaVehicleApplyReviewSubmit"]({
      ...values,
      id: query.id,
    });
    if (success) {
      message.success("提交成功");
      props.history.goBack();
    }
  };

  return (
    <Page headerTitle={props.headerTitle || "审核"} contentPadding="0 20px 20px 20px" isShowFooter={props.isShowFooter || false}>
      <Spin spinning={props.enclosedAreaVehicleApply.enclosedAreaVehicleApplyLoading || getFileLoading}>
        <Divider orientation="left">车辆信息</Divider>
        <Descriptions
          bordered
          column={2}
          styles={{ label: { width: 200 } }}
          items={[
            { label: "车辆类型", children: info.vehicleTypeName },
            { label: "车牌类型", children: info.licenceTypeName },
            { label: "车牌号", children: info.licenceNo },
            { label: "驾驶人姓名", children: info.drivingUserName },
            { label: "手机号", children: info.userPhone },
            { label: "身份证号", children: info.userCard ? atob(info.userCard) : "" },
            { label: "申请区域", children: info.closedAreaName },
            { label: "一级口门名称", children: info.levelOneMkmjName },
            { label: "二级口门名称", children: info.levelTwoMkmjName },
            { label: "申请时间范围", children: `${info.visitStartTime} - ${info.visitEndTime}` },
            { label: "车辆照片", children: (<PreviewImg files={info.vehicleFiles} />) },
            { label: "行驶证照片", children: (<PreviewImg files={info.vehicleLicenseFiles} />) },
            { label: "申请原因", children: info.applyReason },
            { label: "申请人员签字", children: (<PreviewImg files={info.informSignFiles} />) },
          ]}
        />
        {!props.headerTitle && (
          <FormBuilder
            style={{ marginTop: 20 }}
            span={24}
            onFinish={onSubmit}
            values={{
              auditFlag: "2",
            }}
            options={[
              {
                name: "auditFlag",
                label: "是否通过审核",
                render: FORM_ITEM_RENDER_ENUM.RADIO,
                items: [{ bianma: "2", name: "是" }, { bianma: "3", name: "否" }],
              },
              {
                name: "refusalReason",
                label: "驳回原因",
                render: FORM_ITEM_RENDER_ENUM.TEXTAREA,
                dependencies: ["auditFlag"],
                hidden: formValues => !(formValues.auditFlag === "3"),
              },
            ]}
            form={form}
          />
        )}
      </Spin>
    </Page>
  );
}

export default Connect([NS_ENCLOSED_AREA_VEHICLE_APPLY], true)(Review);
