<template>
  <div>
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="权益名称">
          <el-input
              class="w-[280px]"
              v-model="queryParams.name"
              placeholder="精准匹配"
              clearable
              @keyup.enter="resetPage"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never" v-loading="pager.loading">
      <div>
        <el-button v-perms="['article:cate:add']" type="primary" @click="handleAdd()">
          <template #icon>
            <icon name="el-icon-Plus"/>
          </template>
          新增权益配置
        </el-button>
      </div>
      <el-table class="mt-4" size="large" :data="pager.lists">
        <el-table-column type="index" width="55"/>
        <el-table-column label="权益图标" min-width="100">
          <template #default="{ row }">
            <image-contain
                v-if="row.icon"
                :src="row.icon"
                :width="60"
                :height="45"
                fit="contain"
            />
          </template>
        </el-table-column>
        <el-table-column label="权益名称" prop="name" min-width="120" show-tooltip-when-overflow/>
        <el-table-column label="权益价值(元)" prop="value" min-width="80"/>
        <el-table-column label="使用方式" prop="useMethod" min-width="100" :formatter="useMethodFormatter"/>
        <el-table-column label="备注" prop="remark" min-width="120"/>
        <el-table-column label="操作" min-width="180" fixed="right">
          <template #default="{ row }">
            <el-button
                v-perms="['article:cate:edit']"
                type="primary"
                link
                @click="handleEdit(row)"
            >
              编辑
            </el-button>
<!--            <el-button-->
<!--                v-perms="['article:cate:del']"-->
<!--                type="danger"-->
<!--                link-->
<!--                @click="handleDelete(row.id)"-->
<!--            >-->
<!--              删除-->
<!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false"/>
  </div>
</template>
<script lang="ts" setup>
import { useElabPaging } from '@/hooks/useElabPaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {queryBenefits} from "@/api/membership";

const queryParams = reactive({
  name: ''
})
const { pager, getLists, resetPage, resetParams } = useElabPaging({
  fetchFun: queryBenefits,
  params: queryParams
})
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const useMethodFormatter = (row: any) => {
  return row.useMethod == 0 ? '仅展示': ''
}
const volumeFormatter = (row: any) => {
  if (row.volume == 0) {
    return '大'
  } else if (row.volume == 1) {
    return '中'
  } else if (row.volume == 2) {
    return '小'
  } else if (row.volume == 3) {
    return '关闭'
  } else {
    return '大'
  }
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  // await articleCateDelete({id})
  await delPrinter({id})
  feedback.msgSuccess('删除成功')
  getLists()
}

const changeStatus = async (id: number, status: any) => {
  try {
    await editPrinter({id, status})
    feedback.msgSuccess('修改成功')
    getLists()
  } catch (error) {
    getLists()
  }
}
getLists()
</script>
