import { getAlgoInfo, searchAlgoInfo, algoInfoModify } from '@/api/algo/algo';
import { ref } from 'vue';
import { ElMessage } from 'element-plus';

export function useAlgo() {
  const algoList = ref([]);
  const page = ref(1);
  const total = ref(0);
  const pageSize = ref(12);
  const keyWord = ref('');

  const algoId = ref(0);
  const pushLinkPrompt = ref('');
  const pushStatement = ref('');

  const getAlgoDatas = () => {
    return getAlgoInfo(page.value, pageSize.value).then((res) => {
      algoList.value = res.records;
      total.value = res.totalRow;
    });
  };

  const searchAlgoDatas = () => {
    return searchAlgoInfo(page.value, keyWord.value).then((res) => {
      algoList.value = res.records;
    });
  };

  const modifyAlgoDatas = () => {
    return algoInfoModify(algoId.value, pushLinkPrompt.value, pushStatement.value)
      .then(function () {
        ElMessage({
          message: '算法数据保存成功',
          type: 'success',
        });
      })
      .catch(function () {
        ElMessage.error('算法数据保存失败');
      });
  };

  return {
    algoList,
    page,
    total,
    pageSize,
    keyWord,
    getAlgoDatas,
    searchAlgoDatas,
    algoId,
    pushLinkPrompt,
    pushStatement,
    modifyAlgoDatas,
  };
}

export default useAlgo;
