import { ref } from 'vue';
import { RobotType, getRobot, addRobot, editRobot, delRobot } from '@/api/push/push';
import { useRequest } from 'vue-hooks-plus';

export const usePush = () => {
  const robotPushList = ref<RobotType[]>([]);
  const total = ref(0);
  const page = ref(1);
  const size = ref(10);

  // 查询
  const robotSearch = () => {
    const params = {
      pageNumber: page.value,
      pageSize: size.value,
    };
    return getRobot(params).then((res) => {
      return res;
    });
  };

  const { loading, run: getRobotList } = useRequest(robotSearch, {
    manual: true,
    onSuccess: (res) => {
      robotPushList.value = [...res.records];
      total.value = res.totalRow;
    },
  });

  const robotAdd = (data: RobotType) => {
    return addRobot(data).then(() => {
      getRobotList();
    });
  };

  const robotDel = (id: number) => {
    return delRobot(id).then(() => {
      getRobotList();
    });
  };

  const robotEdit = (data: RobotType) => {
    return editRobot(data).then(() => {
      getRobotList();
    });
  };

  return {
    robotPushList,
    total,
    page,
    size,
    loading,
    robotSearch,
    getRobotList,
    robotAdd,
    robotDel,
    robotEdit,
  };
};

export default usePush;
