<template>
  <el-container class="adv-view panel-block">
    <el-header class="sa-header">
      <!-- 简化搜索组件 -->
      <div class="search-container">
        <sa-search-simple :searchFields="searchFields" :defaultValues="defaultSearchValues"
          v-model="currentSearchParams" @search="handleSearch" @reset="handleReset">
        </sa-search-simple>
      </div>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">{{ t('modules.adv.title') }}</div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="handleRefresh"></el-button>
          <el-button icon="Plus" type="primary" @click="addRow">{{ t('modules.adv.create') }}</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 prop="id" label="ID" min-width="100" sortable="custom">
          </el-table-column>
          <el-table-column :label="t('modules.adv.advTitle')" min-width="150">
            <template #default="scope">
              <span class="sa-table-line-1">
                {{ scope.row.title || '-' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.adv.advImage')" min-width="120">
            <template #default="scope">
              <el-image v-if="scope.row.advImage" :src="scope.row.advImage" style="height: 80px" fit="contain"
                :preview-src-list="[scope.row.advImage]" :z-index="9999" preview-teleported />
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.adv.advType')" min-width="120">
            <template #default="scope">
              <el-tag type="primary">
                {{ getAdvTypeText(scope.row.advType) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.adv.linkType')" min-width="100">
            <template #default="scope">
              <el-tag :type="scope.row.linkType === 'IN' ? 'success' : 'warning'">
                {{ getLinkTypeText(scope.row.linkType) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.adv.link')" 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('modules.adv.serialNo')" min-width="100">
            <template #default="scope">
              {{ scope.row.serialNo || 0 }}
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.adv.status')" min-width="100">
            <template #default="scope">
              <el-tag :type="scope.row.statusFlag ? 'success' : 'danger'">
                {{ getStatusText(scope.row.statusFlag) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column :label="t('modules.adv.createTime')" min-width="160">
            <template #default="scope">
              {{ scope.row.createTime || '-' }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" :label="t('modules.adv.actions')" min-width="120">
            <template #default="scope">
              <el-button class="is-link" type="primary" @click="editRow(scope.row)">{{ t('modules.adv.edit')
              }}</el-button>
              <el-popconfirm width="fit-content" :confirm-button-text="t('modules.adv.confirm')"
                :cancel-button-text="t('modules.adv.cancel')" :title="t('modules.adv.confirmDelete')"
                @confirm="deleteApi(scope.row.id)">
                <template #reference>
                  <el-button class="is-link" type="danger">{{ t('modules.adv.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, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { api } from '../content.service';
import { ElMessage } from 'element-plus';
import { useModal } from '@/sheep/hooks';
import { usePagination } from '@/sheep/hooks';
import advEdit from './edit.vue';

const { t } = useI18n();
const { pageData } = usePagination();

// 搜索字段配置 - 使用计算属性以支持语言切换
const searchFields = computed(() => ({
  title: {
    type: 'input',
    label: t('modules.adv.advTitle'),
    placeholder: t('modules.adv.enterAdvTitle'),
    width: 200,
  },
}));

// 默认搜索值
const defaultSearchValues = reactive({
  title: '',
});

// 当前搜索条件 - 使用 ref 支持双向绑定
const currentSearchParams = ref({});

// 列表
const table = reactive({
  data: [],
  order: '',
  sort: '',
});
const loading = ref(true);

// 广告类型文本映射
const getAdvTypeText = (type) => {
  const typeMap = {
    INDEX: t('modules.adv.indexPage'),
  };
  return typeMap[type] || type;
};

// 链接类型文本映射
const getLinkTypeText = (type) => {
  const typeMap = {
    IN: t('modules.adv.internalLink'),
    OUT: t('modules.adv.externalLink'),
  };
  return typeMap[type] || type;
};

// 状态文本映射
const getStatusText = (status) => {
  return status ? t('modules.adv.enabled') : t('modules.adv.disabled');
};
// 获取数据
async function getData(page, searchParams = null) {
  if (page) pageData.page = page;
  loading.value = true;

  // 构建请求参数 - 优先使用传入的参数，否则使用双向绑定的搜索条件
  const finalSearchParams = searchParams !== null ? searchParams : currentSearchParams.value;

  try {
    const { code, data } = await api.adv.list({
      page: pageData.page,
      size: pageData.size,
      queryStr: finalSearchParams.title || '',
    });
    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('modules.adv.getDataFailed'));
    table.data = [];
  }
  loading.value = false;
}
// table 字段排序
function fieldFilter({ prop, order }) {
  table.order = order == 'ascending' ? 'asc' : 'desc';
  table.sort = prop;
  // 使用当前搜索条件进行排序
  const finalSearchParams = currentSearchParams.value || {};
  getData(pageData.page, finalSearchParams);
}

// 刷新处理 - 保持当前筛选条件
const handleRefresh = () => {
  // 使用当前搜索条件刷新数据
  const finalSearchParams = currentSearchParams.value || {};
  getData(pageData.page, finalSearchParams);
};

function addRow() {
  useModal(
    advEdit,
    { title: t('modules.adv.createAdv'), type: 'add' },
    {
      confirm: () => {
        getData();
      },
    },
  );
}

function editRow(row) {
  useModal(
    advEdit,
    {
      title: t('modules.adv.editAdv'),
      type: 'edit',
      id: row.id,
    },
    {
      confirm: () => {
        getData();
      },
    },
  );
}
// 删除api
async function deleteApi(id) {
  try {
    const { code } = await api.adv.delete(id);
    if (code == 200) {
      ElMessage.success(t('modules.adv.deleteSuccess'));
      getData();
    }
  } catch (error) {
    console.error('删除失败:', error);
    ElMessage.error(t('modules.adv.deleteFailed'));
  }
}

// 搜索处理
const handleSearch = () => {
  // 由于使用了 v-model，currentSearchParams 会自动更新
  // 直接调用 getData，会自动使用当前的搜索条件
  getData(1);
};

// 重置处理
const handleReset = () => {
  // 由于使用了 v-model，currentSearchParams 会自动清空
  // 直接调用 getData，会自动使用当前的搜索条件
  getData(1, {});
};

onMounted(() => {
  getData();
});
</script>
<style lang="scss" scoped>
.adv-view {
  .el-header {
    height: auto;
  }

  .el-main {
    .sa-table-wrap {
      height: 100%;
    }
  }
}
</style>
