<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="推进器列表"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['title']"
      @refresh="search"
    >
      <template #toolbar-left>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link status="danger" title="删除" @click="onDelete(record)"> 删除 </a-link>
        </a-space>
      </template>
    </GiTable>
    <ThrusterAddModal ref="ThrusterAddModalRef" @save-success="search"></ThrusterAddModal>
  </div>
</template>

<script setup lang="ts">
import ThrusterAddModal from './thrusterAddModal.vue'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import { listThruster, thrusterDel } from '@/apis/production/thruster'
import type { ThrusterListResp } from '@/apis/production/type'

defineOptions({ name: 'Thruster' })

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((page) => listThruster({ ...page }), { immediate: true })
const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '消耗真实货币数量', dataIndex: 'realMoney', slotName: 'realMoney', align: 'center', width: 180 },
  { title: '消耗金币数量', dataIndex: 'consumeGoldCoin', slotName: 'consumeGoldCoin', align: 'center', width: 180 },
  { title: '每秒产量', dataIndex: 'numericalValue', slotName: 'numericalValue', align: 'center', width: 180 },
  { title: 'level', dataIndex: 'level', slotName: 'level', align: 'center', width: 180 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 350,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: true,
  },
]

// 重置
const reset = () => {
  search()
}

// 新增
const ThrusterAddModalRef = ref<InstanceType<typeof ThrusterAddModal>>()
const onAdd = () => {
  ThrusterAddModalRef.value?.onAdd()
}
const onUpdate = (record: ThrusterListResp) => {
  ThrusterAddModalRef.value?.onUpdate(record.id)
}

const onDelete = (record: ThrusterListResp) => {
  return handleDelete(() => thrusterDel(record.id), {
    content: `是否确定删除推进器「${record.level}」？`,
    showModal: true,
  })
}
</script>

<style scoped lang="scss"></style>
