<template>
  <div class="recommend-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">添加</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-button icon="el-icon-refresh" type="primary" @click="fetchData">刷新</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="排序" prop="sort" show-overflow-tooltip width="80" />

      <el-table-column align="center" label="图片" width="100">
        <template slot-scope="scope">
          <el-tooltip class="item" :content="scope.row.resourceId" effect="dark" placement="top">
            <el-image
              fit="cover"
              :preview-src-list="[scope.row.imageUrl]"
              :src="getThumbUrl(scope.row.imageUrl)"
              style="width: 50px; height: 50px"
            />
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column align="center" label="场景" show-overflow-tooltip>
        <template #default="{ row }">
          {{ getSceneName(row.scene) }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="类型" prop="type" show-overflow-tooltip />

      <el-table-column align="center" label="资源ID" prop="resourceId" show-overflow-tooltip />

      <el-table-column align="center" label="标签" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tag v-if="row.tag === 'new'" type="success">最新</el-tag>
          <el-tag v-else-if="row.tag === 'hot'" type="danger">热门</el-tag>
          <span v-else>{{ row.tag || '--' }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否启用" show-overflow-tooltip>
        <template #default="{ row }">
          <el-switch active-color="#13ce66" inactive-color="#ff4949" :value="row.isEnabled" @change="handleToggleEnable(row, $event)" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button :disabled="!scope.row.isEnabled || isFirstEnabled(scope.row)" type="text" @click="handleMoveUp(scope.row)">
            上移
          </el-button>
          <el-button :disabled="!scope.row.isEnabled || isLastEnabled(scope.row)" type="text" @click="handleMoveDown(scope.row)">
            下移
          </el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete, doMoveUp, doMoveDown, toggleEnable } from '@/api/spring/index/index'
  import { getSceneName, getThumbUrl } from '@/utils/blessing'
  import Edit from './components/RecommendEdit'

  export default {
    name: 'RecommendManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        elementLoadingText: '正在加载...',
        queryForm: { pageNo: 1, pageSize: 10 },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      getSceneName,
      getThumbUrl,
      handleEdit() {
        this.$refs['edit'].showEdit()
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await doDelete({ ids: [row.id] })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.list || data // Handle if list is directly returned or wrapped
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      async handleToggleEnable(row, val) {
        const prev = row.isEnabled
        row.isEnabled = val
        try {
          const { msg } = await toggleEnable(row.id, val)
          if (msg) this.$baseMessage(msg, 'success')
          this.fetchData()
        } catch (e) {
          row.isEnabled = prev
          this.$baseMessage('更新失败', 'error')
        }
      },
      async handleMoveUp(row) {
        try {
          const { msg } = await doMoveUp(row.id)
          if (msg) this.$baseMessage(msg, 'success')
          this.fetchData()
        } catch (e) {
          this.$baseMessage('操作失败', 'error')
        }
      },
      async handleMoveDown(row) {
        try {
          const { msg } = await doMoveDown(row.id)
          if (msg) this.$baseMessage(msg, 'success')
          this.fetchData()
        } catch (e) {
          this.$baseMessage('操作失败', 'error')
        }
      },
      isFirstEnabled(row) {
        if (!this.list || this.list.length === 0) return false
        const enabled = this.list.filter((item) => item.isEnabled)
        if (enabled.length === 0) return false
        return enabled[0].id === row.id
      },
      isLastEnabled(row) {
        if (!this.list || this.list.length === 0) return false
        const enabled = this.list.filter((item) => item.isEnabled)
        if (enabled.length === 0) return false
        return enabled[enabled.length - 1].id === row.id
      },
    },
  }
</script>
