import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Descriptions, Divider } from "antd";
import { useEffect, useState } from "react";
import PreviewImg from "zy-react-library/components/PreviewImg";
import PreviewPdf from "zy-react-library/components/PreviewPdf";
import PreviewWord from "zy-react-library/components/PreviewWord";
import Table from "zy-react-library/components/Table";
import { UPLOAD_FILE_TYPE_ENUM } from "zy-react-library/enum/uploadFile/gwj";
import useGetFile from "zy-react-library/hooks/useGetFile";
import { getFileSuffix, getLabelName, isEmptyToWhether } from "zy-react-library/utils";
import { NS_BI_MARK_INFO } from "~/enumerate/namespace";

const STATUS = [
  { bianma: "0", name: "暂存" },
  { bianma: "1", name: "未开工" },
  { bianma: "2", name: "开工申请中" },
  { bianma: "4", name: "进行中" },
  { bianma: "5", name: "完工申请中" },
  { bianma: "6", name: "已结束" },
];
const VIDEO_TYPE = [
  { bianma: 1, name: "固定摄像头" },
  { bianma: 2, name: "移动摄像头" },
];

const renderFiles = (files = []) => {
  const pdfFiles = [];
  const imgFiles = [];
  const wordFiles = [];
  for (let i = 0; i < files.length; i++) {
    const file = files[i];
    if (getFileSuffix(file.filePath) === "pdf") {
      pdfFiles.push(file);
    }
    else if (["jpg", "jpeg", "png"].includes(getFileSuffix(file.filePath))) {
      imgFiles.push(file);
    }
    else if (["doc", "docx"].includes(getFileSuffix(file.filePath))) {
      wordFiles.push(file);
    }
  }
  return (
    <>
      {imgFiles.length > 0 && <PreviewImg files={imgFiles} />}
      {pdfFiles.length > 0 && <PreviewPdf files={pdfFiles} />}
      {wordFiles.length > 0 && <PreviewWord files={wordFiles} />}
    </>
  );
};

const KeyProject = (props) => {
  const { getFile } = useGetFile();

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

  const getData = async () => {
    const { data = {} } = await props.keyProjectInfo({ id: props.id });
    console.log(data);
    const files = await getFile({
      eqType: UPLOAD_FILE_TYPE_ENUM["168"],
      eqForeignKey: data.keyProjectId,
    });
    setInfo({
      files,
      ...data,
    });
  };

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

  return (
    <div>
      <Divider titlePlacement="start">基本信息</Divider>
      <div>
        <Descriptions
          bordered
          column={1}
          items={[
            { label: "辖区单位", children: info.jurisdictionDepartmentName },
            { label: "辖区单位负责人", children: info.jurisdictionUserName },
            { label: "主管部门", children: info.masterDepartmentName },
            { label: "是否已有项目作业", children: isEmptyToWhether(info.projectWorkFlag) },
            { label: "重点作业名称", children: info.projectName },
            { label: "重点作业属性", children: info.projectTypeName && info.projectTypeName.join(",") },
            { label: "计划工期开始", children: info.planWorkStartDate },
            { label: "计划竣工时间", children: info.planWorkEndDate },
            { label: "相关方单位", children: info.xgfCorpinfoName },
            { label: "相关方单位负责人", children: info.xgfUserName },
            { label: "相关方单位负责人手机号", children: info.xgfMasterPhone },
            { label: "是否设置监理单位", children: isEmptyToWhether(info.supervisionUnitFlag) },
            ...(info.supervisionUnitFlag === 0
              ? []
              : [
                  { label: "监理单位", children: info.supervisionUnitCorpName },
                  { label: "监理单位工程负责人", children: info.supervisionUnitUserName },
                  { label: "监理单位工程负责人电话", children: info.supervisionUnitUserPhone },
                ]),
            { label: "状态", children: getLabelName({ status: info.applyStatus, list: STATUS }) },
            {
              label: "重点作业定位",
              children: (
                <>
                  <span>
                    经度：
                    {info.longitude || "-"}
                  </span>
                  <span>
                    纬度：
                    {info.latitude || "-"}
                  </span>
                </>
              ),
            },
            { label: "安全协议管理", children: renderFiles(info.files) },
          ]}
        />
      </div>
      <Divider orientation="left">绑定视频监控</Divider>
      <div>
        <Table
          dataSource={info.keyProjectCameraList || []}
          columns={[
            { title: "视频名称", dataIndex: "cameraName" },
            { title: "视频类型", dataIndex: "cameraType", render: (_, record) => getLabelName({ status: record.cameraType, list: VIDEO_TYPE }) },
          ]}
          disabledResizer
          options={false}
          pagination={false}
        />
      </div>
      <Divider orientation="left">处罚人员设置</Divider>
      <div>
        <Table
          dataSource={info.keyProjectPenaltyPersonnelList || []}
          columns={[
            { title: "处罚人部门", dataIndex: "departmentName" },
            { title: "处罚人", dataIndex: "userName" },
          ]}
          disabledResizer
          options={false}
          pagination={false}
        />
      </div>
    </div>
  );
};

export default Connect([NS_BI_MARK_INFO], true)(KeyProject);
