<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="id" :request-api="listModelApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['ag:model:add']" icon="CirclePlus" @click="openDialog(1, '算法模型配置新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['ag:model:import']" icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['ag:model:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['ag:model:remove']"
          icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link icon="View" v-auth="['ag:model:query']" @click="openDialog(3, '算法模型配置查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['ag:model:edit']" @click="openDialog(2, '算法模型配置编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['ag:model:remove']" @click="deleteModel(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="Model">
import { ref, reactive, onMounted } from 'vue'
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ProTable from '@/components/ProTable/index.vue'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import {
  listModelApi,
  delModelApi,
  addModelApi,
  updateModelApi,
  importTemplateApi,
  importModelDataApi,
  exportModelApi,
  getModelApi
} from '@/api/modules/ag/model'
import { getAlgorithmOptionApi } from '@/api/modules/task/task'
import { listTaskConfigurationApi } from '@/api/modules/task/taskConfiguration'

// ProTable 实例
const proTable = ref<ProTableInstance>()
let allAgloData = ref([] as any[])

// 删除算法模型配置信息
const deleteModel = async (params: any) => {
  await useHandleData(delModelApi, params.id, '删除【' + params.id + '】算法模型配置')
  proTable.value?.getTableList()
}

// 批量删除算法模型配置信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delModelApi, ids, '删除所选算法模型配置信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出算法模型配置列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出算法模型配置数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportModelApi, '算法模型配置列表', proTable.value?.searchParam)
  )
}

// 批量添加算法模型配置
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '算法模型配置',
    tempApi: importTemplateApi,
    importApi: importModelDataApi,
    getTableList: proTable.value?.getTableList
  }
  dialogRef.value?.acceptParams(params)
}

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.id) {
    res = await getModelApi(row?.id || null)
  }
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    itemsOptions: itemsOptions,
    model: type == 1 ? {} : res.data,
    api: type == 1 ? addModelApi : updateModelApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  // { prop: 'id', label: '主键ID' },
  {
    prop: 'algorithmId',
    label: '算法',
    search: {
      el: 'input'
    }
  },
  {
    prop: 'modelName',
    label: '模型名称',
    search: {
      el: 'input'
    }
  },
  {
    prop: 'sampleNumber',
    label: '训练样本数',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'cycleEpoch',
    label: '训练循环次数',
    search: {
      el: 'input'
    },
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])
// 表单配置项
let itemsOptions: ProForm.ItemsOptions[] = []
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '算法',
      prop: 'algorithmId',
      rules: [{ required: true, message: '算法不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'select',
        labelKey: 'name',
        valueKey: 'id',
        enum: allAgloData.value,
        placeholder: '请选择算法'
      }
    },
    {
      label: '模型名称',
      prop: 'modelName',
      rules: [{ required: true, message: '模型名称不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入模型名称'
      }
    },
    // {
    //   label: '模型',
    //   prop: 'modelAddress',
    //   rules: [{ required: true, message: '模型不能为空', trigger: 'blur' }],
    //   compOptions: {
    //     elTagName: 'file-upload',
    //     accept: '.pt',
    //     fileSize: 4096,
    //     fileType: ['pt']
    //     // props: { fileSize: 4096, fileType: ['pt'] }
    //     // props: { accept: '.pt' }
    //   }
    // },
    {
      label: '模型',
      prop: 'modelAddress',
      rules: [{ required: true, message: '模型不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'slot'
      }
    },
    {
      label: '训练样本数',
      prop: 'sampleNumber',
      compOptions: {
        placeholder: '请输入训练样本数'
      }
    },
    {
      label: '训练循环次数',
      prop: 'cycleEpoch',
      compOptions: {
        placeholder: '请输入训练循环次数'
      }
    },
    {
      label: '备注',
      prop: 'remarks',
      compOptions: {
        placeholder: '请输入备注'
      }
    }
  ]
}
onMounted(() => {
  listTaskConfigurationApi().then(res => {
    allAgloData.value = res.data.list
  })
})
</script>
