<template>
  <el-container class="banner-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">{{ t('modules.banner.title') }}</div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button icon="Plus" type="primary" @click="addRow">{{ t('common.add') }}</el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0">
      <div class="sa-table-wrap panel-block panel-block--bottom" v-loading="loading">
        <el-table height="100%" class="sa-table" :data="table.data" @sort-change="fieldFilter" row-key="id" stripe>
          <template #empty>
            <sa-empty />
          </template>
          <el-table-column type="selection" width="48" align="center"></el-table-column>
          <el-table-column prop="id" label="ID" min-width="100" sortable="custom">
          </el-table-column>
          <el-table-column :label="t('modules.banner.bannerTitle')" min-width="200">
            <template #default="scope">
              <span class="sa-table-line-1">
                {{ scope.row.title || '-' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.banner.image')" min-width="120" align="center">
            <template #default="scope">
              <el-image v-if="scope.row.image" :src="scope.row.image" style="width: 80px; height: 50px" fit="cover"
                :preview-src-list="[scope.row.image]" :z-index="9999" preview-teleported />
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.banner.linkType')" min-width="100" align="center">
            <template #default="scope">
              <el-tag :type="scope.row.linkType === 0 ? 'primary' : 'success'">
                {{ scope.row.linkType === 0 ? t('modules.banner.internalLink') : t('modules.banner.externalLink') }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.banner.linkAddress')" min-width="200">
            <template #default="scope">
              <span class="sa-table-line-1">
                {{ scope.row.link || '-' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column :label="t('common.sort')" min-width="80" align="center">
            <template #default="scope">
              {{ scope.row.seq || 0 }}
            </template>
          </el-table-column>
          <el-table-column :label="t('common.createTime')" min-width="160">
            <template #default="scope">
              {{ scope.row.createTime || '-' }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" :label="t('common.actions')">
            <template #default="scope">
              <el-button class="is-link" type="primary" @click="editRow(scope.row)">{{ t('common.edit') }}</el-button>
              <el-popconfirm width="fit-content" :confirm-button-text="t('common.confirm')"
                :cancel-button-text="t('common.cancel')" :title="t('message.confirmDelete')"
                @confirm="deleteApi(scope.row.id)">
                <template #reference>
                  <el-button class="is-link" type="danger">{{ t('common.delete') }}</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
import { useModal, usePagination } from '@/sheep/hooks';
import api from '../../api';
import BannerEdit from './edit.vue';

const { t } = useI18n();

// 分页数据
const { pageData } = usePagination();

// 表格数据
const table = reactive({
  data: [],
  selection: [],
});

const loading = ref(false);

// 获取数据
async function getData(page = pageData.page, searchData = {}) {
  loading.value = true;
  try {
    const params = {
      page: page,
      size: pageData.size,
      ...searchData,
    };

    const { code, data } = await api.banner.list(params);
    if (code == 200) {
      table.data = data.list;
      pageData.page = data.pageNum;
      pageData.size = data.pageSize;
      pageData.total = data.total;
    }
  } catch (error) {
    console.error('获取轮播图列表失败:', error);
    ElMessage.error(t('message.fetchDataFailed'));
  } finally {
    loading.value = false;
  }
}

// 新建
function addRow() {
  useModal(
    BannerEdit,
    {
      title: t('modules.banner.addBanner'),
      type: 'add',
    },
    {
      confirm: () => {
        getData();
      },
    },
  );
}

// 编辑
function editRow(row) {
  useModal(
    BannerEdit,
    {
      title: t('modules.banner.editBanner'),
      type: 'edit',
      id: row.id,
    },
    {
      confirm: () => {
        getData();
      },
    },
  );
}

// 删除
async function deleteApi(id) {
  try {
    const { code } = await api.banner.delete({ id });
    if (code == 200) {
      getData();
    }
  } catch (error) { }
}

// 字段排序
function fieldFilter({ prop, order }) {
  // 实现排序逻辑
  console.log('排序:', prop, order);
}

onMounted(() => {
  getData();
});
</script>

<style lang="scss" scoped>
.banner-view {
  .search-container {
    margin-bottom: 16px;
  }
}
</style>
