import { Button, Form, message, Modal, Space } from "antd";
import { useState } from "react";
import AddIcon from "zy-react-library/components/Icon/AddIcon";
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 useTable from "zy-react-library/hooks/useTable";
import { getLabelName } from "zy-react-library/utils";
import { SENSOR_ATTR_OPTIONS } from "~/enumerate/constant";
import SensorTypeAdd from "../Add";
import SensorTypeView from "../View";

function SensorTypeList(props) {
  const [form] = Form.useForm();
  const [modalOpen, setModalOpen] = useState(false);
  const [viewOpen, setViewOpen] = useState(false);
  const [currentRecord, setCurrentRecord] = useState({});
  const [currentId, setCurrentId] = useState("");
  const { tableProps, getData } = useTable(props["sensorTypeList"], { form });

  const handleDelete = (record) => {
    Modal.confirm({
      title: "确定删除这条传感器类型吗？",
      maskClosable: false,
      onOk: async () => {
        const res = await props["sensorTypeRemove"]({ id: record.id });
        if (res?.success) {
          message.success("删除成功");
          getData();
        }
      },
    });
  };

  return (
    <Page isShowAllAction={false}>
      <Search
        form={form}
        onFinish={getData}
        options={[
          { name: "typeName", label: "传感器类型" },
        ]}
      />
      <Table
        rowKey="id"
        toolBarRender={() => (
          <Space>
            {/* {props.permission("cgqbj-add") && ( */}
            <Button
              type="primary"
              icon={<AddIcon />}
              onClick={() => {
                setCurrentId("");
                setModalOpen(true);
              }}
            >
              新增
            </Button>
            {/* )} */}
          </Space>
        )}
        columns={[
          { title: "传感器类型", dataIndex: "typeName" },
          {
            title: "传感器属性",
            dataIndex: "sensorAttr",
            render: (_, record) => getLabelName({ status: record.sensorAttr, list: SENSOR_ATTR_OPTIONS }) || "-",
          },
          {
            title: "操作",
            width: 220,
            render: (_, record) => (
              <Space>
                {
                  props.permission("cgqbj-info") && (

                    <Button
                      type="link"
                      onClick={() => {
                        setCurrentRecord(record);
                        setViewOpen(true);
                      }}
                    >
                      查看
                    </Button>
                  )
                }
                {props.permission("cgqbj-edit") && (
                  <Button
                    type="link"
                    onClick={() => {
                      setCurrentId(record.id);
                      setModalOpen(true);
                    }}
                  >
                    编辑
                  </Button>
                )}
                {props.permission("cgqbj-del") && (
                  <Button danger type="link" onClick={() => handleDelete(record)}>
                    删除
                  </Button>
                )}
              </Space>
            ),
          },
        ]}
        {...tableProps}
      />
      {modalOpen && (
        <SensorTypeAdd
          open={modalOpen}
          currentId={currentId}
          loading={props.sensorTypeLoading}
          getData={getData}
          sensorTypeInfo={props.sensorTypeInfo}
          sensorTypeSave={props.sensorTypeSave}
          sensorTypeEdit={props.sensorTypeEdit}
          onCancel={() => {
            setModalOpen(false);
            setCurrentId("");
          }}
        />
      )}
      {viewOpen && (
        <SensorTypeView
          open={viewOpen}
          record={currentRecord}
          onCancel={() => {
            setViewOpen(false);
            setCurrentRecord({});
          }}
        />
      )}
    </Page>
  );
}

export default SensorTypeList;
