<template>
  <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="100px"
    >
    <el-form-item label="指标名称" prop="name" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入指标名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="指标值名称" prop="value" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-input
          v-model="queryParams.value"
          placeholder="请输入指标值名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="指标分类" prop="type" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择指标分类"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_METRIC_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注说明" prop="remark" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注说明"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="指标值类型" prop="valueType" v-hasPermi="['menduner:reward:event-metric:query']">
        <el-select
          v-model="queryParams.valueType"
          placeholder="请选择指标值类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MENDUNER_METRIC_VALUE_TYPE)"
            :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:event-metric:query']">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery" v-hasPermi="['menduner:reward:event-metric:query']">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['menduner:reward:event-metric: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:event-metric: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 label="id" prop="id"/>
      <el-table-column :show-overflow-tooltip="true" label="指标名称" prop="name"/>
      <el-table-column label="指标值" prop="value"/>
      <el-table-column label="指标值类型" prop="valueType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MENDUNER_METRIC_VALUE_TYPE" :value="scope.row.valueType" />
        </template>
      </el-table-column>
      <el-table-column label="指标分类" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MENDUNER_METRIC_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="创建时间" prop="createTime" :formatter="dateFormatter"/>
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button v-hasPermi="['menduner:reward:event-metric:update']" link type="primary" @click="openForm('update', scope.row.id)">修改</el-button>
          <el-button v-hasPermi="['menduner:reward:event-metric: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: 'EventIndicators' })
import SavePage from './save.vue'
import download from '@/utils/download'
import { dateFormatter } from '@/utils/formatTime'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { getEventMetricList, deleteEventMetric, downloadEventMetric } from '@/api/menduner/reward/eventIndicators'

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

const getList = async () => {
  loading.value = true
  try {
    const data = await getEventMetricList(queryParams)
    items.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
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 deleteEventMetric(id)
    message.success(t('common.delSuccess'))
    getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await downloadEventMetric({})
    download.excel(data, '事件指标.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
</script>
