<template>
  <div class="mod-hotSearcch">
    <!-- 使用avue-crud构建CRUD表格 -->
    <avue-crud
      ref="crudRef"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="onSearch"
      @on-load="getDataList"
      @refresh-change="refreshChange"
      @selection-change="selectionChange"
    >
      <!-- 自定义左侧菜单按钮 -->
      <template #menu-left>
        <el-button
          v-if="isAuth('admin:hotSearch:save')"
          type="primary"
          icon="el-icon-plus"
          @click="onAddOrUpdate()"
        >
          新增
        </el-button>
        <el-button
          type="danger"
          :disabled="dataListSelections.length <= 0"
          @click.stop="onDeconste"
        >
          批量删除
        </el-button>
      </template>

      <!-- 自定义状态列内容 -->
      <template #status="scope">
        <el-tag v-if="scope.row.status === 0" type="danger">未启用</el-tag>
        <el-tag v-else>启用</el-tag>
      </template>

      <!-- 自定义行操作菜单 -->
      <template #menu="scope">
        <el-button
          v-if="isAuth('admin:hotSearch:update')"
          type="primary"
          icon="el-icon-edit"
          @click="onAddOrUpdate(scope.row.hotSearchId)"
        >
          修改
        </el-button>
        <el-button
          v-if="isAuth('admin:hotSearch:deconste')"
          type="danger"
          icon="el-icon-deconste"
          @click.stop="onDeconste(scope.row, scope.index)"
        >
          删除
        </el-button>
      </template>
    </avue-crud>

    <!-- 引入用于添加或更新记录的子组件 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="getDataList"
    />
  </div>
</template>

<script setup>
import { isAuth } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import { tableOption } from '@/crud/shop/hotSearch.js'
import AddOrUpdate from './add-or-update.vue'
import { ref, reactive, nextTick } from 'vue'

// 数据列表引用
const dataList = ref([])

// 分页信息
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})

// 表格加载状态
const dataListLoading = ref(false)

/**
 * 获取数据列表方法
 */
const getDataList = (pageParam, params, done) => {
  dataListLoading.value = true
  http({
    url: http.adornUrl('/admin/hotSearch/page'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 20
    }, params))
  })
    .then(({ data }) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
      dataListLoading.value = false
      if (done) done()
    })
}

// 多选框选中的数据
const dataListSelections = ref([])

/**
 * 多选回调
 * @param list - 用户选择的数据列表
 */
const selectionChange = (list) => {
  dataListSelections.value = list
}

// 控制添加或更新对话框显示/隐藏
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)

/**
 * 新增或修改热门搜索条目
 * @param id - 如果存在则为编辑模式，否则为新增模式
 */
const onAddOrUpdate = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}

/**
 * 条件查询触发的数据获取
 * @param params - 查询参数
 * @param done - 完成回调
 */
const onSearch = (params, done) => {
  getDataList(page, params, done)
}

/**
 * 删除单个或多个热门搜索条目
 * @param row - 如果传入row则删除单个条目，否则批量删除选中项
 */
const onDeconste = (row) => {
  const ids = row.hotSearchId ? [row.hotSearchId] : dataListSelections.value?.map(item => item.hotSearchId)
  ElMessageBox.confirm(`确定进行[${row.hotSearchId ? '删除' : '批量删除'}]操作?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      http({
        url: http.adornUrl('/admin/hotSearch'),
        method: 'delete',
        data: http.adornData(ids, false)
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              getDataList()
            }
          })
        })
    }).catch(() => {})
}

/**
 * 刷新页面数据
 */
const refreshChange = () => {
  getDataList(page)
}
</script>
