import { Button, Form, message, Space } from "antd";
import { useState } from "react";
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 { FORM_ITEM_RENDER_ENUM } from "zy-react-library/enum/formItemRender";
import useTable from "zy-react-library/hooks/useTable";
import AlarmRecordView from "~/components/AlarmRecord/View";
import { renderAlarmCurrentValue } from "~/utils/alarm";
import AssignModal from "./components/AssignModal";

function AssignList(props) {
  const [form] = Form.useForm();
  const [modalOpen, setModalOpen] = useState(false);
  const [detailOpen, setDetailOpen] = useState(false);
  const [detailId, setDetailId] = useState("");
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);
  const [currentAlarmId, setCurrentAlarmId] = useState(null);

  const [isBatch, setIsBatch] = useState(false);
  const { tableProps, getData } = useTable(props.alarmRecordList, {
    form,
    transform: formData => ({
      ...formData,
      alarmTimeStart: formData.alarmTime?.[0],
      alarmTimeEnd: formData.alarmTime?.[1],
      status: 10,
    }),
  });

  const handleBatchAssign = () => {
    if (selectedRowKeys.length === 0) {
      message.warning("请先选择要分配的报警记录");
      return;
    }
    setIsBatch(true);
    setCurrentAlarmId(null);
    setModalOpen(true);
  };

  const handleSingleAssign = (record) => {
    setIsBatch(false);
    setCurrentAlarmId(record.id);
    setModalOpen(true);
  };

  const rowSelection = {
    selectedRowKeys,
    onChange: setSelectedRowKeys,
  };

  return (
    <Page isShowAllAction={false}>
      <Search
        form={form}
        onFinish={getData}
        options={[
          { name: "sensorName", label: "传感器名称" },
          { name: "alarmTime", label: "报警时间", render: FORM_ITEM_RENDER_ENUM.DATE_RANGE },
        ]}
      />
      <Table
        rowKey="id"
        rowSelection={rowSelection}
        toolBarRender={() => (
          <Space>
            {
              props.permission("bjczfp-plcz")

              && (
                <Button type="primary" onClick={handleBatchAssign}>
                  批量处置
                </Button>
              )
            }
          </Space>
        )}
        columns={[
          { title: "传感器编码", dataIndex: "sensorCode" },
          { title: "传感器名称", dataIndex: "sensorName", ellipsis: true },
          { title: "报警时间", dataIndex: "alarmTime", width: 180 },
          { title: "报警描述", dataIndex: "alarmDesc", ellipsis: true },
          { title: "告警值", dataIndex: "currentValue" },
          {
            title: "操作",
            width: 180,
            render: (_, record) => (
              <Space>
                {
                  props.permission("bjczfp-czfp")

                  && (
                    <Button type="link" onClick={() => handleSingleAssign(record)}>
                      处置分配
                    </Button>
                  )
                }
                {
                  props.permission("bjczfp-info")
                  && (
                    <Button
                      type="link"
                      onClick={() => {
                        setDetailId(record.id);

                        setDetailOpen(true);
                      }}
                    >
                      查看
                    </Button>
                  )

                }
              </Space>
            ),
          },
        ]}
        {...tableProps}
      />
      {detailOpen && (
        <AlarmRecordView
          open={detailOpen}
          currentId={detailId}
          alarmRecordInfo={props.alarmRecordInfo}
          onCancel={() => {
            setDetailOpen(false);
            setDetailId("");
          }}
        />
      )}
      {modalOpen && (
        <AssignModal
          open={modalOpen}
          isBatch={isBatch}
          alarmId={currentAlarmId}
          alarmIds={selectedRowKeys}
          loading={props.alarmDisposeLoading}
          getData={getData}
          alarmRecordInfo={props.alarmRecordInfo}
          alarmAssign={props.alarmAssign}
          alarmBatchAssign={props.alarmBatchAssign}
          onCancel={() => {
            setModalOpen(false);
            setCurrentAlarmId(null);
            setSelectedRowKeys([]);
          }}
        />
      )}
    </Page>
  );
}

export default AssignList;
