<template>
  <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="100px"
    >
      <el-form-item label="规则名称" prop="title" v-hasPermi="['menduner:reward:point-rule-config:query']">
        <el-input
          v-model="queryParams.title"
          class="!w-240px"
          clearable
          placeholder="请输入规则名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status" v-hasPermi="['menduner:reward:point-rule-config:query']">
        <el-select
          v-model="queryParams.status"
          class="!w-240px"
          clearable
          placeholder="请选择状态"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_POINT_RULE_CONFIG_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['menduner:reward:point-rule-config:query']">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery" v-hasPermi="['menduner:reward:point-rule-config:query']">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['menduner:reward:point-rule-config:create']"
          plain
          type="primary"
          @click="openForm('create')"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['menduner:reward:point-rule-config:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="items"
      row-key="id"
    >
      <el-table-column :show-overflow-tooltip="true" label="规则名称" prop="title"/>
      <el-table-column :show-overflow-tooltip="true" label="事件地址" prop="url"/>
      <el-table-column label="规则类型" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MENDUNER_POINT_RULE_CONFIG_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="积分处理" prop="operation">
        <template #default="scope">
          <span>{{ scope.row.operation === '+' ? '递增' : '递减' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="积分数量" prop="point"/>
      <el-table-column :show-overflow-tooltip="true" label="创建时间" prop="createTime" :formatter="dateFormatter"/>
      <el-table-column label="状态" prop="status" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MENDUNER_POINT_RULE_CONFIG_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button v-if="scope.row.status === '1'" link type="success" @click="handleAction(scope.row.id, 1)">开启</el-button>
          <el-button v-else link type="warning" @click="handleAction(scope.row.id, 0)">关闭</el-button>
          <el-button v-hasPermi="['menduner:reward:point-rule-config:update']" link type="primary" @click="openForm('update', scope.row.id)">修改</el-button>
          <el-button v-hasPermi="['menduner:reward:point-rule-config:delete']" link type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <SavePage ref="formRef" @success="getList" />
</template>

<script setup>
defineOptions({ name: 'Integral' })
import SavePage from './save.vue'
import download from '@/utils/download'
import { dateFormatter } from '@/utils/formatTime'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { getPointRuleConfigPage, deletePointRuleConfig, pointRuleConfigDownload, closeRoleConfig, openRoleConfig } from '@/api/menduner/system/integral'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const total = ref(0)
const queryFormRef = ref()
const queryParams = reactive({
  title: null,
  status: null,
  pageNo: 1,
  pageSize: 10
})
const exportLoading = ref(false)
const loading = ref(false)
const items = ref([])

const getList = async () => {
  const { list, total: number } = await getPointRuleConfigPage(queryParams)
  items.value = list
  total.value = number
}
getList()

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type, id) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    await message.delConfirm()
    await deletePointRuleConfig(id)
    message.success(t('common.delSuccess'))
    getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await pointRuleConfigDownload(queryParams)
    download.excel(data, '积分规则配置表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 开启、关闭按钮操作 */
const handleAction = async (id, type) => {
  try {
    await message.delConfirm(`是否${type ? '开启' : '关闭'}所选中数据？`)
    if (type) {
      await openRoleConfig([id])
    } else {
      await closeRoleConfig([id])
    }
    message.success(t('common.updateSuccess'))
    getList()
  } catch {
  } finally {
  }
}
</script>
