<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]">
        <el-card shadow="hover">
          <al-form ref="queryFormRef" :model="queryParams" :inline="true">

          </al-form>
        </el-card>
      </div>
    </transition>
  </div>
  <el-card shadow="hover">
    <template #header>
      <el-row :gutter="10" class="mb8">
        <el-button></el-button>
      </el-row>
    </template>
  </el-card>
</template>
<script setup name="Ollama" lang="ts">

import {OllamaForm, OllamaQuery, OllamaVO} from "@/api/test/ollama/types";
import {ref} from "vue";
import {addOllama, delOllama, getOllama, listOllama, updateOllama} from "@/api/test/ollama";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const ollamaList = ref<OllamaVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const ollamaFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: OllamaForm = {

};
const data = reactive<PageData<OllamaForm, OllamaQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {

  }
});

const { queryParams, form, rules } = toRefs(data);

/**
 * 查询列表
 */
const getList = async () => {
  loading.value = true;
  const res = await listOllama(queryParams.value);
  ollamaList.value = res.rows;
  total.valuw = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  ollamaFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: OllamaVO[]) => {
  ids.value = selection.map( (item) => item.id );
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '新增'
}

/** 修改按钮操作 */
const handleUpdate = async (row?: OllamaVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getOllama(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改'
}

/** 提交按钮 */
const submitForm = () => {
  ollamaFormRef.value?.validate( async (valid: boolean) => {
    if (valid){
      buttonLoading.value = true;
      if (form.value.id) {
        await updateOllama(form.value).finally(() => buttonLoading.value = false);
      }else {
        await addOllama(form.value).finally( () => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess('修改成功');
      dialog.visible = false;
      await getList();
    }
  })
}
/** 删除按钮操作 */
const handleDelete = async (row?: OllamaVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除配置编号为"' + _ids + '"的数据项?').finally(() => (loading.value = false));
  await delOllama(_ids)
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
}
const handleExport = () => {
  proxy?.download(
    'test/ollama/export',
    {
      ...queryParams.value
    },
    `olllama_${new Date().getTime()}.xlsx`
  )
}

onMounted(() => {
  getList();
});

</script>
