import { getDishList, addDish, updataDish, deleteDish } from '@/services/dish';
import { queryTable } from '@/utils/request';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { useNavigate } from 'react-router-dom';
import { Button, message, Popconfirm } from 'antd';
import { useRef, useState } from 'react';

const DishList = (props) => {
  console.log(props, '===');
  const [showDetail, setShowDetail] = useState(false);
  const [activeKey, setActiveKey] = useState('');
  const actionRef = useRef();
  const navigate = useNavigate();

  const updata = (row) => {
    if (row.id) {
      updataDish(row.id, { state: row.state === '1' ? '2' : '1' }).then((res) => {
        message.success('修改成功');
        actionRef.current.reload();
      });
    }
  };

  const handleDelete = (id) => {
    if (id) {
      deleteDish(id).then((res) => {
        message.success('删除成功');
        actionRef.current.reload();
      });
    }
  };

  const columns = [
    {
      title: '菜肴名称',
      dataIndex: 'name',
    },

    {
      title: '菜肴单位',
      dataIndex: 'unit',
      search: false,
    },
    {
      title: '包含食材(种)',
      dataIndex: 'foodNum',
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 200,
      render: (_, record) => [

        <Button
          key={2}
          style={{ padding: 0 }}
          type="link"
          onClick={() => {
            navigate(`/material/dish/edit?id=${record.id}`);
          }}
        >
          修改
        </Button>,

        <Popconfirm
          key={3}
          title="您是否确认删除 ?"
          onConfirm={() => handleDelete(record.id)}
          okText="确定"
          cancelText="取消"
        >
          {/* manualPush */}
          <Button style={{ padding: 0 }} type="link">
            删除
          </Button>
        </Popconfirm>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable
        search={true}
        actionRef={actionRef}
        rowKey="id"
        toolBarRender={() => [
          <Button
            key="2"
            type="primary"
            onClick={() => {
              navigate('/material/dish/edit');
            }}
          >
            新增
          </Button>,
        ]}
        request={queryTable(getDishList)}
        columns={columns}
      />
    </PageContainer>
  );
};

export default DishList;