// 分组页面
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { Button, Modal, Form, Input, message, Popconfirm, Tag, QRCode, Table } from 'antd';
import { useEffect, useRef, useState } from 'react';
import type { ColumnsType } from 'antd/es/table';
import groupInfoAPI from '@/api/groupService';
const group = () => {
  // 4.定义编辑/新增的弹窗
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const [isModalOpen, setIsModalOpen] = useState(false);
  // 下拉
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const [modelTitle, setModelTitle] = useState<string>();

  // eslint-disable-next-line react-hooks/rules-of-hooks
  const actionRef = useRef<ActionType>();

  // 6.定义表单
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const [typeForm] = Form.useForm();


  // 1.定义列
  const columns: ProColumns<GroupInfoTypes.Info>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      tip: '规则名称是唯一的 key',
      hideInSearch: true,
      hideInTable: true,

    },
    {
      title: '分组名称',
      dataIndex: 'name',
      ellipsis: true,
      align: 'center'
    },

    {
      title: '分组描述',
      dataIndex: 'des',
      hideInSearch: true,
      ellipsis: true,
      align: 'center'
    },



    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      align: 'center',
      // 4.操作内有两个按钮
      render: (_, record) => [

        <Button
          key="edit"
          type="primary" ghost
          onClick={async () => {

            setIsModalOpen(true);
            setModelTitle('编辑分组');

            const copyFormData = JSON.parse(JSON.stringify(record));
            typeForm.setFieldsValue(copyFormData);
          }}
        >编辑
        </Button>,

        // 删除
        <Popconfirm
          key="del"
          title="确定要删除此条数据吗？"
          onConfirm={() => {

            const EnablejsonData: GroupInfoTypes.Info = {
              id: record.id,
              name: '',
              code: ''
            };

            groupInfoAPI.Del(EnablejsonData).then((r: MyResponse.Content) => {
              if (r.statusCode === 200) {
                message.success(r.statusCode === 200 ? '删除成功' : r.message);
                actionRef.current?.reload();
              }
            });
          }}
          onCancel={() => {
            message.info('已取消删除');
          }}
          okText="确认"
          cancelText="关闭"
        >
          <Button type="primary" danger ghost>删除</Button>

        </Popconfirm>,
      ]

    },
  ];


  // eslint-disable-next-line react-hooks/rules-of-hooks
  useEffect(() => {
    // 下拉列表信息
    const groupList = async () => {

      const data = await groupInfoAPI.selectlist();
      console.log(data);

      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      // setgroupitem(data.data);
    };
    groupList();
  }, []);








  // 7.定义表单提交
  //  表单提交
  const OnSubmit = async (values: GroupInfoTypes.Info) => {

    if (values.id) {
      const response = await groupInfoAPI.Update(values);
      if (response.statusCode === 200) {
        message.success('修改成功');
        actionRef.current?.reload();
        setIsModalOpen(false);
      } else {
        message.error(JSON.stringify(response.errors) || '修改失败');
      }
    } else {
      const response = await groupInfoAPI.Add(values);
      console.log('response', response);
      if (response.statusCode === 200) {
        message.success('添加成功');
        actionRef.current?.reload();
        setIsModalOpen(false);
      } else {
        message.error(JSON.stringify(response.errors) || '添加失败');
      }
    }
  };


  return (
    <>
      {/* 2.列的展现 */}
      <ProTable<GroupInfoTypes.Info>
        key="myTable"
        columns={columns}

        actionRef={actionRef}
        cardBordered
        //  3.调用列接口
        request={async (params = {}) => {
          const jsonData: GroupInfoTypes.Page = {
            pageIndex: params.current || 1,
            pageSize: params.pageSize || 10,
            name: params.name || '',
            id: '',
            code: ''
          };
          const response = await groupInfoAPI.PagedList(jsonData);
          if (response.statusCode === 200) {
            return {
              data: response.data.items,
              success: true,
              total: response.data.total,
            };
          } else {
            return {
              data: [],
              success: false,
              total: 0,
            };
          }
        }}
        //  展示的页数
        rowKey="id"
        pagination={{
          pageSize: 10,
          showSizeChanger:false
        }}
        dateFormatter="string"
        headerTitle="分组列表"
        toolBarRender={() => [
          <Button
            key="button"
            type="primary"
            onClick={() => {
              typeForm.resetFields();
              setIsModalOpen(true);
              setModelTitle('新增分组');

            }}
          >
            新增分组
          </Button>,
        ]}

      />
      {/* 5.弹窗 */}
      <Modal
        maskClosable={false}
        key="01"
        width={800}
        title={<Tag color="#cd201f">{modelTitle}</Tag>}
        open={isModalOpen}
        onCancel={() => {
          setIsModalOpen(false);
        }}
        footer={[
          <Button
            key="back"
            onClick={() => {
              setIsModalOpen(false);

            }}
          >
            返回
          </Button>,
          <Button key="submit" form="groupForm" type="primary" htmlType="submit">
            提交
          </Button>,
        ]}
      >
        {/* 弹窗内的表单 */}
        <Form
          form={typeForm}
          name="basic"
          id="groupForm"
          labelAlign='right'
          // lCol={{ span: 4 }}
          wrapperCol={{ span: 12 }}
          onFinish={OnSubmit}
          autoComplete="off"
        >
          <Form.Item label="id" name="id" hidden>
            <Input />
          </Form.Item>

          <Form.Item
            label="分组名称"
            name="name"
            rules={[{ required: true, message: '此项为必填项!' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item label="分组描述" name="des" rules={[{ required: true, message: '此项为必填项!' }]} >
            <Input />
          </Form.Item>




        </Form>
      </Modal>
    </>
  )
}

export default group