<template>
  <div class="topic-category-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.keyword" clearable placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</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="分类名称" min-width="150" prop="title" width="100" />
      <el-table-column align="center" label="排序" prop="sort" width="120">
        <template #default="{ row }">
          <el-input v-model="row.sort" placeholder="排序" size="mini" @blur="handleMove(row)" @keyup.enter.native="handleMove(row)" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="状态" width="100">
        <template #default="{ row }">
          <el-switch v-model="row.enable" :active-value="true" :inactive-value="false" @change="handleToggleStatus(row)" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="100">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

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

<script>
  import { getList, toggleEnable, moveTo } from '@/api/rating/topicCategory'
  import { formatTime } from '@/utils'
  import TopicCategoryEdit from './components/TopicCategoryEdit'

  export default {
    name: 'RatingTopicCategoryIndex',
    components: { TopicCategoryEdit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          keyword: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatTime,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const { data } = await getList(this.queryForm)
          this.list = data.list
          this.total = data.totalCount
        } catch (error) {
          console.error(error)
        } finally {
          this.listLoading = false
        }
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      async handleToggleStatus(row) {
        try {
          const { msg } = await toggleEnable(row.id, row.enable)
          this.$baseMessage(msg || '状态切换成功', 'success')
          this.fetchData()
        } catch (error) {
          console.error(error)
          row.enable = !row.enable
        }
      },
      async handleMove(row) {
        // 如果输入为空或不是数字，给出提示并重新获取列表恢复原值
        const sortVal = Number(row.sort)
        if (isNaN(sortVal)) {
          this.$message.error('排序必须是数字')
          this.fetchData()
          return
        }

        try {
          const { msg } = await moveTo(row.id, sortVal)
          this.$baseMessage(msg || '移动成功', 'success')
          this.fetchData()
        } catch (error) {
          console.error(error)
          this.fetchData()
        }
      },
    },
  }
</script>

<style scoped>
  .topic-category-container {
    padding: 20px;
  }
</style>
