import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Image } from "antd";
import { useEffect, useState } from "react";
import { getFileUrl } from "zy-react-library/utils";
import { NS_BI_MARK_INFO } from "~/enumerate/namespace";

function SafetyMeasures(props) {
  const [safetyMeasures, setSafetyMeasures] = useState([]);
  const [otherSafetyMeasures, setOtherSafetyMeasures] = useState([]);

  const getData = async () => {
    const { data } = await props.eightWorkMeasuresLogs({ workId: props.workId });
    setSafetyMeasures(data.filter(item => item.type === 1));
    setOtherSafetyMeasures(data.filter(item => item.type === 2));
  };

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

  return (
    <tr>
      <td colSpan={props.colSpan} style={{ border: "none", padding: 0 }}>
        <table>
          <tbody>
            <tr>
              <td className="center title" style={{ width: 80 }}>序号</td>
              <td className="center title" style={{ width: "auto" }}>安全措施</td>
              <td className="center title" style={{ width: 100 }}>是否涉及</td>
              <td className="center title" style={{ width: 100 }}>确认人</td>
            </tr>
            {safetyMeasures.map((item, index) => (
              <tr key={index}>
                <td className="center">{index + 1}</td>
                <td>{item.content}</td>
                <td className="center">符合</td>
                <td className="center">
                  {item.signPath && <Image src={getFileUrl() + item.signPath} width={50} height={50} />}
                </td>
              </tr>
            ))}
            {
              otherSafetyMeasures.length > 0 && (
                <tr>
                  <td className="center">{safetyMeasures.length + 1}</td>
                  <td colSpan={3}>
                    {
                      otherSafetyMeasures.map((item, index) => (
                        <div key={index} style={{ display: "flex", justifyContent: "space-between" }}>
                          <div>
                            <span>其它安全措施：</span>
                            <span>{item.content}</span>
                          </div>
                          <div>
                            <span>编制人：</span>
                            <span>{item.createName}</span>
                          </div>
                        </div>
                      ))
                    }
                  </td>
                </tr>
              )
            }
          </tbody>
        </table>
      </td>
    </tr>
  );
}

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