/**
 * 店铺基础信息管理
 * @param Page
 * @returns
 */
 import type { ActionType, ProColumns } from '@ant-design/pro-components';
 import { ProTable } from '@ant-design/pro-components';
 import { Button, Modal, Form, Input, message, Popconfirm, Tag} from 'antd';
 import { useRef, useState} from 'react';
 import StoreInfoAPI from '@/api/storeService'
const Index =()=> {
     //表单
  const [typeForm] = Form.useForm();
  //编辑/新增弹窗
  const [isModalOpen, setIsModalOpen] = useState(false);
  const actionRef = useRef<ActionType>();
  //方法
  const OnSubmit=async (values: StoreTypes.StoreInfo) =>{
    if (values.id) {
      const response = await StoreInfoAPI.Update(values);
      if (response.statusCode === 200) {
        message.success('修改成功');
        actionRef.current?.reload();
        setIsModalOpen(false);
      } else {
        message.error(response.errors || '修改失败');
      }
    } else {
      const response = await StoreInfoAPI.Add(values);
      if (response.statusCode === 200) {
        message.success('添加成功');
        actionRef.current?.reload();
        setIsModalOpen(false);
      } else {
        message.error(response.errors || '添加失败');
      }
    }
  }
  //表单名
  const columns: ProColumns<StoreTypes.StoreInfo>[] = [
    {
      title: '店铺名称',
      dataIndex: 'name',
      
    },
    {
        title: '地址',
        dataIndex: 'address',
        
      },
      {
        title: '联系电话',
        dataIndex: 'phone',
  
      },
      {
        title: '负责人',
        dataIndex: 'directors',
        
      },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      render: (_, record) => [
        <Button
          key="edit"
          type="primary"  ghost
          onClick={async () => {
            setIsModalOpen(true)
            const copyFormData = JSON.parse(JSON.stringify(record));
            typeForm.setFieldsValue(copyFormData);
          }}
        >
          编辑
        </Button>,
        <Popconfirm
          key="del"
          title="确定要删除此条数据吗？"
          onConfirm={() => {
            const delData: StoreTypes.StoreInfo = {
                id: record.id,
                name: '',
                address: '',
                phone: '',
                directors: ''
            };
            StoreInfoAPI.Del(delData).then((r) => {
              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>,
      ],
    },
  ];
  return (
    <>
    <ProTable<StoreTypes.StoreInfo>
        key="myTable"
        columns={columns}
        actionRef={actionRef}
        cardBordered
        request={async (params = {}) => {
          const jsonData: StoreTypes.StoreInfoPage = {
              pageIndex: params.current || 1,
              pageSize: params.pageSize || 10,
              name: params.name || '',
              id: '',
              address: params.address,
              phone:params.phone,
              directors: params.directors
          };
          const response = await StoreInfoAPI.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,
        }}
        dateFormatter="string"
        headerTitle="设备列表"
        toolBarRender={() => [
          <Button
            key="button"
            type="primary"
            onClick={() => {
              typeForm.resetFields();
              setIsModalOpen(true);
            }}
          >
            新建
          </Button>,
        ]}
      />
       <Modal
        key="01"
        width={800}
        title={<Tag color="#cd201f">新增信息</Tag>}
        open={isModalOpen}
        onCancel={() => {
          setIsModalOpen(false);
        }}
        footer={[
          <Button
            key="back"
            onClick={() => {
              setIsModalOpen(false);
            }}
          >
            返回
          </Button>,
          <Button key="submit" form="myForm" type="primary" htmlType="submit">
            提交
          </Button>,
        ]}
      >
        <Form
          form={typeForm}
          name="basic"
          id="myForm"
          labelCol={{ 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="address"
            rules={[{ required: true, message: '此项必填！' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="联系电话"
            name="phone"
            rules={[{ required: true, message: '此项必填！' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="联系人"
            name="directors"
            rules={[{ required: true, message: '此项必填！' }]}
          >
            <Input />
          </Form.Item>
          
        </Form>
      </Modal>
    </>
  )
}
export default Index
