<template>
  <div class="art-full-height">
    <TableSearch v-model="searchForm" @search="handleSearch" @reset="resetSearchParams" />

    <ElCard class="art-table-card" shadow="never">
      <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
        <template #left>
          <ElSpace wrap>
            <ElButton v-permission="'core:dept:save'" @click="showDialog('add')" v-ripple>
              <template #icon>
                <ArtSvgIcon icon="ri:add-fill" />
              </template>
              {{ $t('table.actions.add') }}
            </ElButton>
            <ElButton v-permission="'core:dept:update'" @click="handleSyncConfigs" v-ripple>
              补齐渠道配置
            </ElButton>
          </ElSpace>
        </template>
      </ArtTableHeader>

      <ArtTable
        rowKey="id"
        :loading="loading"
        :data="data"
        :columns="columns"
        @sort-change="handleSortChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      >
        <template #operation="{ row }">
          <div class="flex gap-2">
            <SaButton
              v-permission="'core:dept:update'"
              type="secondary"
              @click="showDialog('edit', row)"
            />
            <SaButton
              v-permission="'core:dept:destroy'"
              type="error"
              @click="openDeleteDialog(row)"
            />
          </div>
        </template>
      </ArtTable>
    </ElCard>

    <EditDialog
      v-model="dialogVisible"
      :dialog-type="dialogType"
      :data="dialogData"
      @success="refreshData"
    />

    <DeleteChannelDialog
      v-model="deleteDialogVisible"
      :dept-id="deleteDeptId"
      :dept-name="deleteDeptName"
      @success="refreshData"
    />
  </div>
</template>

<script setup lang="ts">
  import { useTable } from '@/hooks/core/useTable'
  import { useSaiAdmin } from '@/composables/useSaiAdmin'
  import api from '@/api/system/dept'
  import { ElMessage } from 'element-plus'
  import TableSearch from './modules/table-search.vue'
  import EditDialog from './modules/edit-dialog.vue'
  import DeleteChannelDialog from './modules/delete-channel-dialog.vue'

  const searchForm = ref({
    name: undefined,
    code: undefined,
    status: undefined
  })

  const deleteDialogVisible = ref(false)
  const deleteDeptId = ref<number | null>(null)
  const deleteDeptName = ref('')

  const handleSearch = (params: Record<string, any>) => {
    Object.assign(searchParams, params)
    getData()
  }

  const {
    columns,
    columnChecks,
    data,
    loading,
    getData,
    searchParams,
    resetSearchParams,
    handleSortChange,
    handleSizeChange,
    handleCurrentChange,
    refreshData
  } = useTable({
    core: {
      apiFn: api.list,
      columnsFactory: () => [
        { prop: 'name', label: 'page.table.deptName', minWidth: 200 },
        { prop: 'code', label: 'page.table.deptCode', minWidth: 120 },
        { prop: 'leader.username', label: 'page.table.leader', minWidth: 120 },
        { prop: 'remark', label: 'table.columns.common.description', minWidth: 150, showOverflowTooltip: true },
        { prop: 'sort', label: 'page.table.sort', width: 100 },
        { prop: 'status', label: 'page.table.status', saiType: 'dict', saiDict: 'data_status', width: 100 },
        { prop: 'create_time', label: 'page.table.createTime', width: 180, sortable: true },
        { prop: 'operation', label: 'table.actions.operation', width: 100, fixed: 'right', useSlot: true }
      ]
    }
  })

  const { dialogType, dialogVisible, dialogData, showDialog } = useSaiAdmin()

  const openDeleteDialog = (row: any) => {
    deleteDeptId.value = row.id
    deleteDeptName.value = row.name
    deleteDialogVisible.value = true
  }

  const handleSyncConfigs = async () => {
    try {
      await api.syncChannelConfigs()
      ElMessage.success('已为缺失配置的渠道补齐默认配置')
    } catch (e: any) {
      ElMessage.error(e?.message || '补齐失败')
    }
  }
</script>
